Author: derek Date: Thu Sep 7 22:47:31 2006 New Revision: 80 Added: sofaswitch/trunk/callflow/example_correct.py Log: This works. Added: sofaswitch/trunk/callflow/example_correct.py ============================================================================== --- (empty file) +++ sofaswitch/trunk/callflow/example_correct.py Thu Sep 7 22:47:31 2006 @@ -0,0 +1,185 @@ +#!/usr/bin/python + +import sys +import os +import time +import socket +import Queue +import distutils +import thread +from twisted.internet import reactor, protocol + +class Controller(protocol.Protocol): + def __init__(self): + self.graphSet = False + self.connectionIsMade = False + self.endProgramNow = False + self.quote = "\"" + + def setGraphData(self, graphFile): + self.gd=open(graphFile, "r"); + self.gdc=self.gd.read() + self.data = eval(self.gdc) + self.gd.close() + + self.firstNode = None + for k,v in self.data.iteritems(): + if k[:16] == "Start_Processing": + self.firstNode = k + print "first node is ", k +# break +# self.timeout = reactor.callLater(0, self.startTimerExpired, "graph") + + def startTimerExpired(self, data): + print 'start of start timer expired' + if data == "graph": + self.connectionIsMade = True + if data == "connected": + self.graphSet = True + if self.graphSet and self.connectionIsMade: + print "start processing now " + + def startWorkNow(): + sTuple = "(" + self.quote + self.firstNode + self.quote + ", 0, " +self.quote + "Start_Processing" + self.quote + ")" + self.DoWork(eval(sTuple)) + + def DoWork(self, sTuple): + a,b,c = sTuple + + self.keepGoing = True + + while self.keepGoing: + command = "self." + str(c) + "(" + str(sTuple) + ")" + a,b,c = sTuple = eval(command) + + def makeConnection(self, transport): + print 'Make Connection here' + self.connected = 1 + self.transport = transport + self.connectionMade() + + def connectionMade(self): + print 'Start of connectionMade' + self.myId = "" + self.countReceived = 0 + self.transport.write("?\0") + self.transport.write("whoami\0"); + self.colonChar = ':' + print 'End of connection made' + + def connectionLost(self, reason): + print "connection lost" + + def dataReceived(self, data): + print "start of data received ", data + ssdata = str(data).split('\0') + for oneMessage in ssdata: + self.countReceived = self.countReceived + 1 + if self.countReceived == 1: + self.myId = oneMessage; + self.timeout2 = reactor.callLater(0, self.startTimerExpired, "connected") + else: + self.processReceivedMessage(oneMessage) + + def replyNow(self, data): + self.transport.write(self.tstring) + + + def Start_Processing(self, aTuple): + id,y,z = aTuple + return self.data[id]["Output"] + + def End_Processing(self, sTuple): + self.keepGoing = False + a,b,c = sTuple + print "end of processing at ", a + return (1,2,3) + + def Start_For(self, aTuple): + a,b,c = aTuple + if b==0: + exec(self.data[a]["Initialise"]) + else: + exec(self.data[a]["Increment"]) + + if eval(self.data[a]["Test"]): + return self.data[a]["Output"] + + next = self.data[a]["EndForLoop"] + return self.data[next]["AllDone"] + + def End_For_Loop(self, aTuple): + a,b,c = aTuple + return self.data[a]["LoopAgain"] + + def Conditional(self, aTuple): + a,b,c = aTuple + if variable == 0: + return self.data[a]["OutputTrue"] + return self.data[a]["OutputFalse"] + + def Make_Call(self, sTuple): + a,b,c = sTuple + return self.data[a]["Output"] + + def End_Call(self, sTuple): + a,b,c = sTuple + return self.data[a]["Output"] + + def Python(self, sTuple): + a,b,c = sTuple + exec(self.data[a]["Python Code"]) + return self.data[a]["Output"] + + def Request_Node(self, aTuple): + id,y,z = aTuple + return data[id]["Output"] + + def Timer(self, sTuple): + a,b,c = sTuple + return data[a]["Output"] + + def Eventer(self, sTuple): + a,b,c = sTuple + print "Eventer ", a + return (1,2,3) + + + def processReceivedMessage(self, thisMessage): + print "Received ", thisMessage + +class MyFactory(protocol.ClientFactory): + protocol = Controller + + def clientConnectionFailed(self, connector, reason): + print "Connection failed - goodbye!" + reactor.stop() + + def clientConnectionLost(self, connector, reason): + print "Connection lost - goodbye!" + reactor.stop() + + def buildProtocol(self, addr): + p = self.protocol() + p.factory = self + self.myController = p + self.myController.setGraphData(self.myGraphName) + print 'finished building Protocol' + return p + for key in dir(p): + if '__' not in key: + value = getattr(p,key) +# print key +# print key, " ",value + return p + + def setGraph(self, newName): + self.myGraphName = newName + +f = MyFactory() +f.setGraph("a_graph") +print 'think about connect' +reactor.connectTCP("localhost", 5000, f) +print 'done the connect' +reactor.run() +