2013-08-29 15:21:13 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# (c)2010-2012 the Boeing Company
|
|
|
|
# author: Jeff Ahrenholz <jeffrey.m.ahrenholz@boeing.com>
|
|
|
|
#
|
|
|
|
# List and stop CORE sessions from the command line.
|
|
|
|
#
|
|
|
|
|
2017-04-25 16:45:34 +01:00
|
|
|
import optparse
|
|
|
|
import socket
|
|
|
|
|
2019-04-30 07:31:47 +01:00
|
|
|
from core.api.tlv import coreapi
|
2019-09-10 22:20:51 +01:00
|
|
|
from core.emulator.enumerations import CORE_API_PORT, MessageFlags, SessionTlvs
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
def main():
|
2017-04-25 16:45:34 +01:00
|
|
|
parser = optparse.OptionParser(usage="usage: %prog [-l] <sessionid>")
|
2019-09-10 23:10:24 +01:00
|
|
|
parser.add_option(
|
|
|
|
"-l", "--list", dest="list", action="store_true", help="list running sessions"
|
|
|
|
)
|
2013-08-29 15:21:13 +01:00
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
|
|
|
if options.list is True:
|
2019-09-10 23:10:24 +01:00
|
|
|
num = "0"
|
2017-04-25 16:45:34 +01:00
|
|
|
flags = MessageFlags.STRING.value
|
2013-08-29 15:21:13 +01:00
|
|
|
else:
|
|
|
|
num = args[0]
|
2017-04-25 16:45:34 +01:00
|
|
|
flags = MessageFlags.DELETE.value
|
|
|
|
tlvdata = coreapi.CoreSessionTlv.pack(SessionTlvs.NUMBER.value, num)
|
|
|
|
message = coreapi.CoreSessionMessage.pack(flags, tlvdata)
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
2019-09-10 23:10:24 +01:00
|
|
|
sock.connect(("localhost", CORE_API_PORT))
|
2017-04-25 16:45:34 +01:00
|
|
|
sock.send(message)
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
# receive and print a session list
|
|
|
|
if options.list is True:
|
2017-04-25 16:45:34 +01:00
|
|
|
hdr = sock.recv(coreapi.CoreMessage.header_len)
|
|
|
|
msgtype, msgflags, msglen = coreapi.CoreMessage.unpack_header(hdr)
|
2013-08-29 15:21:13 +01:00
|
|
|
data = ""
|
|
|
|
if msglen:
|
|
|
|
data = sock.recv(msglen)
|
2017-04-25 16:45:34 +01:00
|
|
|
message = coreapi.CoreMessage(msgflags, hdr, data)
|
|
|
|
sessions = message.get_tlv(coreapi.SessionTlvs.NUMBER.value)
|
2019-05-06 00:52:55 +01:00
|
|
|
print("sessions: {}".format(sessions))
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
sock.close()
|
|
|
|
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|