def listenFrom(socket, experiment): global experimentDone, base, timer, timeoutInterval, seqnum msg = gen.listenSockets[socket].recv(packet.packetLength) data = packet.unpack(msg) if data: print "listened ", data[1], " from", socket if data[1] == packet.size and seqnum == packet.size + 1: experimentDone = True else: with baseLock: if data[1] == base: base = data[1] + 1 with baseCond: baseCond.notify() if data[1] == base - 1: with timerLock: with baseLock: if base == seqnum: timer.cancel() else: timer.cancel() sampleRTT = time.time() - data[0] timeoutInterval = timeoutCalculator(sampleRTT) if experiment == 1: timer = threading.Timer( timeoutInterval, timeout1) elif experiment == 2: timer = threading.Timer( timeoutInterval, timeout2) timer.start()
def main(): s = socket() s.bind(("localhost", 3030)) s.listen(1) conn, addr = s.accept() while True: #mensaje recivido incoming_data = conn.recv(1024) print("mensaje recivido en incomingdata del lado servidor" + incoming_data) if incoming_data: packet_id, message = packet.unpack(incoming_data) if packet_id == packet.MESSAGE: packet.send( conn, packet.pack( packet.MESSAGE, 'servicio') #incoming_data debe recivir servicio ) print("incomingdata del lado ddel server" + incoming_data) elif packet_id == packet.PING: packet.send(conn, packet.pack(packet.PING, "")) s.close()
def parse(self): if len(self.rbuf) < 2: return 0, None #print len(self.rbuf) (size,) = struct.unpack('>H', self.rbuf[0:2]) if len(self.rbuf) < size - 2: return 0, None #print 'size=%d' % (size) opcode, msg = packet.unpack(self.rbuf[2:size+2]) self.rbuf = self.rbuf[size+2:] return opcode, msg
def handle(self): valid_ubit = packet.validate_ubit(self.packet, self.uname) if valid_ubit == False: self.server.invalid_count += 1 return # Register user if self.uname not in self.server.users: self.server.users[self.uname] = User(self.uname) user = self.server.users[self.uname] # Unpack and validate packet valid, unpacked, refresh_idx = packet.unpack(self.packet) if valid == False: self.server.invalid_count += 1 return # If this is a packet which is sent to other users, i.e it # can be stringified if unpacked is not None: logging.info(unpacked) # Rate Limit if the packet is of a type that is sent to others allowed = user.add_packet(self.packet) # If user is locked out do not continue if not allowed: print("User {} is locked out".format(self.uname)) self.respond = True resp = packet.pack_message( "You are locked out for {} more seconds".format( (int)(user.limit - clock_gettime(CLOCK_MONOTONIC_RAW)))) self.wfile.write(resp) return if user.packets > self.server.most_active_count: self.server.most_active_count = user.packets self.server.most_active = user.name pkt_idx = self.server.packet_count % PACKET_HISTORY_SIZE self.server.packets[pkt_idx] = self.packet self.server.packet_count += 1 else: if refresh_idx is None: resp = packet.pack_statistics(self.server) else: self.server.refresh_count += 1 resp = packet.pack_refresh(self.server, refresh_idx) self.wfile.write(resp) self.respond = True
def listenPkt(socket, extrasocket): global expectedSeqnum, experimentDone1, experimentDone2, mem while True: if extrasocket == "": if experimentDone1: break else: if socket == "r1": if experimentDone1: break else: if experimentDone2: break msg = gen.listenSockets[socket].recv(packet.packetLength) data = packet.unpack(msg) if data: print "listened ", data[1], " from", socket if data[1] == expectedSeqnum and data[1] != packet.size: mem.append((data[1], data[2])) gen.talkSockets[socket].send(packet.ackPack(data[0], data[1])) print "sent ack for ", data[1], " to ", socket expectedSeqnum += 1 elif data[1] == packet.size and expectedSeqnum == packet.size: mem.append((data[1], data[2])) for i in range(100): gen.talkSockets[socket].send( packet.ackPack(data[0], data[1])) print "sent fin ack for ", data[1], " to ", socket if extrasocket != "": gen.talkSockets[extrasocket].send( packet.ackPack(data[0], data[1])) print "sent fin ack for ", data[1], " to ", extrasocket experimentDone1 = True if extrasocket != "": experimentDone2 = True print "experimentDone from", socket elif data[1] < expectedSeqnum: gen.talkSockets[socket].send(msg) print "sent old ack for ", data[1], " to ", socket print socket, " is done"
def main(): s = socket() s.connect(("localhost", 3030)) while True: message = raw_input("Manda algo al servidor> ") print("Justo despues de mter en el input " + message) if message.lower() == "ping": output_data = packet.pack(packet.PING, "") ticks = time() else: output_data = packet.pack(packet.MESSAGE, message[1:]) print("Mensaje del message[1:] en cliente" + output_data) #elif message.lower() == 'servicio': #output_data = packet.pack(packet.MESSAGE, message[1:]) packet.send(s, output_data) print("impresion del cliente del outData" + output_data) incoming_data = s.recv(1024) print("impresion del cliente del incoData" + incoming_data) if incoming_data: packet_id, message = packet.unpack(incoming_data) if packet_id == packet.MESSAGE: if isinstance(message, bytes): print("asasas" + message) message = message.decode("utf-8") print("Mensaje con decode" + message) #print("El servidor ha respondido: %s." % message) if message == 'servicio': import ConOpenCv pass print("Este mensaje es " + message) elif packet_id == packet.PING: ticks = (time() - ticks) / 2 print("Ping: %.2f ms." % ticks) s.close()
def client_main(t): while True: buf = t.conn.recv(packet.Packet.MAXPACKET) if not buf: raise EOFError() p = packet.unpack(buf) t.on_packet(p)