[PATCH] openbsc[master]: Add twisted-based IPA multiplex

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Max gerrit-no-reply at lists.osmocom.org
Thu Nov 17 12:37:24 UTC 2016


Review at  https://gerrit.osmocom.org/1268

Add twisted-based IPA multiplex

Add sample applications using twisted framework for IPA and CTRL
multiplex.

Change-Id: I07559df420b7fe8418f3412f45acd9a375e43bc5
Related: SYS#3028
---
A openbsc/contrib/twisted_ipa.py
1 file changed, 171 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/openbsc refs/changes/68/1268/1

diff --git a/openbsc/contrib/twisted_ipa.py b/openbsc/contrib/twisted_ipa.py
new file mode 100755
index 0000000..1c6dcdd
--- /dev/null
+++ b/openbsc/contrib/twisted_ipa.py
@@ -0,0 +1,171 @@
+#!/usr/bin/python3
+
+"""
+/*
+ * Copyright (C) 2016 sysmocom s.f.m.c. GmbH
+ *
+ * All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+"""
+
+from ipa import Ctrl, IPA
+from twisted.internet.protocol import ReconnectingClientFactory
+from twisted.internet import reactor, protocol
+from twisted.protocols import basic
+
+
+class IPACommon(basic.Int16StringReceiver):
+    """
+    Generic IPA protocol handler: include some routines for simpler subprotocols
+    It's not intended as full implementation of all subprotocols, rather common ground and example code
+    """
+    def ack(self):
+        self.transport.write(IPA().id_ack())
+
+    def ping(self):
+        self.transport.write(IPA().ping())
+
+    def pong(self):
+        self.transport.write(IPA().pong())
+
+    def handle_CTRL(self, data):
+        """
+        For basic tests only - this should be replaced with proper handling routine: see CtrlServer for example
+        """
+        if self.factory.debug:
+            print('ctrl received %s::%s' % Ctrl().parse(data.decode('utf-8')))
+
+    def handle_OSMO(self, d, ex):
+        """
+        Dispatcher point for OSMO subprotocols: OAP, GSUP etc handling should be added here
+        """
+        if ex == IPA.EXT['CTRL']:
+            self.handle_CTRL(d)
+
+    def handle_CCM(self, data, msgt):
+        """
+        CCM (IPA Connection Management) - only basic logic necessary for tests is implemented
+        """
+        if msgt == IPA.MSGT['ID_GET']:
+            self.transport.getHandle().sendall(IPA().id_resp(self.factory.test_id))
+            # if we call
+            # self.transport.write(IPA().id_resp(self.factory.test_id))
+            # instead, than we would have to also call
+            # reactor.callLater(1, self.ack)
+            # instead of self.ack()
+            # otherwise the writes will be glued together - hence the necessity for ugly hack with 1s timeout
+            # Note: this still might work depending on the IPA implementation details on the other side
+            self.ack()
+            # schedule PING in 4s
+            reactor.callLater(4, self.ping)
+        if msgt == IPA.MSGT['PING']:
+            self.pong()
+
+    def dataReceived(self, data):
+        """
+        Generic message dispatcher for IPA (sub)protocols
+        """
+        (_, pr, ex, d) = IPA().del_header(data)
+        if self.factory.debug:
+            print('IPA received %s::%s %s' % (IPA().proto(pr), IPA().ext_name(pr, ex), d))
+        if pr == IPA.PROTO['CCM']:
+            self.handle_CCM(d, ex)
+        if pr == IPA.PROTO['OSMO']:
+            self.handle_OSMO(d, ex)
+
+    def connectionMade(self):
+        if self.factory.debug:
+            print('IPA connection made!')
+        self.factory.resetDelay() # We have to resetDelay() here to drop internal state to default values to make reconnection logic work
+
+
+class IPAServer(IPACommon):
+    """
+    Test implementation of IPA server
+    Demonstrate CCM opearation by overriding necessary bits from IPACommon
+    """
+    def connectionMade(self):
+        print('IPA server connection made!')
+        super(IPAServer, self).connectionMade() # keep reconnection logic working
+        self.transport.write(IPA().id_get())
+
+
+class CtrlServer(IPACommon):
+    """
+    Test implementation of CTRL server
+    Demonstarte CTRL handling by overriding simpler routines from IPACommon
+    """
+    def connectionMade(self):
+        print('CTRL server connection made!')
+        super(CtrlServer, self).connectionMade() # keep reconnection logic working
+        self.transport.write(Ctrl().trap('LOL', 'what'))
+
+    def handle_CTRL(self, data):
+        (cmd, op, v) = data.decode('utf-8').split(' ', 2)
+        print('ctrl received %s (%s) %s' % (cmd, op, v))
+        if 'SET' == cmd:
+            reply = 'SET_REPLY %s %s' % (op, v)
+        if 'GET' == cmd:
+            reply = 'ERROR %s No variable found' % op
+        self.transport.write(reply.encode('utf-8'))
+
+
+class IPAFactory(ReconnectingClientFactory):
+    """
+    Generic IPA Client Factory which can be used to store state for various subprotocols and manage connections
+    Note: so far we do not really need separate Factory for acting as a server due to protocol simplicity
+    """
+    protocol = IPACommon
+    debug = False
+    test_id = IPA().identity(unit = b'1515/0/1', mac = b'b0:0b:fa:ce:de:ad:be:ef', utype = b'sysmoBTS', name = b'StingRay', location = b'hell', sw = IPA.version.encode('utf-8'))
+
+    def __init__(self, proto = None, debug = False):
+        #ReconnectingClientFactory.__init__(self) # need this to be able to modify noisy attribute
+        if proto:
+            self.protocol = proto
+        if debug:
+            self.debug = debug
+
+    def clientConnectionFailed(self, connector, reason):
+        """
+        Only necessayr for as debugging aid - if we can somehow set parent's class noisy attribute than we can omit this method
+        """
+        if self.debug:
+            print('IPAFactory connection failed:', reason.getErrorMessage())
+        ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)
+
+    def clientConnectionLost(self, connector, reason):
+        """
+        Only necessayr for as debugging aid - if we can somehow set parent's class noisy attribute than we can omit this method
+        """
+        if self.debug:
+            print('IPAFactory connection lost:', reason.getErrorMessage())
+        ReconnectingClientFactory.clientConnectionLost(self, connector, reason)
+
+if __name__ == '__main__':
+        print("Twisted IPA app with IPA v%s loaded." % IPA.version)
+        # test as CTRL client, for example by connecting to osmo-bsc to receive TRAP messages when osmo-bts-* connects to it:
+        #reactor.connectTCP('localhost', 4249, IPAFactory(debug = True))
+        # test as CTRL server, for example using bsc_control.py to issue set/get commands:
+        #reactor.listenTCP(4249, IPAFactory(CtrlServer, debug = True))
+        # test as IPA client, for example by connecting to osmo-nitb which would initiate A-bis/IP session:
+        reactor.connectTCP('localhost', IPA.TCP_PORT_OML, IPAFactory(debug = True))
+        reactor.connectTCP('localhost', IPA.TCP_PORT_RSL, IPAFactory(debug = True))
+        # test as IPA server, for example by running osmo-bts-* which would attempt to connect to us:
+        #reactor.listenTCP(IPA.TCP_PORT_RSL, IPAFactory(IPAServer, debug = True))
+        #reactor.listenTCP(IPA.TCP_PORT_OML, IPAFactory(IPAServer, debug = True))
+        reactor.run()

-- 
To view, visit https://gerrit.osmocom.org/1268
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I07559df420b7fe8418f3412f45acd9a375e43bc5
Gerrit-PatchSet: 1
Gerrit-Project: openbsc
Gerrit-Branch: master
Gerrit-Owner: Max <msuraev at sysmocom.de>



More information about the gerrit-log mailing list