VASmalltalk – 0MQ – moving target …

With each day doing some work with this framework the wrappers changes. Actually the code from yesterday is snow from yesterday and everything is melting away🙂

I rearranged and change the class hierarchy and now each Smalltalk item in the 0MQ network is named a “ZMQNode”. The difference between this class and a specific subclass of ZMQSocket (like MSKZMQReplySocket) is, that a node can work with lots of sockets and implements an internal logic in a serialized programming way.

Due to the fact, that the internal logic is implemented as blocks it would be possible to send the node a string with a block definition and let the node call the Smalltalk compiler itself and change its own logic with the new block compiled block definition.

The setup of the needed sockets are now done in a more configurable way:

Considering the request/replay example from the 0MQ guide the setup of both nodes (one is the server, one is the client – each with only one socket):

 context := ZMQCore newContext.
 "the server"
 (node1 := context newNode)
                 setupFromConfiguration: 
                   (ZMQNodeCfg new initialize
                     name: 'server' ;
                     socketCfgs: (OrderedCollection new
                                   add: (ZMQSocketCfg 
                                          fromArray: #('push' 'tcp://127.0.0.1:12360' 'push' true ));
                                   yourself);
                     yourself);
                 handleBlock: self node1HandleBlock.
  "the client"
  (node2 := context newNode) 
    setupFromConfiguration: 
      (ZMQNodeCfg new initialize
         name: 'client' ;
         socketCfgs: (OrderedCollection new
                        add: (
                          ZMQSocketCfg fromArray: #('pull' 'tcp://127.0.0.1:12360' 'pull' false ));
                        yourself);
	 yourself);
    handleBlock: self node2HandleBlock.		

where the client code (as an example) is now defined in Smalltalk like:

node2HandleBlock

^[ :node | 
    | socket |
    socket := node socketNamed: 'pull'.
    [ node continueBlockFlag ] whileTrue:[ 
      | recString |
      [
        recString := socket receiveStringBlocking.
        recString = 'end' ifTrue:[ node abortLoop ].
      ]
      when: ZMQExceptions::ExZMQ do: [ :sig | sig exitWith: nil]
      when: ZMQExceptions::ExETerm do: [ :sig | node abortLoop. sig exitWith: nil].
    ].
].

Remember, that both nodes are running in different Smalltalk processes, having their own external thread running for socket access. Running both nodes (without Transcript work) on a computer in the same image and Smalltalk (for the first) time uses all cores on my machine.

Understanding 0MQ and their “objects” and it’s pretty easy to rewrite the 0MQ based code in a different language – here as an example C#. The interaction between both languages worked at once. There are now more than 30 bindings available for different programming languages (e.g. GnuSmalltalk, Ruby, Java, C++, Erlang, C#, Haskell, …)

string upstreamAddress = "tcp://127.0.0.1:12360";
ZMQ.Socket upstreamSocket = new Socket(SocketType.PULL);
upstreamSocket.Connect(upstreamAddress);
string receivedString = "";
bool continueFlag = true;

while (continueFlag)
{
    receivedString = System.Text.Encoding.Default.GetString(upstreamSocket.Recv());

    if (receivedString == "end")
    {
        continueFlag = false;
    }
    else
    {
        //System.Console.WriteLine(counter.ToString() + ": " + receivedString);
    }

}

upstreamSocket.Dispose();

By the way. The guide of zeromq is a wonderful documentation of different types of distributed computing and connecting all stuff together with zeromq.

Another remark: the “0MQ” sockets are NOT plain sockets. You can not connect a plain socket against zeromq (e.g. with telnet). Therefore its not a replacement for the standard socket interface. If you want to have Smalltalk sockets talks with 0MQ library socket, you must implement “ZeroMQ Message Transport Protocol” in Smalltalk sockets.

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