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: http://www.youtube.com/watch?v=_JCBphyciAs&feature=autoplay&list=PLAB622092B91FA1FA&lf=results_video&playnext=4
		
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://127.0.0.1:12345'
             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:
	
  socket1
    startWithBackgroundPriority.
							
special class for upstream service. Here we give the 
socket its own API external thread (core)
						
socket2 := MSKZMQUpstreamSocket
             newIn: context
             address: 'tcp://127.0.0.1:12345'
             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						

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

or:
	
  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:

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