# Simulator for the pyuipc module #------------------------------------------------------------------------------ import const import cmd import threading import socket import time import sys import struct import cPickle #------------------------------------------------------------------------------ # Version constants SIM_ANY=0 SIM_FS98=1 SIM_FS2K=2 SIM_CFS2=3 SIM_CFS1=4 SIM_FLY=5 SIM_FS2K2=6 SIM_FS2K4=7 #------------------------------------------------------------------------------ # Error constants ERR_OK=0 ERR_OPEN=1 ERR_NOFS=2 ERR_REGMSG=3 ERR_ATOM=4 ERR_MAP=5 ERR_VIEW=6 ERR_VERSION=7 ERR_WRONGFS=8 ERR_NOTOPEN=9 ERR_NODATA=10 ERR_TIMEOUT=11 ERR_SENDMSG=12 ERR_DATA=13 ERR_RUNNING=14 ERR_SIZE=15 #------------------------------------------------------------------------------ # The version of FSUIPC fsuipc_version=0x0401 lib_version=0x0302 fs_version=SIM_FS2K4 #------------------------------------------------------------------------------ class FSUIPCException(Exception): """FSUIPC exception class. It contains a member variable named errorCode. The string is a text describing the error.""" errors=["OK", "Attempt to Open when already Open", "Cannot link to FSUIPC or WideClient", "Failed to Register common message with Windows", "Failed to create Atom for mapping filename", "Failed to create a file mapping object", "Failed to open a view to the file map", "Incorrect version of FSUIPC, or not FSUIPC", "Sim is not version requested", "Call cannot execute, link not Open", "Call cannot execute: no requests accumulated", "IPC timed out all retries", "IPC sendmessage failed all retries", "IPC request contains bad data", "Maybe running on WideClient, but FS not running on Server, or wrong FSUIPC", "Read or Write request cannot be added, memory for Process is full"] def __init__(self, errorCode): """ Construct the exception """ if errorCode> sys.stderr, "pyuipc_sim.Server._process: failed with exception:", str(e) finally: try: socketFile.close() except: pass clientSocket.close() #------------------------------------------------------------------------------ class Client(object): """Client to the server.""" def __init__(self, serverHost): """Construct the client and connect to the given server host.""" self._socket = socket.socket() self._socket.connect((serverHost, PORT)) self._socketFile = self._socket.makefile() def read(self, data): """Read the given data.""" data = cPickle.dumps((CALL_READ, [data])) self._socket.send(struct.pack("I", len(data)) + data) (length,) = struct.unpack("I", self._socket.recv(4)) data = self._socket.recv(length) (resultCode, result) = cPickle.loads(data) if resultCode==RESULT_RETURNED: return result else: raise result #------------------------------------------------------------------------------ #------------------------------------------------------------------------------ # FIXME: implement proper completion and history class CLI(threading.Thread, cmd.Cmd): """The command-line interpreter.""" def __init__(self, clientSocket): """Construct the CLI.""" self._socket = clientSocket self._socketFile = clientSocket.makefile("rwb") threading.Thread.__init__(self) cmd.Cmd.__init__(self, stdin = self._socketFile, stdout = self._socketFile) self.use_rawinput = False self.intro = "\nPyUIPC simulator command prompt\n" self.prompt = "PyUIPC> " self.daemon = True def run(self): """Execute the thread.""" try: self.cmdloop() except Exception, e: print "pyuipc_sim.CLI.run: command loop terminated abnormally: " + str(e) try: self._socketFile.close() except: pass self._socket.close() def do_set(self, args): """Handle the set command.""" words = args.split() if len(words)==2: variable = words[0] if variable in ["zfw"]: values.__setattr__(variable, float(words[1])) else: print >> self._socketFile, "Unhandled variable: " + variable return False def help_set(self): """Print help for the set command.""" print >> self._socketFile, "set " def do_quit(self, args): """Handle the quit command.""" return True #------------------------------------------------------------------------------ if __name__ == "__main__": client = Client("127.0.0.1") print client.read([(0x3bfc, "d")]) else: server = Server() server.start() #------------------------------------------------------------------------------