Vizard 7 » Reference » Networking » viznet
7.7

viznet

viznet is a wrapper around Vizard network objects that establishes a client server based communication. viznet greatly simplifies the process of sending data to all peers in a networking environment as the peers no longer need to keep track of each other, and thus only deal with communication to the server.

 

Among other things, viznet's separation between client and server can be utilized to centralize process intensive calculations on the server, whilst the clients will be used solely for data input and display.

Server

Defining the Connection

To create a server application in vizard the function viznet.server.start must be called. This function can take a single parameter port which if specified will use the defined port for both input and output, or this function can take two parameters port_in and port_out. The port_in parameter specifies the port which listen for events on and the port_out specifies which port to send events to.

 

Specifying port_in and port_out as different ports is ideal when wanting to run a client and server on the same machine, as otherwise they'll both be listening for events on the same port and then it is just a race condition for which one receives it.

Note: only one client can run on a machine at a time.

import viznet
# Listen on 14950
# Send on 14951
viznet.server.start(port_in=14950,port_out=14951)
Server Functions

The server has functions which allow it to start, stop, send messages and disconnect clients. The start function starts a server which listens and sends on the specified ports as described above. A server can be stopped with the use of the viznet.server.stop function.

 

Sending a message to an individual client is accomplished with the sendClient function which takes the client's network identifier (ip, computer name), the event id, and the message parameters.

 

Sending a message to all clients is accomplished with the send function which is similar to the sendClient function without the added network identifier.

 

Removing a client from the server is performed with the removeClient function which simply takes the client's network identifier as a parameter.

Server Events

The viznet server has two predefined events the first, CLIENT_CONNECT_EVENT occurs whenever a client connects and the second, CLIENT_DISCONNECT_EVENT, occurs whenever a client disconnects.

 

The server also will handle user defined events which is explained below.

# Display Client Name when connected
def onStartClient(e):
    print('** Client joined:',e.sender)
viz.callback(viznet.CLIENT_CONNECT_EVENT,onStartClient)

# Display Client Name when disconnected
def onStopClient(e):
    print('** Client left:',e.sender)
viz.callback(viznet.CLIENT_DISCONNECT_EVENT,onStopClient)

Client

Connecting to a Server

Connecting to a server is accomplished with the use of the connect function which takes the computer name and either the connection port or an input port and output port. The function will return true on success or false on failure.

import viznet
if viznet.client.connect('localhost',port_in=14951,port_out=14950):
    print('Connected')

In the previous example the client connects to the server on the same machine. This client when used with the simple server from above will display a message on the server when it joins and another message on the server when it leaves.

Client Functions

In addition to connect, a client can send messages and disconnect from the server.

 

Disconnecting is accomplished with the disconnect function, which needs no parameters.

def onStopServer(e):
    print('Disconnected from server')
viz.callback(viznet.SERVER_DISCONNECT_EVENT,onStopServer)

Sending a message to the server is done with the send function which needs the event id and message parameters.

 

Additionally a message can be sent to each client with the sendAll function. This takes the same parameters as send however once received by the server it will be sent to each client, including the sending client.

def sendUpdate():
    ori = viz.MainView.getEuler()
    pos = viz.MainView.getPosition()
    viznet.client.sendAll(UPDATE,client=viz.getComputerName(),pos=pos,ori=ori)
vizact.ontimer(.05, sendUpdate)

The above example will send position and orientation data from this client to all other clients every .05 second.

 

When using the sendAll function the message which reaches the client will appear to be from the server, not the original sender. It is a good idea to add an additional message parameter, like in the above example, which specifies the original sender using the client parameter.

Client Events

The only predefined event for the client is SERVER_DISCONNECT_EVENT which occurs when the client is disconnected from the server by use of the server's removeClient function or when the server application is closed.

User Defined Events

In addition to predefined events, user defined events can be created with the viznet.id function. This function takes in a string and returns a unique id. This unique id will be the same on any computer generated on which is important to ensure the client and server are sending or handling the proper event.

 

To handle the event on either the client or the server a callback needs to be setup with the first argument being the event id created with viznet.id.

UPDATE = viznet.id('update')
def update(e):
    print('** Update from',e.sender)
viz.callback(UPDATE,update)

See also

In this section:

Networking basics

Multi User Environment

Networking Command Table

Cluster basics

Other sections:

Tutorial: Network communication