VASmalltalk – 0MQ – Request and Reply example

Here is another example from the user guide of 0MQ: a classical server-client communication:

Ask and Ye Shall Receive example in the 0MQ Guide
		
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 its own
external API thread

  socket1 := MSKZMQReplySocket
               newIn: context
               address: 'tcp://*:5555'
               ownCore: true 
               domain: nil 
               block: [ :s |
                 | stringFromClient |
									
                 Transcript cr ; show: 'Server Socket Working !'.
                 [ s continueReceiveLoop ] whileTrue:[ 					
                   (stringFromClient := s receiveStringNonBlocking) notNil 
                   ifTrue:[
                     Transcript cr ; show: 'ReplySocket: Message from Client: ', stringFromClient.
                     s send: 'World'.
                   ]
                   ifFalse:[ (Delay forMilliseconds: 50) wait ]. 
                 ]. 									
                 Transcript cr ; show: 'Server Socket closing !'.
                 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 := MSKZMQRequestSocket
               newIn: context
               address: 'tcp://localhost:5555'
               ownCore: true 
               domain: nil 
               block: [ :s |
                 Transcript cr ; show: 'Client Socket Working !'.
                 1 to: 10 do: [ :anIndex | | continueFlag |
                   Transcript cr ; show: 'Client sending request !'.
                   s send: 'Hello'.								
                   continueFlag := true.
                   [ continueFlag ] whileTrue:[
                     | receivedString |							
                     (receivedString := s  receiveStringNonBlocking) notNil
                       ifTrue:[ 	
                         Transcript cr; show: (anIndex printString),': Answer from Server: ',receivedString.
                         continueFlag := false ]
                        ifFalse:[ (Delay forMilliseconds: 100) wait ]
                    ]
                ].
                s close.			
                Transcript cr ; show: 'Client Socket Finished !'.
             ].
						
and the socket is started						

  socket2 
    startWithBackgroundPriority.						
						

socket2 is closed automatically after all 10 requests are send
and socket1 has to be closed manually by:						
						
  socket1 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