def process(self, z, b): if isinstance(z, msgs.GetHostByName): if not intercept: self.conn.send( cl.make( msgs.GetHostByNameResponse, host=socket.gethostbyname(z.name), #"10.0.0.251", name=z.name, status=0)) else: self.conn.send( cl.make( msgs.GetHostByNameResponse, #host="10.0.0.148", #251", host="10.0.0.148", #251", name=z.name, status=0)) elif isinstance(z, msgs.SocketOpen): self.sockets.open(z.streamId, z.host, z.port) elif isinstance(z, msgs.SocketClose): self.sockets.close(z.streamId) self.conn.send( cl.make(msgs.SocketCloseResponse, status=0, streamId=z.streamId)) elif isinstance(z, msgs.SocketDataChunk): self.sockets.send(z.streamId, b) elif isinstance(z, msgs.SocketError): self.sockets.close(z.streamId) else: pass
def service(self): for sid, connected, data, err in self.sockets.service(): if data: self.conn.send(cl.make(msgs.SocketDataChunk, streamId=sid), extra=data) elif err: if connected: self.conn.send( cl.make(msgs.SocketError, streamId=sid, errorCode=err)) else: self.conn.send( cl.make(msgs.SocketOpenResponse, status=1, streamId=sid, errorCode=err)) elif connected: self.conn.send( cl.make(msgs.SocketOpenResponse, status=0, streamId=sid))
### elif descriptor.type == descriptor.TYPE_ENUM: ### print(" enum") ### #enum_name = descriptor.enum_type.values[value].name ### #print "%s: %s" % (descriptor.full_name, enum_name) ### else: ### print(" other") ### #print "%s: %s" % (descriptor.full_name, value) ### #print("Field: ", d) ### ##dump_object(b) ## #f = BCMessageDecoder() #msg = sendMessage(b) # #for z in f.process(msg): # print(z) # ##decodeMessage(msg) # # print( cl.make( msgs.BlackCoralFileTransfer, startFileTransferResponse=cl.make( msgs.StartFileTransferResponse, checksum=123, id="abcd", length=12, #len(filedata), status=0)))
# required filetransfer_Status status = 2; # } # # optional StartFileTransferResponse startFileTransferResponse = 1; # message StartFileTransferResponse { # optional uint32 checksum = 4; # required string id = 1; # optional uint32 length = 3; # required filetransfer_Status status = 2; # } # } # sendmsg( cl.make(msgs.BlackCoralFileTransfer, startFileTransferResponse=cl.make(msgs.StartFileTransferResponse, checksum=checksum, id=avail_path, length=len(filedata), status=0))) receiver = cl.BCMessageDecoder() while True: data, addr = sock_recv.recvfrom(1024) # buffer size is 1024 bytes for z, b in receiver.process(data): if b: print("Got: ", type(z).__name__, z, "buf=", b) else: print("Got: ", type(z).__name__, z) if isinstance(z, msgs.CompanionFileTransfer): if z.HasField("fileChunkRequest"): fid = z.fileChunkRequest.id
print("Got: ", type(z).__name__, z, "buf=", b) else: print("Got: ", type(z).__name__, z) if isinstance(z, msgs.CompanionFileTransfer): if z.HasField("fileChunkRequest"): fid = z.fileChunkRequest.id print("FileChunkRequest, id=", fid) if True: #fid == avail_path: print("Sending file chunk: ", z.fileChunkRequest.startByte, z.fileChunkRequest.length) sendmsg(cl.make(msgs.BlackCoralFileTransfer, fileChunkResponse=cl.make( msgs.FileChunkResponse, id=fid, startByte=z.fileChunkRequest.startByte, status=0 ) ), filedata[z.fileChunkRequest.startByte:(z.fileChunkRequest.startByte+z.fileChunkRequest.length)]) #required uint32 length = 3; #required uint32 startByte = 2; #pass elif z.HasField("startFileTransfer"): fid = z.startFileTransfer.id print("StartFileTransfer, id=", fid) if True: #fid == avail_path: # send back response
res = sock_recv.recvfrom(1024) # buffer size is 1024 bytes if res: did_something = True data, _ = res for z, b in receiver.process(data): if b: print("Got: ", type(z).__name__, z, "buf=", b) else: print("Got: ", type(z).__name__, z) if isinstance(z, msgs.GetHostByName): if not intercept: sendmsg( cl.make( msgs.GetHostByNameResponse, host=socket.gethostbyname( z.name), #"10.0.0.251", name=z.name, status=0)) else: sendmsg( cl.make( msgs.GetHostByNameResponse, #host="10.0.0.148", #251", host="10.0.0.148", #251", name=z.name, status=0)) elif isinstance(z, msgs.SocketOpen): sock_handler.open(z.streamId, z.host, z.port) elif isinstance(z, msgs.SocketClose): sock_handler.close(z.streamId) sendmsg(
def handle_command(cmd): global running cmd = cmd.strip() try: if cmd == 'help': print(""" pair - StartInputDevicePairing exp <name> [key=value ....] - StartExperience with arguments sexp <name> - stop experience conn 0/1 - set if network is connected dummy n - send dummy msg tsettings - request templated settings features - request features caplogs [name] - CaptureLogs (optional reportId) screenshot [format] - CaptureScreenshot (optional format) """) elif cmd.startswith('conn '): conn.send( cl.make(msgs.BlackCoral, sync=cl.make(msgs.BlackCoralSync, state=cl.make( msgs.State, isNetReachable=cmd.split()[1] == '1', )))) elif cmd == 'pair': h = msgs.StartInputDevicePairing() b = msgs.BlackCoral() b.startInputDevicePairing.CopyFrom(h) conn.send(b) elif cmd.startswith('dummy '): n = cmd.split()[1] print("Sending dummy_%s" % (n)) conn.send( cl.make(msgs.BlackCoral, **{'dummy_%s' % (n): cl.make(msgs.DummyEmpty)})) elif cmd.startswith("features"): conn.send( cl.make(msgs.BlackCoralFeatures, featureList=cl.make(msgs.GetFeatureList))) elif cmd.startswith("tsettings"): conn.send( cl.make(msgs.BlackCoralTemplatedSettings, getSettings=cl.make(msgs.GetSettings))) elif cmd.startswith("screenshot"): parts = cmd.split() if len(parts) > 1: conn.send(cl.make(msgs.CaptureScreenshot, fileformat=parts[1])) else: conn.send(cl.make(msgs.CaptureScreenshot)) elif cmd.startswith("caplogs"): parts = cmd.split() if len(parts) > 1: conn.send(cl.make(msgs.CaptureLogs, reportId=parts[1])) else: conn.send(cl.make(msgs.CaptureLogs)) elif cmd == 'quit': do_quit() #running = False #conn.close() elif cmd.startswith("sexp"): import re #data = """part 1;"this is ; part 2;";'this is ; part 3';part 4;this "is ; part" 5""" PATTERN = re.compile(r'''((?:[^\ "']|"[^"]*"|'[^']*')+)''') parts = PATTERN.split(cmd)[1::2] if len(parts) < 2: print("Bad cmd") else: if parts[1].startswith('"'): parts[1] = parts[1][1:-1] conn.send( cl.make(msgs.BlackCoral, stopExperience=cl.make( msgs.StopExperience, name=parts[1], ))) elif cmd.startswith("exp"): import re #data = """part 1;"this is ; part 2;";'this is ; part 3';part 4;this "is ; part" 5""" PATTERN = re.compile(r'''((?:[^\ "']|"[^"]*"|'[^']*')+)''') parts = PATTERN.split(cmd)[1::2] if len(parts) < 2: print("Bad cmd") else: if parts[1].startswith('"'): parts[1] = parts[1][1:-1] args = [(k.split('=')[0], k.split('=')[1]) for k in parts[2:]] print(parts[0], parts[1], args) conn.send( cl.make(msgs.BlackCoral, startExperience=cl.make( msgs.StartExperience, name=parts[1], parameters=[ cl.make(msgs.ParameterEntry, key=k, value=v) for k, v in args ]))) else: print("Invalid command: " + cmd) #except KeyboardInterrupt: # raise except Exception as e: print("Exception handling command: ", e) pass