#!/usr/local/bin/python -i
#==============================================================================
#
#  $Id: dopyserver,v 1.9 2001/08/04 19:23:24 mike Exp $
#
"""
   Sample DOPY server program.
   
   Usage:
      dopyserver [threads=none|select|com|func]
   
   dopyserver publishes the "test" object, which contains some very
   basic methods for testing the protocol.
"""
#
#  Copyright (C) 1999 Michael A. Muller
#
#  Permission is granted to use, modify and redistribute this code,
#  providing that the following conditions are met:
#
#  1) This copyright/licensing notice must remain intact.
#  2) If the code is modified and redistributed, the modifications must 
#  include documentation indicating that the code has been modified.
#  3) The author(s) of this code must be indemnified and held harmless
#  against any damage resulting from the use of this code.
#
#  This code comes with ABSOLUTELY NO WARRANTEE, not even the implied 
#  warrantee of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
#
#  $Log: dopyserver,v $
#  Revision 1.9  2001/08/04 19:23:24  mike
#  Added support for multiple threading modes.
#
#  Revision 1.8  2001/07/13 02:05:49  mike
#  Added two threading modes (mainly so that communication processing doesn't
#  have to consume thread resources).
#
#  Revision 1.7  1999/07/09 13:48:47  mike
#  Added tests for persistence services.
#
#  Revision 1.6  1999/06/16 14:32:22  mike
#  First cut at name services.
#
#  Revision 1.5  1999/06/11 14:03:55  mike
#  Made remote objects transportable.
#
#  Revision 1.4  1999/04/24 20:58:13  mike
#  Cleaned up for distribution.
#
#
#==============================================================================

import os, sys

# remove the current directory from the search path so that this runs ok
# from the dopy library directory
if sys.path[0] in ('', os.curdir):
   del sys.path[0]

import dopy
from dopy import tcp

class Test:
   
   def hello(self, str):
      print 'hello', str, \
         'received from', hub.getContext().protocol().getProtocolId()
      return 'hello ' + str

   def kwparms(self, p1, p2):
      print 'kwparms =', p1, p2, \
         'received from', hub.getContext().protocol().getProtocolId()
      return (p1, p2)
   
   def raiseException(self):
      print 'raiseException received from', \
         hub.getContext().protocol().getProtocolId()
      raise Exception("Something bad happened")

   def getTransient(self):
      print 'getTransient received from', \
         hub.getContext().protocol().getProtocolId()
      return hub.makeAnonObject(Transient())

   def testCallback(self, remote, text):
      print 'testCallback received from', \
         hub.getContext().protocol().getProtocolId()
      callbackValue = remote.doit(text)
      print 'got return value from callback:', callbackValue
      return callbackValue

class Transient:
   """
      This class is a test of a transient object which will be created
      and registered anonymously.
   """
   def hello(self, str):
      print 'transient hello', str, \
         'received from', hub.getContext().protocol().getProtocolId()
      return 'hello ' + str

# get command line parms
threadMode = -1
for arg in sys.argv[1:]:
   if arg[:8] == 'threads=':
      threadMode = \
         {
         'none': dopy.THRD_NONE,
         'select': dopy.THRD_SELECT,
         'func': dopy.THRD_FUNC,
         'com': dopy.THRD_COM
         }.get(arg[8:], -1)
      
      if threadMode == -1:
         print 'Unknown thread mode "%s"' % arg[8:]
   else:
      print 'unknown option %s' % repr(arg)

if threadMode == -1:
   hub = dopy.getHub()
else:
   hub = dopy.getHub(threadMode = threadMode)
tcp.makeServer(9600)
hub.addObject('test', Test())

# add a naming context - making this a name server!!
from dopy.naming import StandardNamingContext
namingContext = StandardNamingContext()
hub.addObject('naming', namingContext)

# add persistant object services
from dopy.pos import FileSysDirectory
hub.addObject('pos', FileSysDirectory('.', 'pos'))

hub.mainloop()
