# Ported from C++ by Hugh Perkins June 2005
# Third VOS tutorial. Local and Remote Vobjects and Messages.
#
# This tutorial file covers:
# - The difference between local and remote Vobjects
# - Accessing a remote Vobject
# - Creating a message and filling its fields
# - Sending a message to another Vobject
#
# This file (vostut3client.py) is released into the public domain. No
# restrictions are placed on its use, distribution or inclusion into
# other works. */
from vos import *
import traceback
import sys
# You must run this program after running vostut3server.
print "VOS Tutorial 3 Client\n"
localsite = Site()
lvse = LocalVipSiteExtension()
localsite.addSiteExtension( lvse )
# If the server process reports that it is using a different port
# that 4231 then you will need to supply it on the command line.
siteurl = ""
if( len( sys.argv ) > 1):
siteurl = sys.argv[1]
else:
siteurl = "vip://localhost:4231"
try:
# The primary purpose of VOS is to allow transparent access to
# Vobjects, whether they are local or remote. A "local" Vobject
# resides in the same process and address space as this application.
# It does not involve any sort of network activity (even a
# loopback socket) to manipulate a local Vobject. A "remote"
# Vobject is anything which requires some sort of network
# activity to access.
#
# Vobjects are located using URLs. The URL expresses the
# hostname and port of the site the Vobject resides upon,
# and the name of the object we want to access. The static
# method findObjectFromRoot() takes care of connecting to the
# remote site, getting information about the Vobject and
# creating an instance of RemoteVobject which will represent
# the Vobject in our application. */
#
primus = Vobject.findObjectFromRoot( siteurl + "/primus" )
# All the methods we demonstrated in the previous tutorial
# are available. Compare the following output to the same
# code in the server:
site = primus.getSite()
print "vostut3client: Vobject site name is: " + primus.getSiteName()
print "vostut3client: Vobject URL is: " + primus.getURLstr()
print "vostut3client: Vobject site is: " + site.getURLstr()
#print "vostut3client: Our local site is: " + localsite.getURLstr()
print "vostut3client: Primus is local? " + str( primus.isLocal() )
print "vostut3client: Primus is remote? " + str( primus.isRemote() )
# Vobjects can exchange messages with one another. One way
# to think about a Vobject is as a mailbox: an address which
# can send and receive messages intended for a certain part
# of your application. Let's say hello to the "primus"
# object.
# Messages are reference counted and should always be
# assigned to a vRef<>. When assigning a bare pointer to a
# smart pointer, you must tell it whether to increment the
# reference count or not. A true value for the second
# parameter means increment, a false value means do not
# increment. Normally you supply "true", but when an object
# is newly created, its reference count is already set to 1.
# Since we want the smart pointer to delete the object when
# we are done with it, we must tell vRef<> not to increment
# the pointer, so that when it is done it will decrement it
# to 0 and actually delete the object.
m = Message()
# It seem odd that we need to say that the message type of a
# message is "message" but in fact there are a few different
# types of messages with different semantics. A simple
# "message" is sent directly from one Vobject to another.
# Other messages types are covered in a later tutorial.
m.setType("message")
# The message is directed to the primus Vobject. You should
# always use the full URL of the Vobject you are sending to.
m.setTo(primus.getURLstr())
# The message comes from our local site.
m.setFrom(localsite.getURL().getString())
# The method. This is the action expressed by this message.
m.setMethod("tutorial:hello")
# Now we add some fields. Fields of a VOS message are
# ordered and consist of a key and a value. The first
# parameter is the key, and the second parameter is the value
# associated with that key. Fields are ordered and
# distinguished by position, so keys may appear more than
# once.
m.appendField("unus", "one")
m.appendField("word", "Caesar")
m.appendField("unus", "ichi")
m.appendField("duo", "two")
# Send the messages to the "primus" Vobject. That's it!
# Simple!
print "vostut3client: sending message..."
primus.sendMessage(m)
# The following exceptions may be emitted by
# Vobject::findObjectFromRoot():
except:
print "exception :-/"
print traceback.print_exc(file=sys.stdout)