updated coresendmsg and core-manage to be 2/3 compliant

This commit is contained in:
bharnden 2019-06-04 14:42:40 -07:00
parent 6c861a6ff8
commit f78736ebfd
2 changed files with 31 additions and 31 deletions

View file

@ -119,13 +119,13 @@ class FileUpdater(object):
search = "emane_models =" search = "emane_models ="
elif target == "nodetype": elif target == "nodetype":
if self.options.userpath is None: if self.options.userpath is None:
raise ValueError, "missing user path" raise ValueError("missing user path")
filename = os.path.join(self.options.userpath, "nodes.conf") filename = os.path.join(self.options.userpath, "nodes.conf")
search = self.data search = self.data
else: else:
raise ValueError, "unknown target" raise ValueError("unknown target")
if not os.path.exists(filename): if not os.path.exists(filename):
raise ValueError, "file %s does not exist" % filename raise ValueError("file %s does not exist" % filename)
return search, filename return search, filename
def update_file(self, fn=None): def update_file(self, fn=None):
@ -236,7 +236,7 @@ def main():
try: try:
up = FileUpdater(action, target, data, options) up = FileUpdater(action, target, data, options)
r = up.process() r = up.process()
except Exception, e: except Exception as e:
sys.stderr.write("Exception: %s\n" % e) sys.stderr.write("Exception: %s\n" % e)
sys.exit(1) sys.exit(1)
if not r: if not r:

View file

@ -19,9 +19,9 @@ def print_available_tlvs(t, tlv_class):
""" """
Print a TLV list. Print a TLV list.
""" """
print "TLVs available for %s message:" % t print("TLVs available for %s message:" % t)
for tlv in sorted([tlv for tlv in tlv_class.tlv_type_map], key=lambda x: x.name): for tlv in sorted([tlv for tlv in tlv_class.tlv_type_map], key=lambda x: x.name):
print "%s:%s" % (tlv.value, tlv.name) print("%s:%s" % (tlv.value, tlv.name))
def print_examples(name): def print_examples(name):
@ -52,9 +52,9 @@ def print_examples(name):
"srcname=\"./test.log\"", "srcname=\"./test.log\"",
"move a test.log file from host to node 2"), "move a test.log file from host to node 2"),
] ]
print "Example %s invocations:" % name print("Example %s invocations:" % name)
for cmd, descr in examples: for cmd, descr in examples:
print " %s %s\n\t\t%s" % (name, cmd, descr) print(" %s %s\n\t\t%s" % (name, cmd, descr))
def receive_message(sock): def receive_message(sock):
@ -68,7 +68,7 @@ def receive_message(sock):
data = sock.recv(4096) data = sock.recv(4096)
msghdr = data[:coreapi.CoreMessage.header_len] msghdr = data[:coreapi.CoreMessage.header_len]
except KeyboardInterrupt: except KeyboardInterrupt:
print "CTRL+C pressed" print("CTRL+C pressed")
sys.exit(1) sys.exit(1)
if len(msghdr) == 0: if len(msghdr) == 0:
@ -84,11 +84,11 @@ def receive_message(sock):
except KeyError: except KeyError:
msg = coreapi.CoreMessage(msgflags, msghdr, msgdata) msg = coreapi.CoreMessage(msgflags, msghdr, msgdata)
msg.message_type = msgtype msg.message_type = msgtype
print "unimplemented CORE message type: %s" % msg.type_str() print("unimplemented CORE message type: %s" % msg.type_str())
return msg return msg
if len(data) > msglen + coreapi.CoreMessage.header_len: if len(data) > msglen + coreapi.CoreMessage.header_len:
print "received a message of type %d, dropping %d bytes of extra data" \ print("received a message of type %d, dropping %d bytes of extra data" \
% (msgtype, len(data) - (msglen + coreapi.CoreMessage.header_len)) % (msgtype, len(data) - (msglen + coreapi.CoreMessage.header_len)))
return msgcls(msgflags, msghdr, msgdata) return msgcls(msgflags, msghdr, msgdata)
@ -103,15 +103,15 @@ def connect_to_session(sock, requested):
smsg = coreapi.CoreSessionMessage.pack(flags, tlvdata) smsg = coreapi.CoreSessionMessage.pack(flags, tlvdata)
sock.sendall(smsg) sock.sendall(smsg)
print "waiting for session list..." print("waiting for session list...")
smsgreply = receive_message(sock) smsgreply = receive_message(sock)
if smsgreply is None: if smsgreply is None:
print "disconnected" print("disconnected")
return False return False
sessstr = smsgreply.get_tlv(SessionTlvs.NUMBER.value) sessstr = smsgreply.get_tlv(SessionTlvs.NUMBER.value)
if sessstr is None: if sessstr is None:
print "missing session numbers" print("missing session numbers")
return False return False
# join the first session (that is not our own connection) # join the first session (that is not our own connection)
@ -119,7 +119,7 @@ def connect_to_session(sock, requested):
sessions = sessstr.split("|") sessions = sessstr.split("|")
sessions.remove(str(localport)) sessions.remove(str(localport))
if len(sessions) == 0: if len(sessions) == 0:
print "no sessions to join" print("no sessions to join")
return False return False
if not requested: if not requested:
@ -127,10 +127,10 @@ def connect_to_session(sock, requested):
elif requested in sessions: elif requested in sessions:
session = requested session = requested
else: else:
print "requested session not found!" print("requested session not found!")
return False return False
print "joining session: %s" % session print("joining session: %s" % session)
tlvdata = coreapi.CoreSessionTlv.pack(SessionTlvs.NUMBER.value, session) tlvdata = coreapi.CoreSessionTlv.pack(SessionTlvs.NUMBER.value, session)
flags = MessageFlags.ADD.value flags = MessageFlags.ADD.value
smsg = coreapi.CoreSessionMessage.pack(flags, tlvdata) smsg = coreapi.CoreSessionMessage.pack(flags, tlvdata)
@ -142,12 +142,12 @@ def receive_response(sock, opt):
""" """
Receive and print a CORE message from the given socket. Receive and print a CORE message from the given socket.
""" """
print "waiting for response..." print("waiting for response...")
msg = receive_message(sock) msg = receive_message(sock)
if msg is None: if msg is None:
print "disconnected from %s:%s" % (opt.address, opt.port) print("disconnected from %s:%s" % (opt.address, opt.port))
sys.exit(0) sys.exit(0)
print "received message:", msg print("received message: %s" % msg)
def main(): def main():
@ -162,13 +162,13 @@ def main():
usagestr += "Supported message flags (flags=f1,f2,...):\n %s" % flags usagestr += "Supported message flags (flags=f1,f2,...):\n %s" % flags
parser = optparse.OptionParser(usage=usagestr) parser = optparse.OptionParser(usage=usagestr)
parser.set_defaults( parser.set_defaults(
port=CORE_API_PORT, port=CORE_API_PORT,
address="localhost", address="localhost",
session=None, session=None,
listen=False, listen=False,
examples=False, examples=False,
tlvs=False, tlvs=False,
tcp=False tcp=False
) )
parser.add_option("-H", dest="examples", action="store_true", parser.add_option("-H", dest="examples", action="store_true",
@ -217,7 +217,7 @@ def main():
# build a message consisting of TLVs from "type=value" arguments # build a message consisting of TLVs from "type=value" arguments
flagstr = "" flagstr = ""
tlvdata = "" tlvdata = b""
for a in args: for a in args:
typevalue = a.split("=") typevalue = a.split("=")
if len(typevalue) < 2: if len(typevalue) < 2:
@ -255,11 +255,11 @@ def main():
try: try:
sock.connect((opt.address, opt.port)) sock.connect((opt.address, opt.port))
except Exception as e: except Exception as e:
print "Error connecting to %s:%s:\n\t%s" % (opt.address, opt.port, e) print("Error connecting to %s:%s:\n\t%s" % (opt.address, opt.port, e))
sys.exit(1) sys.exit(1)
if not connect_to_session(sock, opt.session): if not connect_to_session(sock, opt.session):
print "warning: continuing without joining a session!" print("warning: continuing without joining a session!")
sock.sendall(msg) sock.sendall(msg)
if opt.listen: if opt.listen: