#!/usr/local/bin/python
#==============================================================================
#
#  $Id: dopyclient,v 1.3 1999/04/24 20:58:13 mike Exp $
#
"""
   Sample DOPY client program.
   
   Usage:
      dopyclient <server-host>
   
   dopyclient attaches to the DOPY server program on the given host (using
   the local host name if non is specified) and performs a suite of test 
   message invocations.
"""
#
#  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: dopyclient,v $
#  Revision 1.3  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.tcp

import socket

# get the name of the host to that the server is running on, either from the
# command line or from the socket module

if len(sys.argv) == 2:
   host = sys.argv[1]
else:
   host = socket.gethostname()
   
# TCP/IP port number to connect to
port = 9600

# get the remote object

print 'connecting to %s:%d...' % (host, port)
obj = dopy.tcp.remote(host, port, 'test')

# try a simple method invocation

try:
   if obj.hello('client!') == 'hello client!':
      print 'test of basic method invocation ok'
   else:
      raise Exception()
except Exception, ex:
   print 'test of basic method invocation failed'

# try sending keyword parms

try:
   if obj.kwparms(p1 = 100, p2 = 200) == (100, 200):
      print 'test of keyword parms ok'
   else:
      raise Exception()
except Exception, ex:
   print 'test of keyword parms failed'

# try calling a function that raises an exception

try:
   obj.raiseException()
except Exception, ex:
   print 'test of exceptions ok'
else:
   print 'test of exceptions failed'
