# Ported from C++ by Hugh Perkins <hughperkins@gmail.com> June 2005

import time

from vos import *

# You must run this program before running vostut3client.


# This tutorial demonstrates how to handle incoming messages.  The
# following class provides a method to be called when a message is
# received.  Message handler objects must derive from the RefCounted
# class (meaning they support reference-counted memory management).

def handleMessage( msg):
    print "got message"
    print " Python message object recieve is: ",
    print msg
    # First print out the message
    print " received a message:\n" + \
              msg.getFormattedString()

    # Messages have a number of standard fields.  The "method"
    # field indicates the action the sender is expressing.
    print "Method is: " + msg.getMethod()

    # Another standard field is "from".  There is of
    # course a "to" field as well :-)
    print "vostut3server: The object " + msg.getFrom() + \
       " said hello to us!  Isn't that nice."

    try:
        # We can access a particular field using its name.  If no
        # field by that name is present, then a NoSuchFieldError
        # exception will be thrown.  You cannot access the fixed to,
        # from, method, etc fields this way (use getTo(), getFrom(),
        # getMethod() instead). */
        
        # Note that try, except is *not* sufficient to catch the C++ exceptions
        # thrown at us by vos, but I think there is something in swig
        # to handle this?

     #   print "vostut3server: The word of the day is \"" + \
     #        msg.getField("word").value + "\""
       pass

    except:
        print "vostut3server: Oops, no \"word\" field is present, no word of the day for us."

    print "vostut3server: The fields in this message are:"

    # The fields in a message are also ordered and so we can iterate
    # over them:
    for i in range( 0, msg.numFields() ):
        print " " + str( i ) + "  " + msg.getField(i).getKey() \
                  + "=\"" \
                  + msg.getField(i).getValue()

print "VOS Tutorial 3 Server\n\n"

localsite = Site()
lvse = LocalVipSiteExtension()
localsite.addSiteExtension(lvse)
localsite.setDefaultPolicy("core:accept-all")

primus = localsite.createVobject("primus")

# Since primus was created on localsite, calling getSite() on
# primus just gives us back localsite.  Compare the output of
# following to the output of the same code on the client.

site = primus.getSite()
print "vostut3server: Vobject site name is: " + primus.getSiteName()
print "vostut3server: Vobject URL is: " + primus.getURLstr()
print "vostut3server: Vobject site is: " + site.getURLstr() 
#print "vostut3server: Our local site is: " + localsite.getURLstr()

# The difference between local and remote objects is discussed in
# vostut3client.
print "vostut3server: Primus is local? " + str( primus.isLocal() )
print "vostut3server: Primus is remote? " + str( primus.isRemote() )

# Create our message handler object, passing it a pointer to
# primus.

# tmh = TutorialMessageHandler(primus)
# tmh = VosCallback()

# Register a message handler for primus by specifying a method on
# a particular object to be called.  In this case, when a message
# with method "tutorial:hello" is delivered to primus, it will
# call the handleSampleMessage() method on the object "tmh" that
# we have created.

primus
vobjectbase = primus.getVobjectBase()
print str(primus)
print "objectbase=" + str(vobjectbase)

pymessages = pyMessages()

vobjectbase.addPyMessageHandler("tutorial:hello", pymessages )

while True:
   time.sleep(0.02)
   msg = pymessages.getNextMessage()
   if not msg is None:
      handleMessage( msg )

