def gatewayLogin(gatewayIP, gatewayPort): # cut/paste from python manual tcpSock = None for res in socket.getaddrinfo(gatewayIP, gatewayPort, socket.AF_UNSPEC, socket.SOCK_STREAM): af, socktype, proto, canonname, sa = res try: tcpSock = socket.socket(af, socktype, proto) except OSError as msg: tcpSock = None continue try: tcpSock.connect(sa) except OSError as msg: tcpSock.close() tcpSock = None continue break if tcpSock is None: sys.stderr.write( "ERROR: {}: Could not open socket to gateway host.\n".format(me)) sys.exit(10) #with tcpSock: # get the gateway's attention. The Protocol_Document.pdf explains how, not why. connectString = b'CONNECTSERVERHOST\r\n\r\n' # not a string... tcpSock.sendall(connectString) # the gateway does not respond to the connect message. don't wait for something here because you aren't going to get it # tx/rx challenge (?) (gateway returns its mac address in the form 01-23-45-AB-CD-EF) # why? dunno. tcpSock.sendall(makeMessage(code.CHALLENGE_QUERY)) data = tcpSock.recv(48) if not data: sys.stderr.write("WARNING: {}: no {} data received.\n".format( me, "CHALLENGE_ANSWER")) rcvcode, data = decodeMessage(data) if (rcvcode != code.CHALLENGE_ANSWER): sys.stderr.write("WARNING: {}: rcvCode2({}) != {}.\n".format( me, CHALLENGE_ANSWER)) sys.exit(10) # now that we've "connected" and "challenged," we can "login." None of these things # actually do anything, but they are required. msg = createLoginMessage() tcpSock.sendall(makeMessage(code.LOCALLOGIN_QUERY, msg)) data = tcpSock.recv(48) if not data: sys.stderr.write("WARNING: {}: no {} data received.\n".format( me, "LOCALLOGIN_ANSWER")) rcvCode, data = decodeMessage(data) if (rcvCode != code.LOCALLOGIN_ANSWER): sys.stderr.write("WARNING: {}: rcvCode({}) != {}.\n".format( me, rcvCode, code.LOCALLOGIN_ANSWER)) sys.exit(10) # response should be empty return tcpSock
def queryStatus(gatewaySocket, data): # get pool status gatewaySocket.sendall( makeMessage(code.POOLSTATUS_QUERY, struct.pack("<I", 0))) rcvcode, buff = decodeMessage(gatewaySocket.recv(480)) if (rcvcode != code.POOLSTATUS_ANSWER): sys.stderr.write("WARNING: {}: rcvCode({}) != {}.\n".format( me, rcvCode, code.POOLSTATUS_ANSWER)) sys.exit(11) return gateway.decodeStatusAnswer.decodeStatusAnswer(buff, data)
def queryConfig(gatewaySocket, data): #get controler config gatewaySocket.sendall( makeMessage(code.CTRLCONFIG_QUERY, struct.pack("<2I", 0, 0))) rcvcode, buff = decodeMessage(gatewaySocket.recv(1024)) if (rcvcode != code.CTRLCONFIG_ANSWER): sys.stderr.write("WARNING: {}: rcvCode({}) != {}.\n".format( me, rcvcode, code.CTRLCONFIG_ANSWER)) sys.exit(11) return gateway.decodeConfigAnswer.decodeConfigAnswer(buff, data)
def queryButtonPress(gatewaySocket, circuitID, circuitState): gatewaySocket.sendall( makeMessage(code.BUTTONPRESS_QUERY, struct.pack("<III", 0, circuitID, circuitState))) rcvcode, buff = decodeMessage(gatewaySocket.recv(480)) if (rcvcode != code.BUTTONPRESS_ANSWER): sys.stderr.write("WARNING: {}: rcvCode({}) != {}.\n".format( me, rcvCode, code.BUTTONPRESS_ANSWER)) sys.exit(11) print(rcvcode) return True
def queryGateway(gatewaySocket): # send a simple query and print the response, no advanced decoding required. gatewaySocket.sendall(makeMessage(code.VERSION_QUERY)) data = gatewaySocket.recv(480) if not data: sys.stderr.write("WARNING: {}: no {} data received.\n".format( me, "VERSION_ANSWER")) rcvcode, buff = decodeMessage(data) if (rcvcode != code.VERSION_ANSWER): sys.stderr.write("WARNING: {}: rcvCode({}) != {}.\n".format( me, rcvCode2, code.VERSION_ANSWER)) sys.exit(10) return getMessageString(buff)