[PATCH] openbsc[master]: Simplify bsc_control.py

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:23 UTC 2016


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

Simplify bsc_control.py

Use functions from ipa module to simplify existing code and to
facilitate testing of ipa module.

Change-Id: I25fd7cd4b42126354b72abd60a3837be5d13e159
---
M openbsc/contrib/bsc_control.py
1 file changed, 18 insertions(+), 56 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/openbsc refs/changes/66/1266/1

diff --git a/openbsc/contrib/bsc_control.py b/openbsc/contrib/bsc_control.py
index de0c2a9..a0d25b1 100755
--- a/openbsc/contrib/bsc_control.py
+++ b/openbsc/contrib/bsc_control.py
@@ -1,28 +1,18 @@
 #!/usr/bin/python
 
-import sys,os, random
 from optparse import OptionParser
+from ipa import Ctrl
 import socket
-import struct
 
 verbose = False
 
-def prefix_ipa_ctrl_header(data):
-	return struct.pack(">HBB", len(data)+1, 0xee, 0) + data
-
-def ipa_ctrl_header(header):
-        (plen, ipa_proto, osmo_proto) = struct.unpack(">HBB", header)
-        return None if (ipa_proto != 0xee or osmo_proto != 0) else plen
-
 def remove_ipa_ctrl_header(data):
-	if (len(data) < 4):
-		raise BaseException("Answer too short!")
-        plen = ipa_ctrl_header(data[:4])
-        if (None == plen):
+        (plen, d) = Ctrl().del_header(data)
+        if None == d:
                 raise BaseException("Wrong protocol in answer!")
-	if (plen + 3 > len(data)):
+	if plen + 3 > len(data):
 		print "Warning: Wrong payload length (expected %i, got %i)" % (plen, len(data) - 3)
-	return data[4:plen+3], data[plen+3:]
+	return d[:plen + 3], d[plen + 3:]
 
 def connect(host, port):
 	if verbose:
@@ -33,43 +23,20 @@
 	sck.connect((host, port))
 	return sck
 
-def send(sck, data):
-	if verbose:
-		print "Sending \"%s\"" %(data)
-	data = prefix_ipa_ctrl_header(data)
-	sck.send(data)
-
-def do_set(var, value, op_id, sck):
-	setmsg = "SET %s %s %s" %(op_id, var, value)
-	send(sck, setmsg)
-
-def do_get(var, op_id, sck):
-	getmsg = "GET %s %s" %(op_id, var)
-	send(sck, getmsg)
-
 def do_set_get(sck, var, value = None):
-        r = random.randint(1, sys.maxint)
-        if (value != None):
-                s = 'SET_REPLY'
-                do_set(var, value, r, sck)
-        else:
-                s = 'GET_REPLY'
-                do_get(var, r, sck)
-        (answer, data) = remove_ipa_ctrl_header(sck.recv(4096))
-        x  = answer.split()
-        if (s == x[0] and str(r) == x[1] and var == x[2]):
-                return None if ('SET_REPLY' == s and value != x[3]) else x[3]
-        return None
+        (r, c) = Ctrl().cmd(var, value)
+        sck.send(c)
+        (_, answer) = Ctrl().del_header(sck.recv(4096))
+        return (answer,) + Ctrl().verify(answer, r, var, value)
 
 def set_var(sck, var, val):
-        return do_set_get(sck, var, val)
+        do_set_get(sck, var, val)
 
 def get_var(sck, var):
-        return do_set_get(sck, var)
+        (_, _, v) = do_set_get(sck, var)
+        return v
 
 if __name__ == '__main__':
-        random.seed()
-
         parser = OptionParser("Usage: %prog [options] var [value]")
         parser.add_option("-d", "--host", dest="host",
                           help="connect to HOST", metavar="HOST")
@@ -79,8 +46,6 @@
                           dest="cmd_get", help="perform GET operation")
         parser.add_option("-s", "--set", action="store_true",
                           dest="cmd_set", help="perform SET operation")
-        parser.add_option("-i", "--id", dest="op_id", default=random.randint(1, sys.maxint),
-                          help="set id manually", metavar="ID")
         parser.add_option("-v", "--verbose", action="store_true",
                           dest="verbose", help="be verbose", default=False)
         parser.add_option("-m", "--monitor", action="store_true",
@@ -104,26 +69,23 @@
         if options.cmd_set:
 	        if len(args) < 2:
 		        parser.error("Set requires var and value arguments")
-	        do_set(args[0], ' '.join(args[1:]), options.op_id, sock)
+                (a, _, _) = do_set_get(sock, args[0], ' '.join(args[1:]))
+	        print "Got message:", a
 
         if options.cmd_get:
 	        if len(args) != 1:
 		        parser.error("Get requires the var argument")
-	        do_get(args[0], options.op_id, sock)
-
-        data = sock.recv(1024)
-        while (len(data)>0):
-	        (answer, data) = remove_ipa_ctrl_header(data)
-	        print "Got message:", answer
+                (a, _, _) = do_set_get(sock, args[0])
+	        print "Got message:", a
 
         if options.monitor:
-	        while (True):
+	        while True:
 		        data = sock.recv(1024)
 		        if len(data) == 0:
 			        print "Connection is gone."
 			        break
 
-		        while (len(data)>0):
+		        while len(data) > 0:
 			        (answer, data) = remove_ipa_ctrl_header(data)
 			        print "Got message:", answer
 

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

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



More information about the gerrit-log mailing list