-
Notifications
You must be signed in to change notification settings - Fork 3
Python TCP Eventbus Client
You can download the library for python here. Then unzip it. Within that folder there is a folder called “Eventbus”. It’s our python module. There is another folder “simple example” which includes the example that we are going to discuss.
Let’s get the “client” folder of the “simple example”. Add Eventbus module into it.
It’s our client.
Then take the “server” folder of the “simple example”. It’s our server.

Figure 1- File structure
I assume you have installed maven and vert.x 3.2.1. If not,
Download Vertx from here
This provides a quick guide to install vert.x here
Run Server
Then open terminal in “server” folder.
1. Build the package
>> mvn clean package
2) run the server
>>java -jar target/Server-1.0-SNAPSHOT-fat.jar

Figure 2- Server started
Run Client
Then open terminal in “client” folder.
>>python Client.py

Figure 3 – Client running
Figure 3 – Server running
If you could run examples then you done. Let’s discuss the client and server examples
Client class

Main thread
One of most important thing of the socket which is created by Eventbus is “It takes all data immediately and hand over them to handlers” unless you tell them to wait.
Create Eventbus

Eventbus(instance, host, port)
Later we are going to use methods of Client class. Because of that we should pass it as a parameter. Instance is an essential parameter. Default values of host and port are localhost and 7000.
DeliveryOption

DeliveryOption object is holding few important parameters,
1. Headers
2. Reply address
3. Time interval
Headers
headers can be added to a message using a DeliveryOption object.
Reply address
Reply address of the message can be added to a message using a DeliveryOption object.
Time Interval
After that time if a reply message did not get, reply handler will be called with a timeout error. We will discuss more about it later.
Registering a handler

registerHandler(address, DeliveryOption, Handler)
After registration any message that comes to the address will be routed to the Handler.
Handlers
Handlers are the functions that take only one argument. Function name does not matter.
Eg: Handler(message), add(msg)
The message is a dictionary object.
Send

Body can be defined as a dictionary object.
send(address, body, DeliveryOption)
The address and the body are essential parameters.
send(address, body, DeliveryOption,replyHandler)
Reply Handlers
Reply handlers are the functions that take only 2 arguments. Function name does not matter.
Eg: replyHandler(error, message), testReply(err, msg)
The message and error are dictionary objects.
If a replyHandler has been set, the program will wait for a message DeliveryOption.timeInterval period. If a reply message received, program will execute next line and message will be routed to the replyHandler with a null error. If not replyHandler will be called with a “TIMEOUT” error.
Close socket

Finally, socket must be closed.
closeConnection(timeInterval)
Socket will be close automatically after the timeinterval. You can wait for other messages in this period. Default timeInterval is 30 sec.
There is another example “TimeKeeper”. Check it for more information.