#!/usr/bin/env python3 ''' TIME server, should respond to time requests of VMS, VAXELN en (soon) RSTS $ open /read /write NETF ONAPI4::"150=" $ write NETF "''f$getsyi("SCSNODE")'" $ read NETF time $ close NETF ''' import sys, json, socket from datetime import datetime from time import sleep def main(argv): encode = json.JSONEncoder().encode decode = json.JSONDecoder().decode logreq = dict (level = 20, message = "Starting TIME server as object 150") print (encode(logreq), file = sys.stderr, flush = True) for work in sys.stdin: work = decode(work) # Each work item contains three standard fields: # handle - an integer that uniquely identifies a connection # type - the type of data, for example "interrupt" # data - the payload received (as a latin-1 encoded string, # which stands for the underlying bytes 1:1) conn = work["handle"] mtype = work["type"] msg = work["data"] if mtype == "data": logreq = dict (level = 20, message = "Data received: " + msg) print (encode(logreq), file = sys.stderr, flush = True) sleep(1) dn = datetime.now() if msg[:6] == 'PIRSTS': # send the local time in RSTS-format dateCode = (int(dn.strftime("%Y"))-1970)*1000+int(dn.strftime("%-j")) timeCode = 1440-int(dn.strftime("%-H"))*60-int(dn.strftime("%-M")) # minutes until midnight remtime = str(dateCode) + " " + str(timeCode) else: # send the local time in VMS-format remtime = dn.strftime("%d-%b-%Y %H:%M:%S.%f").upper()[:23] req = { "handle" : conn, "type" : "data", "data" : remtime } print (encode(req), flush = True) logreq = dict (level = 20, message = "Time sent: " + remtime) print (encode(logreq), file = sys.stderr, flush = True) elif mtype == "connect": logreq = dict (level = 20, message = "Connect request received.") print (encode(logreq), file = sys.stderr, flush = True) req = { "handle" : conn, "type" : "accept" } print (encode(req), flush = True) sleep(1) elif mtype == "disconnect": logreq = dict (level = 20, message = "Disconnect received.") print (encode(logreq), file = sys.stderr, flush = True) # Client connection is closed, exit return 0 # Unexpected EOF on stdin without a preceding disconnect return 1 if __name__ == "__main__": sys.exit(main(sys.argv))