add interactive shell to stat more calls from mncc_test2.py

This commit is contained in:
Harald Welte 2015-12-01 21:39:08 +01:00
parent b3b64c0ce2
commit 7a76ec60a8
1 changed files with 25 additions and 8 deletions

View File

@ -2,11 +2,13 @@
from gsm_call_fsm import GsmCallFsm, GsmCallConnector from gsm_call_fsm import GsmCallFsm, GsmCallConnector
from mncc_sock import MnccSocket from mncc_sock import MnccSocket
from thread import start_new_thread
import pykka import pykka
import logging import logging
import signal import signal, sys
import sys import readline, code
# MnccActor provides an interface for GsmCallFsm to send MNCC messages
class MnccActor(pykka.ThreadingActor): class MnccActor(pykka.ThreadingActor):
def __init__(self, mncc_sock): def __init__(self, mncc_sock):
super(MnccActor, self).__init__() super(MnccActor, self).__init__()
@ -17,7 +19,15 @@ class MnccActor(pykka.ThreadingActor):
if message['type'] == 'send': if message['type'] == 'send':
mncc_sock.send(message['msg']) mncc_sock.send(message['msg'])
# MNCC receive thread, broadcasting received MNCC packets to GsmCallFsm
def mncc_rx_thread(mncc_sock):
while 1:
msg = mncc_sock.recv()
print "Received %s from MNCC, broadcasting to Call FSMs" % msg
# we simply broadcast to all calls
pykka.ActorRegistry.broadcast({'type': 'mncc', 'msg': msg}, GsmCallFsm)
# capture SIGINT (Ctrl+C) and stop all actors
def sigint_handler(signum, frame): def sigint_handler(signum, frame):
pykka.ActorRegistry.stop_all() pykka.ActorRegistry.stop_all()
sys.exit(0) sys.exit(0)
@ -26,15 +36,22 @@ logging.basicConfig(level=logging.DEBUG)
signal.signal(signal.SIGINT, sigint_handler) signal.signal(signal.SIGINT, sigint_handler)
# start the MnccSocket and associated pykka actor + rx thread
mncc_sock = MnccSocket() mncc_sock = MnccSocket()
mncc_act = MnccActor.start(mncc_sock) mncc_act = MnccActor.start(mncc_sock)
start_new_thread(mncc_rx_thread, (mncc_sock,))
# start a first bogus call
call_conn = GsmCallConnector.start(mncc_act).proxy() call_conn = GsmCallConnector.start(mncc_act).proxy()
call_conn.start_call_ab("1234", "6789") call_conn.start_call_ab("1234", "6789")
while 1: # start a shell to enable the user to add more calls as needed
msg = mncc_sock.recv() vars = globals().copy()
print "Received %s from MNCC, broadcasting to Call FSMs" % msg vars.update(locals())
# FIXME: we simply broadcast to all calls shell = code.InteractiveConsole(vars)
pykka.ActorRegistry.broadcast({'type': 'mncc', 'msg': msg}, GsmCallFsm) try:
#pykka.ActorRegistry.broadcast({'type': 'mncc', 'msg': msg}) shell.interact()
except SystemExit:
pass
# clan up after user leaves interactive shell
sigint_handler(signal.SIGINT, None)