Project

General

Profile

Manual for the Client and Server class

Design of the Server class (used by the handler scripts)

Creating the the ServerHandler object [ServerHandler(port)]

To create an object of ServerHandler class one would type: server = ServerClass.ServerHandler(50008), where 50008 represents the port number 50008 (one should choose a port that is not used by the system itself or some other application)

Opening the socket and listening for a connection [openSocket()]

After creating the object (the code above this topic) we need to tell the object to try to open the defined port (in our example it is the port 50008) and to listen on the same port (i.e. listen for the client to connect to it). We can do that by calling the openSocket() function. The openSocket() function will either produce a 0 if it couldn't open the predefined port number or it will remain in a wait state until the client connects. openSocket() will return you an integer value:

1, if the port was successfully opened and the client connected to the port (this may happen after some time, no other code will be executed)
0, if the function openSocket() couldn't open the port for some reason (after the function returns a 0, one may print out the error by using print objectName.error)

Receiving data from the client [receiveData(timeout)]

After the client successfully connected to our server we would like to send or receive some data. Our server class is not an interrupt driven class but a well defined state machine. Once you call the receiveData(timeout) function, it waits until it receives a packet/packets or by having a set timer that will timeout the function after some time so that a deadlock is avoided. You can define the timeout with the timeout variable, if it is set to 0 (means no timeout, wait forever until data is received) and for every other value it will produce a timeout in case no data is received after that amount of seconds has passed. After calling the receiveData(0) function it will return you a string value:

'NO DATA', if there are no data to receive, means that one of the sides got disconnected
data, where data represents a string value sent by the client
'TIMEOUT', if a timeout was triggered
0, if you were not connected
2, if an unknown error appeared (set debuMode = 1, if you want to see the error)
3, if the connection was reset by the other side

Sending data to the client [sendData(data)]

By now we know how to receive data but the server(handler) should also be able to send data back to the client (e.g. send the results to the client). This can be done by using the function sendData(data), where data represents a string. After calling this function, it will return you an integer value:

1, if the data was sent successfully
0, if you are not connected and the data couldn't be sent
2, if an unknown error happened (set debugMode = 1, if you want to see what the error was)

Closing the connection [closeConnection()]

When you finish using the socket, it is appropriate to close the port. To close the port one would call the closeConnection() function. The function will return you an integer value:

1, if it succeeded in closing the port
0, if you were not connected then it won't close the port
2, if an unknown error happened (set debugMode = 1, if you want to see what the error was)
3, if the other side closed the connection

Killing a port [killPort()]

Apparently before solving the "bug" (it is defined by default in python not to allow you to use the same port in 60 seconds because there could be delayed TCP/IP packets still arriving to its destination) that didn't allow us to create a new server object on the same port after the port was closed in the duration of 60 seconds. Anyways, the function remained in the class. So if some other application tries to use the same port as your server object (e.g. two parallel running handlers on the same machine) this function will find the PID of the application and kill it. After the function has been called, it will return you an integer value:

1, if it successfully tried to kill the port
0, if it wasn't a successful try of killing the port (it may happen that another application with higher privileges is using the port and we can't do anything there)

Finding out the IP address of the client [connectedTo()]

If one wants to find out the IP address of the client, the function connectedTo() should be called. It will return you the IP address of the client. If the resulting value is 127.0.0.1 then you are either connected to the localhost or not connected at all.

Example code of using the Server class

import ServerClass

while 1:
        server = ServerClass.ServerHandler(50008) #define the port
        tried = server.openSocket()

        if server.error != 'No error':
                print server.error
                if server.error == '[Errno 98] Address already in use':
                        print 'one should try to kill the port'
                        print server.killPort()

        if server.connected == 1:
                test = server.receiveData(0)
                print test

        if server.connected == 1:
                if server.sendData(test) == 1:
                        print 'data sent successfully'

        if server.connected == 1:
                print server.receiveData(0)

        close = server.closeConnection()
        if close == 1:
                print 'Closed connection successfully'

        del server

Design of the Client class (used by the main controller)

Creating the the Connection object [Connection(host, port)]

Now we want to create a client (main controller will use this object for connecting to the handlers), client = ClientClass.Connection('localhost',50008) would create an object of the ClientClass. You call the function with two parameters. The first parameter is a string and represents the host address (i.e. location of the server) and the second parameter is the port number on which it should connect (the port on which the server listens on should be the same as on the client side).

Connecting the client (main controller) to the server (handlers) [connect()]

After we have created the client object we would like to connect to the server (i.e the handler) now. If you want to connect to the server you would call the connect() function, the function will return you an integer value:

1, if the client object successfully connected to the server
0, if it couldn't connect to the server

Receiving data from the server [receiveData()]

After the client successfully connected to our server we would like to send or receive some data. Our client class is not an interrupt driven class but a well defined state machine. Once you call the receiveData(timeout) function, it waits until it receives a packet/packets or by having a set timer that will timeout the function after some time so that a deadlock is avoided. You can define the timeout with the timeout variable, if it is set to 0 (means no timeout, wait forever until data is received) and for every other value it will trigger a timeout in case no data is received after that amount of seconds has passed. After calling the receiveData(0) function it will return you a string value:

'TIMEOUT', if a timeout was triggered
0, if you were not connected
2, if an unknown error appeared (set debuMode = 1, if you want to see the error)
3, if the connection was reset by the other side

Sending data to the server [sendData(data)]

By now we know how to receive data but the client(main controller) should also be able to send data back to the server (e.g. send the commands to the handler what it should do). This can be done by using the function sendData(data), where data represents a string. After calling this function, it will return you an integer value:

1, if the data was sent successfully
0, if you are not connected and the data couldn't be sent
2, if an unknown error happened (set debugMode = 1, if you want to see what the error was)

Closing the connection [closeConnection()]

When you finish using the socket, it is appropriate to close the port. To close the port one would call the closeConnection() function. The function will return you an integer value:

1, if it succeeded in closing the port
0, if you were not connected then it won't close the port
2, if an unknown error happened (set debugMode = 1, if you want to see what the error was)
3, if the other side already closed the connection

Example code of how to utilize the ClientClass

import ClientClass

#MAIN PART 
x = ClientClass.Connection('132.230.4.69',50008)
print x.connect()
x.sendData('hello server :)')
print x.receiveData(0)
x.sendData('I send you another message')
x.closeConnection()

Code of the ClientClass

The code for the ClientClass can be found in the repository in the file ClientClass.py