VASmalltalk – 0MQ 4.0 and the stream socket

0MQ uses a framed message technique when sending/receiving their messages over network. The problem is, that a program with a plain socket can not connect to the 0MQ infrastructure.

With release 4.0 the developers have introduced a special socket, which enables the user to send/receive data to plain sockets. They did it by introducing a new socket type: the stream socket.

With that socket type one can build a small http server example:

| aContext aZMQSocketConfiguration aSocket |
"create a context"
aContext := ZMQExtendedContext newContextNamed: 'StreamExample'.
aContext startWithOneThread.

"define the socket configuration values"
aZMQSocketConfiguration := ZMQStreamSocket defaultSocketConfigurationAt: 
                        (ZMQEndpoint protocol: 'tcp' interface: '127.0.0.1' port: 4133).
aZMQSocketConfiguration
  shouldConnectViaBind: true.

"create the socket"	
aSocket := ZMQStreamSocket newSocketWithConfiguration: aZMQSocketConfiguration in: aContext.

"connect the socket to the network"
aSocket
  cmdConnect.

"example via announcements ..."	
aSocket announcer
  subscribe: ZMQTelegramReceivedAnnouncement 
  do: [ :announcement :announce |
    | response telegram streamSocket identity content answerStream |
    telegram := announcement zmqTelegram.
    streamSocket := telegram socket.
    identity := telegram frames first.
    content := telegram frames last.
		
    answerStream := WriteStream on: ByteArray new.
		
    Transcript 
      cr ;
      show: content data asString.
			
    answerStream
      nextPutAll: 'HTTP/1.0 200 OK' ;
      nextPutAll: Character cr asString;
      nextPutAll: Character lf asString;
      nextPutAll: 'Content-Type: text/plain' ;
      nextPutAll: Character cr asString;
      nextPutAll: Character lf asString;
      nextPutAll: Character cr asString;
      nextPutAll: Character lf asString;
      nextPutAll: 'Hello, World!'.
	
    "send the answer"	
    response := ZMQTelegram  newOnSocket: streamSocket.
    response
      addFrame: identity ;
      addFrame: (ZMQBinaryFrame newWithData: answerStream contents);
      sendTelegram.

    "the following telegram closes the connection"			
    response := ZMQTelegram  newOnSocket: streamSocket.
    response
      addFrame: identity ;
      addFrame: (ZMQTextFrame newWithData: '');
      sendTelegram.		
   ].

The following stuff is printed in the local transcript:

GET / HTTP/1.1
Host: 127.0.0.1:4133
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
DNT: 1
Connection: keep-alive

and the browser just shows:

Hello,World!
This entry was posted in Smalltalk and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s