diff --git a/scripts/socket/freepy/README b/scripts/socket/freepy/README index dae9520bdb..dd4c078212 100644 --- a/scripts/socket/freepy/README +++ b/scripts/socket/freepy/README @@ -9,8 +9,18 @@ Install See INSTALL +Debugging +========= + +Set FREEPY_DEBUG_ON = True in globals.py + +TODO: pull this from an environment variable or a config file + Rebulding State Machines ======================== + +(you only need to do this if you changed an .sm file) + for each .sm file: java -jar /usr/src/smc/bin/Smc.jar -python -g THE.sm diff --git a/scripts/socket/freepy/__init__.py b/scripts/socket/freepy/__init__.py index 0f70180bec..43bbfbc1a2 100644 --- a/scripts/socket/freepy/__init__.py +++ b/scripts/socket/freepy/__init__.py @@ -54,20 +54,20 @@ class FreepyDispatcher(LineReceiver): self.active_request = None # the current active (de-queued) request def connectionMade(self): - print "Connection made" + self.log("Connection made") self.conncb(self) def connectionLost(self, reason): if self.discocb: self.discocb(reason) - print "connectionLost: %s" % reason + self.log("connectionLost: %s" % reason) def log(self, msg): """ print a message to stdout if debug enabled """ - if freepy.globals.DEBUG_ON: + if freepy.globals.FREEPY_DEBUG_ON: print msg def login(self, passwd): @@ -166,7 +166,6 @@ class FreepyDispatcher(LineReceiver): TODO: add this """ - print "confdtmf called" if bgapi == True: msg = "bgapi conference %s dtmf %s %s" % \ (conf_name, member_id, dtmf) @@ -296,7 +295,7 @@ class FreepyDispatcher(LineReceiver): msg = "api sofia status profile %s as xml" % (profile_name) req = request.ApiRequest() self.requestq.put(req) - print "sending to fs: %s" % msg + self.log("sending to fs: %s" % msg) self.transport.write("%s\n\n" % msg) return req.getDeferred() @@ -357,9 +356,15 @@ class FreepyDispatcher(LineReceiver): def lineReceived(self, line): self.log("<< %s" % line) if not self.active_request: + + # if no active request pending, we ignore + # blank lines + if not line.strip(): + return + # if no active request, dequeue a new one if self.requestq.empty(): - # we are receiving data from fs without an + # we are receiving non-empty data from fs without an # active request pending. that means that # there is a bug in the protocol handler # (or possibly in fs) diff --git a/scripts/socket/freepy/bgapirequest.sm b/scripts/socket/freepy/bgapirequest.sm index 258dabd112..0894232f4a 100644 --- a/scripts/socket/freepy/bgapirequest.sm +++ b/scripts/socket/freepy/bgapirequest.sm @@ -25,11 +25,16 @@ ApiResponseStarted GotReplyText { BlankLine - Startup + nil { - setRequestFinished(); callOrErrback(); + } + JobUuid + Startup + { + setRequestFinished(); callOrErrback(); + } } @@ -43,8 +48,7 @@ Default nil { setRequestFinished(); - errbackDeferred("Protocol failure - was not expecting blank line"); - } + errbackDeferred("Protocol failure - was not expecting blank line"); } CommandReply nil @@ -64,7 +68,7 @@ Default nil { setRequestFinished(); - errbackDeferred("Protocol failure - was not expecting line needing to be processed"); + errbackDeferred("Protocol failure handling bgapi response - was not expecting line needing to be processed"); } } diff --git a/scripts/socket/freepy/bgapirequest_sm.py b/scripts/socket/freepy/bgapirequest_sm.py index c50076e700..135b7b5286 100644 --- a/scripts/socket/freepy/bgapirequest_sm.py +++ b/scripts/socket/freepy/bgapirequest_sm.py @@ -19,6 +19,9 @@ class BgApiRequestState(statemap.State): def CommandReply(self, fsm): self.Default(fsm) + def JobUuid(self, fsm): + self.Default(fsm) + def ProcessLine(self, fsm, line): self.Default(fsm) @@ -82,7 +85,7 @@ class MainMap_Default(BgApiRequestState): fsm.clearState() try: ctxt.setRequestFinished() - ctxt.errbackDeferred("Protocol failure - was not expecting line needing to be processed") + ctxt.errbackDeferred("Protocol failure handling bgapi response - was not expecting line needing to be processed") finally: fsm.setState(endState) @@ -109,10 +112,15 @@ class MainMap_ApiResponseStarted(MainMap_Default): class MainMap_GotReplyText(MainMap_Default): def BlankLine(self, fsm): - ctxt = fsm.getOwner() if fsm.getDebugFlag() == True: fsm.getDebugStream().write("TRANSITION : MainMap.GotReplyText.BlankLine()\n") + + def JobUuid(self, fsm): + ctxt = fsm.getOwner() + if fsm.getDebugFlag() == True: + fsm.getDebugStream().write("TRANSITION : MainMap.GotReplyText.JobUuid()\n") + fsm.getState().Exit(fsm) fsm.clearState() try: @@ -147,6 +155,11 @@ class BgApiRequest_sm(statemap.FSMContext): self.getState().CommandReply(self) self._transition = None + def JobUuid(self): + self._transition = 'JobUuid' + self.getState().JobUuid(self) + self._transition = None + def ProcessLine(self, *arglist): self._transition = 'ProcessLine' self.getState().ProcessLine(self, *arglist) diff --git a/scripts/socket/freepy/globals.py b/scripts/socket/freepy/globals.py index 708010b90e..6d8ccdcad5 100644 --- a/scripts/socket/freepy/globals.py +++ b/scripts/socket/freepy/globals.py @@ -1,2 +1,10 @@ -DEBUG_ON = True +import os + +if os.environ.has_key('FREEPY_DEBUG_ON'): + # pull from environment if avail + FREEPY_DEBUG_ON = os.environ['FREEPY_DEBUG_ON'] +else: + # fall back to hardcoded value + FREEPY_DEBUG_ON = False + diff --git a/scripts/socket/freepy/request.py b/scripts/socket/freepy/request.py index 9f00a58fea..c901976e6d 100644 --- a/scripts/socket/freepy/request.py +++ b/scripts/socket/freepy/request.py @@ -83,7 +83,7 @@ class FreepyRequest(object): otherwise, if the fs response is incomplete, just buffer the data """ - if not line or len(line) == 0: + if not line.strip() or len(line.strip()) == 0: self._fsm.BlankLine() return self.isRequestFinished() @@ -110,6 +110,16 @@ class FreepyRequest(object): self._fsm.ReplyText() return self.isRequestFinished() + matchstr = re.compile("Job-UUID", re.I) + result = matchstr.search(line) + if (result != None): + fields = line.split(":") # eg, ['Job-UUID','c9eee07e-508-..'] + endfields = fields[1:] + # ignore job uuid given on this line, take the one sent + # in Reply-Text response line + # self.response_content = "".join(endfields) + self._fsm.JobUuid() + return self.isRequestFinished() matchstr = re.compile("api/response", re.I) result = matchstr.search(line) @@ -125,7 +135,6 @@ class FreepyRequest(object): self._fsm.ContentLength() return self.isRequestFinished() - self._fsm.ProcessLine(line) return self.isRequestFinished() @@ -194,37 +203,12 @@ class BgApiRequest(FreepyRequest): linereceived: """ - def __init__(self): super(BgApiRequest, self).__init__() import bgapirequest_sm self._fsm = bgapirequest_sm.BgApiRequest_sm(self) - def processOLD(self, line): - - if not line or len(line) == 0: - self._fsm.BlankLine() - return self.isRequestFinished() - - matchstr = re.compile("command/reply", re.I) - result = matchstr.search(line) - if (result != None): - self._fsm.CommandReply() - return self.isRequestFinished() - - matchstr = re.compile("Reply-Text", re.I) - result = matchstr.search(line) - if (result != None): - self.response_content = line.split(":")[1] - self._fsm.ReplyText() - return self.isRequestFinished() - - self._fsm.ProcessLine(line) - return self.isRequestFinished() - - - def getResponse(self): # subclasses may want to parse this into a meaningful @@ -252,28 +236,6 @@ class ApiRequest(FreepyRequest): self._fsm = apirequest_sm.ApiRequest_sm(self) self.response_content = "" - def processOLD(self, line): - - if not line or len(line) == 0: - self._fsm.BlankLine() - return self.isRequestFinished() - - matchstr = re.compile("api/response", re.I) - result = matchstr.search(line) - if (result != None): - self._fsm.ApiResponse() - return self.isRequestFinished() - - matchstr = re.compile("Content-Length", re.I) - result = matchstr.search(line) - if (result != None): - # line: Content-Length: 34 - self.content_length = int(line.split(":")[1].strip()) - self._fsm.ContentLength() - return self.isRequestFinished() - - self._fsm.ProcessLine(line) - return self.isRequestFinished() def doNothing(self): # weird smc issue workaround attempt