#!/usr/local/bin/python -i
#==============================================================================
#
#  $Id: dopyserver,v 1.7 1999/07/09 13:48:47 mike Exp $
#
"""
   Sample DOPY server program.
   
   Usage:
      dopyserver
   
   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.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

tcp.makeServer(9600)
hub = dopy.getHub()
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()
