fixed protocol incompatibility, make more robust and less anal regarding spurious blank lines, debugging instructions added to README. debug off by default

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@9009 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
Traun Leyden 2008-07-13 04:10:52 +00:00
parent f01fde034c
commit 995f477475
6 changed files with 65 additions and 63 deletions

View File

@ -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

View File

@ -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)

View File

@ -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");
}
}

View File

@ -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)

View File

@ -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

View File

@ -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