提交 995f4774 authored 作者: Traun Leyden's avatar Traun Leyden

fixed protocol incompatibility, make more robust and less anal regarding…

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
上级 f01fde03
...@@ -9,8 +9,18 @@ Install ...@@ -9,8 +9,18 @@ Install
See 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 Rebulding State Machines
======================== ========================
(you only need to do this if you changed an .sm file)
for each .sm file: for each .sm file:
java -jar /usr/src/smc/bin/Smc.jar -python -g THE.sm java -jar /usr/src/smc/bin/Smc.jar -python -g THE.sm
......
...@@ -54,20 +54,20 @@ class FreepyDispatcher(LineReceiver): ...@@ -54,20 +54,20 @@ class FreepyDispatcher(LineReceiver):
self.active_request = None # the current active (de-queued) request self.active_request = None # the current active (de-queued) request
def connectionMade(self): def connectionMade(self):
print "Connection made" self.log("Connection made")
self.conncb(self) self.conncb(self)
def connectionLost(self, reason): def connectionLost(self, reason):
if self.discocb: if self.discocb:
self.discocb(reason) self.discocb(reason)
print "connectionLost: %s" % reason self.log("connectionLost: %s" % reason)
def log(self, msg): def log(self, msg):
""" """
print a message to stdout if debug enabled print a message to stdout if debug enabled
""" """
if freepy.globals.DEBUG_ON: if freepy.globals.FREEPY_DEBUG_ON:
print msg print msg
def login(self, passwd): def login(self, passwd):
...@@ -166,7 +166,6 @@ class FreepyDispatcher(LineReceiver): ...@@ -166,7 +166,6 @@ class FreepyDispatcher(LineReceiver):
TODO: add this TODO: add this
""" """
print "confdtmf called"
if bgapi == True: if bgapi == True:
msg = "bgapi conference %s dtmf %s %s" % \ msg = "bgapi conference %s dtmf %s %s" % \
(conf_name, member_id, dtmf) (conf_name, member_id, dtmf)
...@@ -296,7 +295,7 @@ class FreepyDispatcher(LineReceiver): ...@@ -296,7 +295,7 @@ class FreepyDispatcher(LineReceiver):
msg = "api sofia status profile %s as xml" % (profile_name) msg = "api sofia status profile %s as xml" % (profile_name)
req = request.ApiRequest() req = request.ApiRequest()
self.requestq.put(req) self.requestq.put(req)
print "sending to fs: %s" % msg self.log("sending to fs: %s" % msg)
self.transport.write("%s\n\n" % msg) self.transport.write("%s\n\n" % msg)
return req.getDeferred() return req.getDeferred()
...@@ -357,9 +356,15 @@ class FreepyDispatcher(LineReceiver): ...@@ -357,9 +356,15 @@ class FreepyDispatcher(LineReceiver):
def lineReceived(self, line): def lineReceived(self, line):
self.log("<< %s" % line) self.log("<< %s" % line)
if not self.active_request: 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 no active request, dequeue a new one
if self.requestq.empty(): 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 # active request pending. that means that
# there is a bug in the protocol handler # there is a bug in the protocol handler
# (or possibly in fs) # (or possibly in fs)
......
...@@ -25,11 +25,16 @@ ApiResponseStarted ...@@ -25,11 +25,16 @@ ApiResponseStarted
GotReplyText GotReplyText
{ {
BlankLine BlankLine
Startup nil
{ {
setRequestFinished(); callOrErrback();
} }
JobUuid
Startup
{
setRequestFinished(); callOrErrback();
}
} }
...@@ -43,8 +48,7 @@ Default ...@@ -43,8 +48,7 @@ Default
nil nil
{ {
setRequestFinished(); setRequestFinished();
errbackDeferred("Protocol failure - was not expecting blank line"); errbackDeferred("Protocol failure - was not expecting blank line"); }
}
CommandReply CommandReply
nil nil
...@@ -64,7 +68,7 @@ Default ...@@ -64,7 +68,7 @@ Default
nil nil
{ {
setRequestFinished(); 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");
} }
} }
......
...@@ -19,6 +19,9 @@ class BgApiRequestState(statemap.State): ...@@ -19,6 +19,9 @@ class BgApiRequestState(statemap.State):
def CommandReply(self, fsm): def CommandReply(self, fsm):
self.Default(fsm) self.Default(fsm)
def JobUuid(self, fsm):
self.Default(fsm)
def ProcessLine(self, fsm, line): def ProcessLine(self, fsm, line):
self.Default(fsm) self.Default(fsm)
...@@ -82,7 +85,7 @@ class MainMap_Default(BgApiRequestState): ...@@ -82,7 +85,7 @@ class MainMap_Default(BgApiRequestState):
fsm.clearState() fsm.clearState()
try: try:
ctxt.setRequestFinished() 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: finally:
fsm.setState(endState) fsm.setState(endState)
...@@ -109,10 +112,15 @@ class MainMap_ApiResponseStarted(MainMap_Default): ...@@ -109,10 +112,15 @@ class MainMap_ApiResponseStarted(MainMap_Default):
class MainMap_GotReplyText(MainMap_Default): class MainMap_GotReplyText(MainMap_Default):
def BlankLine(self, fsm): def BlankLine(self, fsm):
ctxt = fsm.getOwner()
if fsm.getDebugFlag() == True: if fsm.getDebugFlag() == True:
fsm.getDebugStream().write("TRANSITION : MainMap.GotReplyText.BlankLine()\n") 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.getState().Exit(fsm)
fsm.clearState() fsm.clearState()
try: try:
...@@ -147,6 +155,11 @@ class BgApiRequest_sm(statemap.FSMContext): ...@@ -147,6 +155,11 @@ class BgApiRequest_sm(statemap.FSMContext):
self.getState().CommandReply(self) self.getState().CommandReply(self)
self._transition = None self._transition = None
def JobUuid(self):
self._transition = 'JobUuid'
self.getState().JobUuid(self)
self._transition = None
def ProcessLine(self, *arglist): def ProcessLine(self, *arglist):
self._transition = 'ProcessLine' self._transition = 'ProcessLine'
self.getState().ProcessLine(self, *arglist) self.getState().ProcessLine(self, *arglist)
......
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
...@@ -83,7 +83,7 @@ class FreepyRequest(object): ...@@ -83,7 +83,7 @@ class FreepyRequest(object):
otherwise, if the fs response is incomplete, just buffer the data 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() self._fsm.BlankLine()
return self.isRequestFinished() return self.isRequestFinished()
...@@ -110,6 +110,16 @@ class FreepyRequest(object): ...@@ -110,6 +110,16 @@ class FreepyRequest(object):
self._fsm.ReplyText() self._fsm.ReplyText()
return self.isRequestFinished() 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) matchstr = re.compile("api/response", re.I)
result = matchstr.search(line) result = matchstr.search(line)
...@@ -125,7 +135,6 @@ class FreepyRequest(object): ...@@ -125,7 +135,6 @@ class FreepyRequest(object):
self._fsm.ContentLength() self._fsm.ContentLength()
return self.isRequestFinished() return self.isRequestFinished()
self._fsm.ProcessLine(line) self._fsm.ProcessLine(line)
return self.isRequestFinished() return self.isRequestFinished()
...@@ -194,37 +203,12 @@ class BgApiRequest(FreepyRequest): ...@@ -194,37 +203,12 @@ class BgApiRequest(FreepyRequest):
linereceived: linereceived:
""" """
def __init__(self): def __init__(self):
super(BgApiRequest, self).__init__() super(BgApiRequest, self).__init__()
import bgapirequest_sm import bgapirequest_sm
self._fsm = bgapirequest_sm.BgApiRequest_sm(self) 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): def getResponse(self):
# subclasses may want to parse this into a meaningful # subclasses may want to parse this into a meaningful
...@@ -252,28 +236,6 @@ class ApiRequest(FreepyRequest): ...@@ -252,28 +236,6 @@ class ApiRequest(FreepyRequest):
self._fsm = apirequest_sm.ApiRequest_sm(self) self._fsm = apirequest_sm.ApiRequest_sm(self)
self.response_content = "" 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): def doNothing(self):
# weird smc issue workaround attempt # weird smc issue workaround attempt
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论