bsc_control.py: use ipa.py module
Simplify code by using Ctrl implementation from ipa.py Change-Id: I25fd7cd4b42126354b72abd60a3837be5d13e159
This commit is contained in:
parent
82caa3e9b7
commit
bd33f54444
|
@ -1,30 +1,13 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
# -*- mode: python-mode; py-indent-tabs-mode: nil -*-
|
# -*- mode: python-mode; py-indent-tabs-mode: nil -*-
|
||||||
|
|
||||||
import sys,os, random
|
import random
|
||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
|
from ipa import Ctrl
|
||||||
import socket
|
import socket
|
||||||
import struct
|
|
||||||
|
|
||||||
verbose = False
|
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):
|
|
||||||
raise BaseException("Wrong protocol in answer!")
|
|
||||||
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:]
|
|
||||||
|
|
||||||
def connect(host, port):
|
def connect(host, port):
|
||||||
if verbose:
|
if verbose:
|
||||||
print "Connecting to host %s:%i" % (host, port)
|
print "Connecting to host %s:%i" % (host, port)
|
||||||
|
@ -34,39 +17,31 @@ def connect(host, port):
|
||||||
sck.connect((host, port))
|
sck.connect((host, port))
|
||||||
return sck
|
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):
|
def do_set_get(sck, var, value = None):
|
||||||
r = random.randint(1, sys.maxint)
|
(r, c) = Ctrl().cmd(var, value)
|
||||||
if (value != None):
|
sck.send(c)
|
||||||
s = 'SET_REPLY'
|
answer = Ctrl().rem_header(sck.recv(4096))
|
||||||
do_set(var, value, r, sck)
|
return (answer,) + Ctrl().verify(answer, r, var, value)
|
||||||
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
|
|
||||||
|
|
||||||
def set_var(sck, var, val):
|
def set_var(sck, var, val):
|
||||||
return do_set_get(sck, var, val)
|
(a, _, _) = do_set_get(sck, var, val)
|
||||||
|
return a
|
||||||
|
|
||||||
def get_var(sck, var):
|
def get_var(sck, var):
|
||||||
return do_set_get(sck, var)
|
(_, _, v) = do_set_get(sck, var)
|
||||||
|
return v
|
||||||
|
|
||||||
|
def _leftovers(sck):
|
||||||
|
data = sck.recv(1024)
|
||||||
|
if len(data) != 0:
|
||||||
|
tail = data
|
||||||
|
while True:
|
||||||
|
(head, tail) = Ctrl().split_combined(tail)
|
||||||
|
print "Got message:", Ctrl().rem_header(head)
|
||||||
|
if len(tail) == 0:
|
||||||
|
break
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
random.seed()
|
random.seed()
|
||||||
|
@ -105,27 +80,19 @@ if __name__ == '__main__':
|
||||||
if options.cmd_set:
|
if options.cmd_set:
|
||||||
if len(args) < 2:
|
if len(args) < 2:
|
||||||
parser.error("Set requires var and value arguments")
|
parser.error("Set requires var and value arguments")
|
||||||
do_set(args[0], ' '.join(args[1:]), options.op_id, sock)
|
_leftovers(sock)
|
||||||
|
print "Got message:", set_var(sock, args[0], ' '.join(args[1:]))
|
||||||
|
|
||||||
if options.cmd_get:
|
if options.cmd_get:
|
||||||
if len(args) != 1:
|
if len(args) != 1:
|
||||||
parser.error("Get requires the var argument")
|
parser.error("Get requires the var argument")
|
||||||
do_get(args[0], options.op_id, sock)
|
_leftovers(sock)
|
||||||
|
(a, _, _) = do_set_get(sock, args[0])
|
||||||
data = sock.recv(1024)
|
print "Got message:", a
|
||||||
while (len(data)>0):
|
|
||||||
(answer, data) = remove_ipa_ctrl_header(data)
|
|
||||||
print "Got message:", answer
|
|
||||||
|
|
||||||
if options.monitor:
|
if options.monitor:
|
||||||
while True:
|
while True:
|
||||||
data = sock.recv(1024)
|
if not _leftovers(sock):
|
||||||
if len(data) == 0:
|
|
||||||
print "Connection is gone."
|
print "Connection is gone."
|
||||||
break
|
break
|
||||||
|
|
||||||
while len(data) > 0:
|
|
||||||
(answer, data) = remove_ipa_ctrl_header(data)
|
|
||||||
print "Got message:", answer
|
|
||||||
|
|
||||||
sock.close()
|
sock.close()
|
||||||
|
|
Loading…
Reference in New Issue