Skip to content

Instantly share code, notes, and snippets.

@Intey
Created December 16, 2019 14:07
Show Gist options
  • Save Intey/1a851ba6e710e911881dcd77b5c46ed7 to your computer and use it in GitHub Desktop.
Save Intey/1a851ba6e710e911881dcd77b5c46ed7 to your computer and use it in GitHub Desktop.
Red ZeroMQ from Red(not Red/System)
Red [
Title: "Red Jupyter kernel implementation"
Author: "Intey"
File: "%kernel.red"
Tabs: 4
License: "MIT"
; Needs: ['json 'zeromq] ;; waiting for 0.8.0
]
#system [
#define ZMQ_ASSERT(r) [
if r < 0 [print-line ["ZMQ [" zmq/errno "]: " zmq/strerror zmq/errno ]]
]
#include %ZeroMQ/ZeroMQ.reds
]
start-server: routine [
/local
ctx
responder
r
buffer
bytes
][
ctx: zmq/ctx_new
responder: zmq/socket ctx ZMQ_REP
print-line ["ZMQ Context: " ctx]
print-line ["ZMQ Responder: " responder]
r: zmq/bind responder "tcp://*:5556"
ZMQ_ASSERT(r)
if r <> 0 [
print-line "ZMQ bind failed!"
quit -1
]
r: zmq/setsockopt responder ZMQ_SUB as byte-ptr! "" 0
ZMQ_ASSERT(r)
buffer: allocate 256
bytes: 0
forever [
print-line "Waiting for request..."
bytes: zmq/recv responder buffer 255 0
ZMQ_ASSERT(bytes)
if bytes >= 0 [
if bytes > 255 [bytes: 255]
bytes: bytes + 1
buffer/bytes: #"^@" ;to create valid c-string ending just in case
]
print-line ["Received request: " as c-string! buffer]
r: zmq/send responder as byte-ptr! "World" 5 0
ZMQ_ASSERT(r)
]
]
start-server
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment