pyhelloselect - an object server that implements a mainloop in conjuction with ILU's mainloop using select.

This ILU application 'pyhello' is written in python. It defines one interface 'HelloObject', one class 'Hello', and one method 'hello'. This application is composed of a server and a client. The server's responibility is to serve to all of its clients the phrase "Hello Stanford DL Library from Python" when the 'hello' method is called on 'Hello'. The client just need to know how to get to the server and call the right method in order to receive this wonderful greeting.

To start the server type:

	HelloObjectServer.py &

The server will print out a bunch of garbage. This garbage is the binding handle the client needs in order to find the server anywhere on the Internet. You will need to use this handle as the first parameter [binding_handle} to HelloObjectClient.py.

Note: The server needs to be place in the background with the '&' symbol at the end of the command line.

To start the client type:

	HelloObjectClient.py "[binding_handle]" 

Note: remember to place double quotes around the binding_handle.

The client should print "Hello Stanford DL project!" and exit. The server will continue to wait and process new hello messages.


The Code


HelloObject.isl


INTERFACE HelloObject;

TYPE Hello = OBJECT OPTIONAL
	METHODS
		hello () : ilu.CString
	END;

HelloObjectServer.py


#!/usr/local/bin/python
#

import os, socket, sys, ilu, HelloObject__skel
import select

class Hello(HelloObject__skel.Hello):
    def hello(self):
	return "Hello Stanford DL Project!";

class IOHandler:
  def __init__(self, fd, func, rock):
    self.fd = fd
    self.func = func
    self.rock = rock
  def fileno(self):
    return self.fd
  def call(self):
    self.func(self.fd, self.rock)

class FDMap:
  def __init__(self):
    self.ifdMap = {}
    self.ofdMap = {}
  def RegisterInput(self, iohc):
##    print "registerInput:", iohc, type(iohc)
    fd = iohc.fileno()
    self.ifdMap[fd] = iohc
    return 1
  def RegisterOutput(self, iohc):
##    print "registerOutput:", iohc
    fd = iohc.fileno()
    self.ofdMap[fd] = iohc
    return 1
  def UnregisterInput(self, fd):
##    print "unregisterInput:", fd
    if self.ifdMap.has_key(fd):
      del self.ifdMap[fd]
    else:
      return None
    return 1
  def UnregisterOutput(self, fd):
##    print "unregisterOutput:", fd
    if self.ofdMap.has_key(fd):
      del self.ofdMap[fd]
    else:
      return None
    return 1

  def MainLoop(self):
    while 1:
      results = select.select(self.ifdMap.keys(), self.ofdMap.keys(), [], )
##      print results
      ifd = results[0]
      ofd = results[1]
      efd = results[2]

      for fd in ifd:
	if self.ifdMap.has_key(fd):
	  self.ifdMap[fd].call()

      for fd in ofd:
	if self.ofdMap.has_key(fd):
	  self.ofdMap[fd].call()


    

_fdMap = FDMap()

def _reg_inp(iohc):
  global _fdMap
  return _fdMap.RegisterInput(iohc)

def _can_inp(fd):
  global _fdMap
  return _fdMap.UnregisterInput(fd)

def _reg_out(iohc):
  global _fdMap
  return _fdMap.RegisterOutput(iohc)

def _can_out(fd):
  global _fdMap
  return _fdMap.UnregisterOutput(fd)

def _set_alarm(thc):
  pass

def _can_alarm():
  pass

def cbHello(fd, rock):
##  line = os.read(fd, 1)
  line = input()
  try:
    print eval(line)
  except SyntaxError:
    print "SyntaxError"
  except:
    print "error"
    pass


def main(argv):
  global _fdMap
  server = ilu.CreateServer(None, None, None);

  mach = Hello()
  print mach.IluSBH()

  iohc = IOHandler(sys.stdin.fileno(), cbHello, 42)
  _fdMap.RegisterInput(iohc)

  ilu.AddRegisterersToDefault(_reg_inp, _can_inp, _reg_out, _can_out, \
			      _set_alarm, _can_alarm)
  _fdMap.MainLoop()


main(sys.argv)

HelloObjectClient.py


#!/usr/local/bin/python
#

import sys, ilu, HelloObject

argc = len(sys.argv)
if argc < 2:
    print 'usage:', sys.argv[0], '[sbh-machine1]'
    sys.exit(1)

HelloObjectInstance = ilu.ObjectOfSBH(HelloObject.Hello, sys.argv[1])

print HelloObjectInstance.hello()
print



Imakefile


NormalObjectRule()
DependTarget()

InterfaceTarget(HelloObject.isl)

#ifdef ADD_PYTHON_LANGUAGE

ILUPythonTarget(HelloObject.py HelloObject__skel.py, HelloObject.isl)
all:: HelloObject.py HelloObject__skel.py

#endif