[asterisk-commits] mjordan: branch mjordan/longcat r378191 - /team/mjordan/longcat/test_script.py

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri Dec 21 13:08:04 CST 2012


Author: mjordan
Date: Fri Dec 21 13:08:00 2012
New Revision: 378191

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=378191
Log:
Add a quickie test script for AsyncAGI

Let's me play back monkeys incessantly to a signle channel. And may eventually
let me replay monkeys over, and over, and over, and over, and over, and rewind,
and again, and again, and all work and no play makes Jacky a dull boy

Added:
    team/mjordan/longcat/test_script.py   (with props)

Added: team/mjordan/longcat/test_script.py
URL: http://svnview.digium.com/svn/asterisk/team/mjordan/longcat/test_script.py?view=auto&rev=378191
==============================================================================
--- team/mjordan/longcat/test_script.py (added)
+++ team/mjordan/longcat/test_script.py Fri Dec 21 13:08:00 2012
@@ -1,0 +1,226 @@
+#!/usr/bin/env python
+
+from twisted.internet import stdio, reactor, protocol
+from twisted.protocols import basic
+from starpy import manager
+import re
+import sys
+import optparse
+
+AMI = None
+SERVER = None
+USER = ''
+PASSWORD = ''
+HOST = ''
+PORT = ''
+
+CONSOLE_PORT = 5959
+
+class DataForwardingProtocol(protocol.Protocol):
+	def __init__(self):
+		self.output = None
+		self.normalize_new_lines = False
+
+	def dataReceived(self, data):
+		if self.normalize_new_lines:
+			data = re.sub(r"(\r\n|\n)", "\r\n", data)
+		if self.output:
+			self.output.write(data)
+
+class StdioProxyProtocol(DataForwardingProtocol):
+	def connectionMade(self):
+		input_forwarder = DataForwardingProtocol()
+		input_forwarder.output = self.transport
+		input_forwarder.normalize_new_lines = True
+		stdio_wrapper = stdio.StandardIO(input_forwarder)
+		self.output = stdio_wrapper
+		print 'Connected to server. Press Ctrl+C to close connection.'
+
+class StdioProxyFactory(protocol.ClientFactory):
+	protocol = StdioProxyProtocol
+
+	def clientConnectionLost(self, transport, reason):
+		if reactor.running:
+			reactor.stop()
+
+	def clientConnectionFailed(self, transport, reason):
+		print reason.getErrorMessage()
+		if reactor.running:
+			reactor.stop()
+
+class ServerProtocol(basic.LineOnlyReceiver):
+
+	def connectionMade(self):
+		print 'New Connection Received'
+
+	def connectionLost(self, reason):
+		print "Connection Terminated: %s" % str(reason)
+		if reactor.running:
+			reactor.stop()
+
+	def lineReceived(self, line):
+		''' All the magic happens here '''
+		tokens = line.split(' ')
+		command = tokens[0].lower()
+		message = {}
+		SERVER.send_message('Attempting...')
+		if (command == 'playback'):
+			if len(tokens) == 1:
+				SERVER.send_message('playback needs an arg buddy')
+				return
+			type = tokens[1].lower()
+			if type == 'file':
+				if len(tokens) == 2:
+					SERVER.send_message('What file did you want?')
+					return
+				file = tokens[2].lower()
+				message['action'] = 'AGI'
+				message['channel'] = CHANNEL
+				message['command'] = 'STREAM FILE %s ""' % file
+				AMI.sendMessage(message)
+		elif (command == 'reverse'):
+			message['action'] = 'AGI'
+			message['channel'] = CHANNEL
+			message['command'] = 'ASYNCAGI CONTROL STREAM REVERSE'
+			AMI.sendMessage(message)
+
+		elif (command == 'restart'):
+			message['action'] = 'AGI'
+			message['channel'] = CHANNEL
+			message['command'] = 'ASYNCAGI CONTROL STREAM RESTART'
+			AMI.sendMessage(message)
+
+		elif (command == 'forward'):
+			message['action'] = 'AGI'
+			message['channel'] = CHANNEL
+			message['command'] = 'ASYNCAGI CONTROL STREAM FORWARD'
+			AMI.sendMessage(message)
+
+		elif (command == 'stop'):
+			message['action'] = 'AGI'
+			message['channel'] = CHANNEL
+			message['command'] = 'ASYNCAGI CONTROL STREAM STOP'
+			AMI.sendMessage(message)
+		
+		elif (command == 'pause'):
+			message['action'] = 'AGI'
+			message['channel'] = CHANNEL
+			message['command'] = 'ASYNCAGI CONTROL STREAM PAUSE'
+			AMI.sendMessage(message)
+		else:
+			SERVER.send_message('I do not know that one.')
+		return
+
+	def send_message(self, message):
+		self.sendLine(message)
+
+class ServerFactory(protocol.Factory):
+
+	def __init__(self, entry_function):
+		self.mainFunction = entry_function
+
+	def buildProtocol(self, addr):
+		p = ServerProtocol()
+		p.factory = self
+		global SERVER
+		SERVER = p
+		return p
+
+
+
+def dump_event(event):
+	SERVER.send_message('Received AMI event: %s' % event['event'])
+	for k, v in event.items():
+		if k != 'event':
+			SERVER.send_message('%s: %s' % (k, v))
+	SERVER.send_message('\n')
+	return
+
+def newchannel_handler(ami, event):
+	dump_event(event)
+	return
+
+def newexten_handler(ami, event):
+	dump_event(event)
+	return
+
+def varset_handler(ami, event):
+	#dump_event(event)
+	return
+
+def hangup_handler(ami, event):
+	dump_event(event)
+	return
+
+def asyncagi_handler(ami, event):
+	dump_event(event)
+	global CHANNEL
+	CHANNEL = event['channel']
+	SERVER.send_message('Tracking channel %s' % CHANNEL)
+	return
+
+def login_success(ami):
+	global AMI
+	AMI = ami
+	SERVER.send_message('AMI login successful')
+
+	# Okay, now we can do more things
+	ami.registerEvent('Newchannel', newchannel_handler)
+	ami.registerEvent('Newexten', newexten_handler)
+	ami.registerEvent('VarSet', varset_handler)
+	ami.registerEvent('Hangup', hangup_handler)	
+	ami.registerEvent('AsyncAGI', asyncagi_handler)
+	returni
+
+def login_error(reason):
+	SERVER.send_message('AMI login error: %s' % reason)
+	SERVER.send_message('Bailing out, sorry...')
+	return
+
+def login():
+	SERVER.send_message('Connecting as %s, %s to %s, %s' % (USER, PASSWORD, HOST, PORT))
+	ami_factory = manager.AMIFactory(USER, PASSWORD)
+	ami_factory.login(HOST, int(PORT)).addCallbacks(login_success, login_error)
+	return
+
+def entry_point():
+	SERVER.send_message('Hey, we entered in!')
+	return
+
+def main(argv=None):
+	global USER
+	global PASSWORD
+	global HOST
+	global PORT
+
+	if argv is None:
+		args = sys.argv
+
+	parser = optparse.OptionParser(usage='test_script.py [options]')
+	parser.add_option('-u', '--user', dest='user', default='mjordan',
+		help='The user for the AMI connection')
+	parser.add_option('-p', '--password', dest='password', default='secret',
+		help='The password for the AMI connection')
+	parser.add_option('-s', '--server', dest='host', default='127.0.0.1',
+		help='The server to connect to')
+	parser.add_option('-o', '--port', dest='port', default='5038',
+		help='The port to connect on')
+	(options, args) = parser.parse_args(argv)
+
+	USER = options.user
+	PASSWORD = options.password
+	HOST = options.host
+	PORT = options.port
+
+	server_factory = ServerFactory(entry_point)
+	reactor.listenTCP(CONSOLE_PORT, server_factory, 100)
+	reactor.connectTCP('localhost', CONSOLE_PORT, StdioProxyFactory())
+	reactor.callLater(1, login)
+	reactor.run()
+
+	return 0
+
+if __name__ == "__main__":
+	sys.exit(main() or 0)
+
+

Propchange: team/mjordan/longcat/test_script.py
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/mjordan/longcat/test_script.py
------------------------------------------------------------------------------
    svn:executable = *

Propchange: team/mjordan/longcat/test_script.py
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/mjordan/longcat/test_script.py
------------------------------------------------------------------------------
    svn:mime-type = text/plain




More information about the asterisk-commits mailing list