VASmalltalk – 0MQ

Ok, in the last posting you saw a very simple example and the plain code using the low-level Smalltalk wrapper around the API.

Here – after changing the structure – a more OO like approach in a sense of programming as TobSocket is doing it:

Reference to:
Execute in the  workspace:
  context := ZMQCore newContext: 1.
special class with additional logic. somehow the
way TobSocket does it ...

Here we define the send socket - using the external
API thread of context

socket1 := MSKZMQDownstreamSocket
             newIn: context
             address: 'tcp://'
             ownCore: false 
             domain: nil 
             block: [ :s |
               (Delay forSeconds: 5) wait.	
               1 to: 10 do: [ :anIndex |
                 s send: 'Hello ',(anIndex printString).
                 (Delay forSeconds: 5) wait.							
		s close.					
here the server is started:
special class for upstream service. Here we give the 
socket its own API external thread (core)
socket2 := MSKZMQUpstreamSocket
             newIn: context
             address: 'tcp://'
             ownCore: true 
             domain: nil  
             block: [ :s |
               [ s continueReceiveLoop ] whileTrue:[ 
                 | receivedString |							
                 (receivedString := s  receiveStringNonBlocking) notNil
                   ifTrue:[ Transcript cr; show: '1: ',receivedString ]
                   ifFalse:[(Delay forMilliseconds: 100) wait ]
               s close.			
and the socket is started						

socket1 is closed automatically after all 10 messages are send
and socket2 has to be closed manually by:						
  socket2 setDomainObjectToFalse

  context term								
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: Logo

You are commenting using your 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