提交 bf98f981 authored 作者: Traun Leyden's avatar Traun Leyden

event_socket connection lib for twisted python apps

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@5828 d0543943-73ff-0310-b7d9-9358b9ac24b2
上级 01e2f20b
- Add /path/to/freeswitch/scripts/socket to your PYTHONPATH
- Copy/Paste the code from test1() in fshelper.py to your own test module
- Adapt code as needed and run
\ No newline at end of file
Socket library to interface w/ freeswitch mod_event_socket from Twisted python applications.
差异被折叠。
%start MainMap::Startup
%class ApiRequest
%map MainMap
%%
Startup
{
ApiResponse
ApiResponseStarted
{
}
}
ApiResponseStarted
{
ContentLength
ContentPreStarted
{
}
}
ContentPreStarted
{
BlankLine
ContentStarted
{
}
}
ContentStarted
{
ProcessLine(line)
[ctxt.add_content(line) == True]
Startup
{
setRequestFinished(); callbackDeferred(ctxt.getResponse());
}
ProcessLine(line)
nil
{
// for some reason, have to add doNothing() here or
// importing smc will fail. looks like smc bug.
doNothing();
}
}
Default
{
BlankLine
nil
{
setRequestFinished();
errbackDeferred("Protocol failure");
}
ContentFinished
nil
{
setRequestFinished();
errbackDeferred("Protocol failure");
}
ContentLength
nil
{
setRequestFinished();
errbackDeferred("Protocol failure");
}
ApiResponse
nil
{
setRequestFinished();
errbackDeferred("Protocol failure");
}
ProcessLine(line)
nil
{
setRequestFinished();
errbackDeferred("Protocol failure");
}
}
%%
\ No newline at end of file
# DO NOT MODIFY THIS CODE - AUTOMATICALLY GENERATED BY SMC
import statemap
class ApiRequestState(statemap.State):
def Entry(self, fsm):
pass
def Exit(self, fsm):
pass
def ApiResponse(self, fsm):
self.Default(fsm)
def BlankLine(self, fsm):
self.Default(fsm)
def ContentFinished(self, fsm):
self.Default(fsm)
def ContentLength(self, fsm):
self.Default(fsm)
def ProcessLine(self, fsm, line):
self.Default(fsm)
def Default(self, fsm):
if fsm.getDebugFlag() == True:
fsm.getDebugStream().write('TRANSITION : Default\n')
msg = "\n\tState: %s\n\tTransition: %s" % (
fsm.getState().getName(), fsm.getTransition())
raise TransitionUndefinedException, msg
class MainMap_Default(ApiRequestState):
def BlankLine(self, fsm):
ctxt = fsm.getOwner()
if fsm.getDebugFlag() == True:
fsm.getDebugStream().write("TRANSITION : MainMap.Default.BlankLine()\n")
endState = fsm.getState()
fsm.clearState()
try:
ctxt.setRequestFinished()
ctxt.errbackDeferred("Protocol failure")
finally:
fsm.setState(endState)
def ContentFinished(self, fsm):
ctxt = fsm.getOwner()
if fsm.getDebugFlag() == True:
fsm.getDebugStream().write("TRANSITION : MainMap.Default.ContentFinished()\n")
endState = fsm.getState()
fsm.clearState()
try:
ctxt.setRequestFinished()
ctxt.errbackDeferred("Protocol failure")
finally:
fsm.setState(endState)
def ContentLength(self, fsm):
ctxt = fsm.getOwner()
if fsm.getDebugFlag() == True:
fsm.getDebugStream().write("TRANSITION : MainMap.Default.ContentLength()\n")
endState = fsm.getState()
fsm.clearState()
try:
ctxt.setRequestFinished()
ctxt.errbackDeferred("Protocol failure")
finally:
fsm.setState(endState)
def ApiResponse(self, fsm):
ctxt = fsm.getOwner()
if fsm.getDebugFlag() == True:
fsm.getDebugStream().write("TRANSITION : MainMap.Default.ApiResponse()\n")
endState = fsm.getState()
fsm.clearState()
try:
ctxt.setRequestFinished()
ctxt.errbackDeferred("Protocol failure")
finally:
fsm.setState(endState)
def ProcessLine(self, fsm, line):
ctxt = fsm.getOwner()
if fsm.getDebugFlag() == True:
fsm.getDebugStream().write("TRANSITION : MainMap.Default.ProcessLine(line)\n")
endState = fsm.getState()
fsm.clearState()
try:
ctxt.setRequestFinished()
ctxt.errbackDeferred("Protocol failure")
finally:
fsm.setState(endState)
class MainMap_Startup(MainMap_Default):
def ApiResponse(self, fsm):
if fsm.getDebugFlag() == True:
fsm.getDebugStream().write("TRANSITION : MainMap.Startup.ApiResponse()\n")
fsm.getState().Exit(fsm)
fsm.setState(MainMap.ApiResponseStarted)
fsm.getState().Entry(fsm)
class MainMap_ApiResponseStarted(MainMap_Default):
def ContentLength(self, fsm):
if fsm.getDebugFlag() == True:
fsm.getDebugStream().write("TRANSITION : MainMap.ApiResponseStarted.ContentLength()\n")
fsm.getState().Exit(fsm)
fsm.setState(MainMap.ContentPreStarted)
fsm.getState().Entry(fsm)
class MainMap_ContentPreStarted(MainMap_Default):
def BlankLine(self, fsm):
if fsm.getDebugFlag() == True:
fsm.getDebugStream().write("TRANSITION : MainMap.ContentPreStarted.BlankLine()\n")
fsm.getState().Exit(fsm)
fsm.setState(MainMap.ContentStarted)
fsm.getState().Entry(fsm)
class MainMap_ContentStarted(MainMap_Default):
def ProcessLine(self, fsm, line):
ctxt = fsm.getOwner()
if fsm.getDebugFlag() == True:
fsm.getDebugStream().write("TRANSITION : MainMap.ContentStarted.ProcessLine(line)\n")
if ctxt.add_content(line) == True :
fsm.getState().Exit(fsm)
fsm.clearState()
try:
ctxt.setRequestFinished()
ctxt.callbackDeferred(ctxt.getResponse())
finally:
fsm.setState(MainMap.Startup)
fsm.getState().Entry(fsm)
else:
endState = fsm.getState()
fsm.clearState()
try:
ctxt.doNothing()
finally:
fsm.setState(endState)
class MainMap:
Startup = MainMap_Startup('MainMap.Startup', 0)
ApiResponseStarted = MainMap_ApiResponseStarted('MainMap.ApiResponseStarted', 1)
ContentPreStarted = MainMap_ContentPreStarted('MainMap.ContentPreStarted', 2)
ContentStarted = MainMap_ContentStarted('MainMap.ContentStarted', 3)
Default = MainMap_Default('MainMap.Default', -1)
class ApiRequest_sm(statemap.FSMContext):
def __init__(self, owner):
statemap.FSMContext.__init__(self)
self._owner = owner
self.setState(MainMap.Startup)
MainMap.Startup.Entry(self)
def ApiResponse(self):
self._transition = 'ApiResponse'
self.getState().ApiResponse(self)
self._transition = None
def BlankLine(self):
self._transition = 'BlankLine'
self.getState().BlankLine(self)
self._transition = None
def ContentFinished(self):
self._transition = 'ContentFinished'
self.getState().ContentFinished(self)
self._transition = None
def ContentLength(self):
self._transition = 'ContentLength'
self.getState().ContentLength(self)
self._transition = None
def ProcessLine(self, *arglist):
self._transition = 'ProcessLine'
self.getState().ProcessLine(self, *arglist)
self._transition = None
def getState(self):
if self._state == None:
raise statemap.StateUndefinedException
return self._state
def getOwner(self):
return self._owner
%start MainMap::Startup
%class BgApiRequest
%map MainMap
%%
Startup
{
CommandReply
ApiResponseStarted
{
}
}
ApiResponseStarted
{
ReplyText
GotReplyText
{
}
}
GotReplyText
{
BlankLine
Startup
{
setRequestFinished(); callOrErrback();
}
}
Default
{
BlankLine
nil
{
setRequestFinished();
errbackDeferred("Protocol failure");
}
CommandReply
nil
{
setRequestFinished();
errbackDeferred("Protocol failure");
}
ReplyText
nil
{
setRequestFinished();
errbackDeferred("Protocol failure");
}
ProcessLine(line)
nil
{
setRequestFinished();
errbackDeferred("Protocol failure");
}
}
%%
\ No newline at end of file
# DO NOT MODIFY THIS CODE - AUTOMATICALLY GENERATED BY SMC
import statemap
class BgApiRequestState(statemap.State):
def Entry(self, fsm):
pass
def Exit(self, fsm):
pass
def BlankLine(self, fsm):
self.Default(fsm)
def CommandReply(self, fsm):
self.Default(fsm)
def ProcessLine(self, fsm, line):
self.Default(fsm)
def ReplyText(self, fsm):
self.Default(fsm)
def Default(self, fsm):
if fsm.getDebugFlag() == True:
fsm.getDebugStream().write('TRANSITION : Default\n')
msg = "\n\tState: %s\n\tTransition: %s" % (
fsm.getState().getName(), fsm.getTransition())
raise TransitionUndefinedException, msg
class MainMap_Default(BgApiRequestState):
def BlankLine(self, fsm):
ctxt = fsm.getOwner()
if fsm.getDebugFlag() == True:
fsm.getDebugStream().write("TRANSITION : MainMap.Default.BlankLine()\n")
endState = fsm.getState()
fsm.clearState()
try:
ctxt.setRequestFinished()
ctxt.errbackDeferred("Protocol failure")
finally:
fsm.setState(endState)
def CommandReply(self, fsm):
ctxt = fsm.getOwner()
if fsm.getDebugFlag() == True:
fsm.getDebugStream().write("TRANSITION : MainMap.Default.CommandReply()\n")
endState = fsm.getState()
fsm.clearState()
try:
ctxt.setRequestFinished()
ctxt.errbackDeferred("Protocol failure")
finally:
fsm.setState(endState)
def ReplyText(self, fsm):
ctxt = fsm.getOwner()
if fsm.getDebugFlag() == True:
fsm.getDebugStream().write("TRANSITION : MainMap.Default.ReplyText()\n")
endState = fsm.getState()
fsm.clearState()
try:
ctxt.setRequestFinished()
ctxt.errbackDeferred("Protocol failure")
finally:
fsm.setState(endState)
def ProcessLine(self, fsm, line):
ctxt = fsm.getOwner()
if fsm.getDebugFlag() == True:
fsm.getDebugStream().write("TRANSITION : MainMap.Default.ProcessLine(line)\n")
endState = fsm.getState()
fsm.clearState()
try:
ctxt.setRequestFinished()
ctxt.errbackDeferred("Protocol failure")
finally:
fsm.setState(endState)
class MainMap_Startup(MainMap_Default):
def CommandReply(self, fsm):
if fsm.getDebugFlag() == True:
fsm.getDebugStream().write("TRANSITION : MainMap.Startup.CommandReply()\n")
fsm.getState().Exit(fsm)
fsm.setState(MainMap.ApiResponseStarted)
fsm.getState().Entry(fsm)
class MainMap_ApiResponseStarted(MainMap_Default):
def ReplyText(self, fsm):
if fsm.getDebugFlag() == True:
fsm.getDebugStream().write("TRANSITION : MainMap.ApiResponseStarted.ReplyText()\n")
fsm.getState().Exit(fsm)
fsm.setState(MainMap.GotReplyText)
fsm.getState().Entry(fsm)
class MainMap_GotReplyText(MainMap_Default):
def BlankLine(self, fsm):
ctxt = fsm.getOwner()
if fsm.getDebugFlag() == True:
fsm.getDebugStream().write("TRANSITION : MainMap.GotReplyText.BlankLine()\n")
fsm.getState().Exit(fsm)
fsm.clearState()
try:
ctxt.setRequestFinished()
ctxt.callOrErrback()
finally:
fsm.setState(MainMap.Startup)
fsm.getState().Entry(fsm)
class MainMap:
Startup = MainMap_Startup('MainMap.Startup', 0)
ApiResponseStarted = MainMap_ApiResponseStarted('MainMap.ApiResponseStarted', 1)
GotReplyText = MainMap_GotReplyText('MainMap.GotReplyText', 2)
Default = MainMap_Default('MainMap.Default', -1)
class BgApiRequest_sm(statemap.FSMContext):
def __init__(self, owner):
statemap.FSMContext.__init__(self)
self._owner = owner
self.setState(MainMap.Startup)
MainMap.Startup.Entry(self)
def BlankLine(self):
self._transition = 'BlankLine'
self.getState().BlankLine(self)
self._transition = None
def CommandReply(self):
self._transition = 'CommandReply'
self.getState().CommandReply(self)
self._transition = None
def ProcessLine(self, *arglist):
self._transition = 'ProcessLine'
self.getState().ProcessLine(self, *arglist)
self._transition = None
def ReplyText(self):
self._transition = 'ReplyText'
self.getState().ReplyText(self)
self._transition = None
def getState(self):
if self._state == None:
raise statemap.StateUndefinedException
return self._state
def getOwner(self):
return self._owner
"""
FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
Copyright (C) 2005/2006, Anthony Minessale II <anthmct@yahoo.com>
Version: MPL 1.1
The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the
License.
The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
The Initial Developer of the Original Code is
Anthony Minessale II <anthmct@yahoo.com>
Portions created by the Initial Developer are Copyright (C)
the Initial Developer. All Rights Reserved.
Contributor(s): Traun Leyden <tleyden@branchcut.com>
"""
import sys
from twisted.internet import reactor, defer
from twisted.protocols.basic import LineReceiver
from twisted.internet.protocol import Protocol, ClientFactory
from twisted.python import failure
import time, re
from time import strftime
from Queue import Queue
from freepy import request
"""
This class connects to freeswitch and listens for
events and calls callback with the events.
Example messages
=================
Content-Length: 675
Content-Type: text/event-xml
<event>
<header name="force-contact" value="nat-connectile-dysfunction"></header>
etc..
</event>
Content-Length: 875
Content-Type: text/event-xml
<event>
...
</event>
"""
class FreeswitchEventListener(LineReceiver):
def __init__(self, conncb, discocb=None):
self.delimiter='\n' # parent class uses this
self.conncb=conncb
self.discocb=discocb
self.bufferlines = []
self.receiving_event = False # state to track if in <event>..</event>
self.requestq = Queue() # queue of pending requests
self.active_request = None # the current active (de-queued) request
def connectionMade(self):
self.conncb(self)
def connectionLost(self, reason):
if self.discocb:
self.discocb(reason)
print "connectionLost: %s" % reason
def login(self, passwd):
"""
send login request
"""
msg = "auth %s" % passwd
req = request.LoginRequest()
self.active_request = req
self.transport.write("%s\n\n" % msg)
return req.getDeferred()
def sniff_events(self, output_type, events):
"""
@param output_type - eg, xml or plain
@param events - list of events, eg ['all']
"""
event_list = " ".join(events)
msg = "event %s %s" % (output_type, event_list)
self.transport.write("%s\n\n" % msg)
def sniff_custom_events(self, output_type, events):
"""
when sniffing custom events, the CUSTOM keyword
must be present in message
http://wiki.freeswitch.org/wiki/Event_Socket#event
@param output_type - eg, xml or plain
@param events - list of events, eg ['all']
"""
event_list = " ".join(events)
msg = "event %s CUSTOM %s" % (output_type, event_list)
self.transport.write("%s\n\n" % msg)
def sniff_all_events(self, output_type):
"""
@param output_type - eg, xml or plain
"""
msg = "event %s all" % output_type
self.transport.write("%s\n\n" % msg)
def lineReceived(self, line):
if not self.active_request:
if line.find("<event>") != -1:
self.receiving_event = True
if self.receiving_event:
self.bufferlines.append(line)
if line.find("</event>") != -1:
event_xml_str = "\n".join(self.bufferlines)
self.eventReceived(event_xml_str)
self.bufferlines = []
self.receiving_event = False
else:
# we have an active request (seperate state machine)
# tell the request to process the line, and tell us
# if its finished or not. if its finished, we remove it
# as the active request so that a new active request will
# be de-queued.
finished = self.active_request.process(line)
if finished == True:
self.active_request = None
def eventReceived(self, event_xml_str):
"""
should be overridden by subclasses
"""
raise Exception("This is an abstract class, should be overridden "
"in a subclass")
class FreeswitchEventListenerFactory(ClientFactory):
def __init__(self, protoclass, host=None, passwd=None, port=None):
"""
@param protoclass - a class (not instance) of the protocol
should be a subclass of a FreeswitchEventListener
"""
self.protoclass=protoclass
if host:
self.host = host
if passwd:
self.passwd = passwd
if port:
self.port = port
self.protocol = None
self.connection_deferred = None
self.num_attempts = 0
def reset(self):
self.protocol = None
self.connection_deferred = None
def connect(self):
if self.protocol:
# if we have a protocol object, we are connected (since we always
# null it upon any disconnection)
return defer.succeed(self.protocol)
#if self.connection_deferred:
# we are already connecting, return existing dfrd
# return self.connection_deferred
# connect and automatically login after connection
if not self.connection_deferred:
self.connection_deferred = defer.Deferred()
self.connection_deferred.addCallback(self.dologin)
self.connection_deferred.addErrback(self.generalError)
reactor.connectTCP(self.host, self.port, self)
return self.connection_deferred
def conncb(self, protocol):
self.protocol = protocol
deferred2callback = self.connection_deferred
self.connection_deferred = None
deferred2callback.callback(self.protocol)
def generalError(self, failure):
print "General error: %s" % failure
return failure
def startedConnecting(self, connector):
pass
def buildProtocol(self, addr):
return self.protoclass(self.conncb, self.discocb)
def clientConnectionLost(self, connector, reason):
print "clientConnectionLost! conn=%s, reason=%s" % (connector,
reason)
self.connection_deferred = None
self.protocol = None
def clientConnectionFailed(self, connector, reason):
print "clientConnectionFailed! conn=%s, reason=%s" % (connector,
reason)
#self.protocol = None
if self.num_attempts < 100:
self.num_attempts += 1
return reactor.callLater(5, self.connect)
else:
deferred2callback = self.connection_deferred
deferred2callback.errback(reason)
def discocb(self, reason):
print "disconnected. reason: %s" % reason
self.protocol = None
def dologin(self, connectmsg):
return self.protocol.login(self.passwd)
def test1():
fel = FreeswitchEventListener
factory = FreeswitchEventListenerFactory(protoclass=fel,
host="127.0.0.1",
port=8021,
passwd="ClueCon")
def connected(result):
print "We connected, result: %s" % result
events=['sofia::register','sofia::expire']
factory.protocol.sniff_custom_events(output_type="xml", events=events)
#factory.protocol.sniff_all_events(output_type="xml")
def failure(failure):
print "Failed to connect: %s" % failure
d = factory.connect()
d.addCallbacks(connected, failure)
d.addErrback(failure)
reactor.run()
if __name__=="__main__":
test1()
差异被折叠。
%start MainMap::Startup
%class LoginRequest
%map MainMap
%%
Startup
{
AuthRequest
AuthRequestStarted
{
}
}
AuthRequestStarted
{
BlankLine
AuthRequestFinished
{
}
}
AuthRequestFinished
{
CommandReply
CommandReplyStarted
{
}
}
CommandReplyStarted
{
ReplyText
GotReplyText
{
}
}
GotReplyText
{
BlankLine
Startup
{
setRequestFinished(); callOrErrback();
}
}
Default
{
BlankLine
nil
{
setRequestFinished();
errbackDeferred("Protocol failure");
}
AuthRequest
nil
{
setRequestFinished();
errbackDeferred("Protocol failure");
}
CommandReply
nil
{
setRequestFinished();
errbackDeferred("Protocol failure");
}
ReplyText
nil
{
setRequestFinished();
errbackDeferred("Protocol failure");
}
ProcessLine(line)
nil
{
setRequestFinished();
errbackDeferred("Protocol failure");
}
}
%%
\ No newline at end of file
# DO NOT MODIFY THIS CODE - AUTOMATICALLY GENERATED BY SMC
import statemap
class LoginRequestState(statemap.State):
def Entry(self, fsm):
pass
def Exit(self, fsm):
pass
def AuthRequest(self, fsm):
self.Default(fsm)
def BlankLine(self, fsm):
self.Default(fsm)
def CommandReply(self, fsm):
self.Default(fsm)
def ProcessLine(self, fsm, line):
self.Default(fsm)
def ReplyText(self, fsm):
self.Default(fsm)
def Default(self, fsm):
if fsm.getDebugFlag() == True:
fsm.getDebugStream().write('TRANSITION : Default\n')
msg = "\n\tState: %s\n\tTransition: %s" % (
fsm.getState().getName(), fsm.getTransition())
raise TransitionUndefinedException, msg
class MainMap_Default(LoginRequestState):
def BlankLine(self, fsm):
ctxt = fsm.getOwner()
if fsm.getDebugFlag() == True:
fsm.getDebugStream().write("TRANSITION : MainMap.Default.BlankLine()\n")
endState = fsm.getState()
fsm.clearState()
try:
ctxt.setRequestFinished()
ctxt.errbackDeferred("Protocol failure")
finally:
fsm.setState(endState)
def AuthRequest(self, fsm):
ctxt = fsm.getOwner()
if fsm.getDebugFlag() == True:
fsm.getDebugStream().write("TRANSITION : MainMap.Default.AuthRequest()\n")
endState = fsm.getState()
fsm.clearState()
try:
ctxt.setRequestFinished()
ctxt.errbackDeferred("Protocol failure")
finally:
fsm.setState(endState)
def CommandReply(self, fsm):
ctxt = fsm.getOwner()
if fsm.getDebugFlag() == True:
fsm.getDebugStream().write("TRANSITION : MainMap.Default.CommandReply()\n")
endState = fsm.getState()
fsm.clearState()
try:
ctxt.setRequestFinished()
ctxt.errbackDeferred("Protocol failure")
finally:
fsm.setState(endState)
def ReplyText(self, fsm):
ctxt = fsm.getOwner()
if fsm.getDebugFlag() == True:
fsm.getDebugStream().write("TRANSITION : MainMap.Default.ReplyText()\n")
endState = fsm.getState()
fsm.clearState()
try:
ctxt.setRequestFinished()
ctxt.errbackDeferred("Protocol failure")
finally:
fsm.setState(endState)
def ProcessLine(self, fsm, line):
ctxt = fsm.getOwner()
if fsm.getDebugFlag() == True:
fsm.getDebugStream().write("TRANSITION : MainMap.Default.ProcessLine(line)\n")
endState = fsm.getState()
fsm.clearState()
try:
ctxt.setRequestFinished()
ctxt.errbackDeferred("Protocol failure")
finally:
fsm.setState(endState)
class MainMap_Startup(MainMap_Default):
def AuthRequest(self, fsm):
if fsm.getDebugFlag() == True:
fsm.getDebugStream().write("TRANSITION : MainMap.Startup.AuthRequest()\n")
fsm.getState().Exit(fsm)
fsm.setState(MainMap.AuthRequestStarted)
fsm.getState().Entry(fsm)
class MainMap_AuthRequestStarted(MainMap_Default):
def BlankLine(self, fsm):
if fsm.getDebugFlag() == True:
fsm.getDebugStream().write("TRANSITION : MainMap.AuthRequestStarted.BlankLine()\n")
fsm.getState().Exit(fsm)
fsm.setState(MainMap.AuthRequestFinished)
fsm.getState().Entry(fsm)
class MainMap_AuthRequestFinished(MainMap_Default):
def CommandReply(self, fsm):
if fsm.getDebugFlag() == True:
fsm.getDebugStream().write("TRANSITION : MainMap.AuthRequestFinished.CommandReply()\n")
fsm.getState().Exit(fsm)
fsm.setState(MainMap.CommandReplyStarted)
fsm.getState().Entry(fsm)
class MainMap_CommandReplyStarted(MainMap_Default):
def ReplyText(self, fsm):
if fsm.getDebugFlag() == True:
fsm.getDebugStream().write("TRANSITION : MainMap.CommandReplyStarted.ReplyText()\n")
fsm.getState().Exit(fsm)
fsm.setState(MainMap.GotReplyText)
fsm.getState().Entry(fsm)
class MainMap_GotReplyText(MainMap_Default):
def BlankLine(self, fsm):
ctxt = fsm.getOwner()
if fsm.getDebugFlag() == True:
fsm.getDebugStream().write("TRANSITION : MainMap.GotReplyText.BlankLine()\n")
fsm.getState().Exit(fsm)
fsm.clearState()
try:
ctxt.setRequestFinished()
ctxt.callOrErrback()
finally:
fsm.setState(MainMap.Startup)
fsm.getState().Entry(fsm)
class MainMap:
Startup = MainMap_Startup('MainMap.Startup', 0)
AuthRequestStarted = MainMap_AuthRequestStarted('MainMap.AuthRequestStarted', 1)
AuthRequestFinished = MainMap_AuthRequestFinished('MainMap.AuthRequestFinished', 2)
CommandReplyStarted = MainMap_CommandReplyStarted('MainMap.CommandReplyStarted', 3)
GotReplyText = MainMap_GotReplyText('MainMap.GotReplyText', 4)
Default = MainMap_Default('MainMap.Default', -1)
class LoginRequest_sm(statemap.FSMContext):
def __init__(self, owner):
statemap.FSMContext.__init__(self)
self._owner = owner
self.setState(MainMap.Startup)
MainMap.Startup.Entry(self)
def AuthRequest(self):
self._transition = 'AuthRequest'
self.getState().AuthRequest(self)
self._transition = None
def BlankLine(self):
self._transition = 'BlankLine'
self.getState().BlankLine(self)
self._transition = None
def CommandReply(self):
self._transition = 'CommandReply'
self.getState().CommandReply(self)
self._transition = None
def ProcessLine(self, *arglist):
self._transition = 'ProcessLine'
self.getState().ProcessLine(self, *arglist)
self._transition = None
def ReplyText(self):
self._transition = 'ReplyText'
self.getState().ReplyText(self)
self._transition = None
def getState(self):
if self._state == None:
raise statemap.StateUndefinedException
return self._state
def getOwner(self):
return self._owner
"""
FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
Copyright (C) 2005/2006, Anthony Minessale II <anthmct@yahoo.com>
Version: MPL 1.1
The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the
License.
The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
The Initial Developer of the Original Code is
Anthony Minessale II <anthmct@yahoo.com>
Portions created by the Initial Developer are Copyright (C)
the Initial Developer. All Rights Reserved.
Contributor(s): Traun Leyden <tleyden@branchcut.com>
"""
"""
Data models for objects inside freeswitch
"""
import re
class ConfMember:
def __init__(self, rawstring):
self.rawstring = rawstring
self.member_id = None
self.member_uri = None
self.uuid = None
self.caller_id_name = None
self.caller_id_number = None
self.flags = None
self.volume_in = None
self.volume_out = None
self.energy_level = None
self.parse(self.rawstring)
def parse(self, rawstring):
"""
1;sofia/mydomain.com/user@somewhere.com;898e6552-24ab-11dc-9df7-9fccd4095451;FreeSWITCH;0000000000;hear|speak;0;0;300
"""
fields = rawstring.split(";")
self.member_id = fields[0]
self.member_uri = fields[1]
self.uuid = fields[2]
self.caller_id_name = fields[3]
self.caller_id_number = fields[4]
self.flags = fields[5]
self.volume_in = fields[6]
self.volume_out = fields[7]
self.energy_level = fields[8]
def brief_member_uri(self):
"""
if self.member_uri is sofia/mydomain.com/foo@bar.com
return foo@bar.com
"""
if not self.member_uri:
return None
if self.member_uri.find("/") == -1:
return self.member_uri
r = self.member_uri.split("/")[-1] # tokenize on "/" and return last item
return r
def __repr__(self):
return self.__str__()
def __str__(self):
return "%s (%s)" % (self.member_id, self.member_uri)
差异被折叠。
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论