コード例 #1
0
    def get_version(self, mac, addr):
        target = (bytes.fromhex(mac) + bytes(8))[:8]
        self.sequence = (self.sequence + 1) & 0xff
        out_data = protocol.Frame(
            frame_header=protocol.FrameHeader(source=self.source),
            frame_address=protocol.FrameAddress(target=target,
                                                res_required=True,
                                                sequence=self.sequence),
            protocol_header=protocol.ProtocolHeader(
                _type=protocol.PacketType.DEVICE_GET_VERSION)).serialize()

        now = time.monotonic()
        timeout = now
        for i in range(GET_VERSION_TRIES):
            self.socket.sendto(out_data, addr)

            timeout += GET_VERSION_TIMEOUT
            while now < timeout:
                r, _, _ = select.select([self.socket], [], [], timeout - now)
                if len(r):
                    in_data, in_addr = self.socket.recvfrom(0x1000)
                    frame = protocol.Frame()
                    frame.deserialize(in_data)
                    if (in_addr == addr and frame.frame_header.protocol == 1024
                            and frame.frame_header.addressable
                            and frame.frame_header.source == self.source
                            and frame.frame_address.target == target
                            and frame.frame_address.sequence == self.sequence
                            and frame.protocol_header.type
                            == protocol.PacketType.DEVICE_STATE_VERSION):
                        return frame.payload
                now = time.monotonic()
        raise UDPException()
コード例 #2
0
def protocol4(ps):
    next_frame_to_send = protocol.SequenceNumber(0, MAX_SEQ)
    frame_expected = protocol.SequenceNumber(0, MAX_SEQ)
    r = protocol.Frame()
    s = protocol.Frame()
    buffer = protocol.Packet()
    ps.from_network_layer(buffer)
    s.info = buffer
    s.seq = next_frame_to_send.copy()
    s.ack = frame_expected.opposite()
    ps.to_physical_layer(s)
    ps.start_timer(s.seq.value)
    while True:
        event = ps.wait_for_event()
        if event == protocol.frame_arrival:
            r = ps.from_physical_layer()
            if r.seq == frame_expected:
                ps.to_network_layer(r.info)
                frame_expected.inc()
            if r.ack == next_frame_to_send:
                ps.stop_timer(r.ack.value)
                ps.from_network_layer(buffer)
                next_frame_to_send.inc()
        s.info = buffer
        s.seq = next_frame_to_send.copy()
        s.ack = frame_expected.opposite()
        ps.to_physical_layer(s)
        ps.start_timer(s.seq.value)
コード例 #3
0
def sender1(ps):
    while True:
        buffer = protocol.Packet()
        ps.from_network_layer(buffer)
        s = protocol.Frame()
        s.info = buffer
        ps.to_physical_layer(s)
コード例 #4
0
ファイル: go-back-n.py プロジェクト: andrew-chen/csis349
def send_data(frame_nr,frame_expected,buffer,ps):
	s = protocol.Frame()
	s.info = buffer[frame_nr.value]
	s.seq = frame_nr.copy()
	s.ack = frame_expected.copy().decr()
	ps.to_physical_layer(s)
	ps.start_timer(frame_nr.value)
コード例 #5
0
def receiver2(ps):
	while True:
		time.sleep(1)
		ps.wait_for_event()
		s = ps.from_physical_layer()
		buffer = s.info
		ps.to_network_layer(buffer)
		ps.to_physical_layer(protocol.Frame())
コード例 #6
0
ファイル: color.py プロジェクト: nick-lifx/hsbk_rgb
 def rx_frame(self, data):
     frame = protocol.Frame()
     frame.deserialize(data)
     if (frame.frame_header.protocol == 1024
             and frame.frame_header.addressable
             and frame.frame_header.source == udp.source
             and frame.frame_address.target
             == (bytes.fromhex(self.mac) + bytes(8))[:8]
             and frame.frame_address.sequence == self.sequence
             and frame.protocol_header.type
             == protocol.PacketType.DEVICE_ACKNOWLEDGEMENT):
         self.timeout = None
         self.out_data = None
コード例 #7
0
    def get_service(self, mac=None):
        target = bytes(8) if mac is None else (bytes.fromhex(mac) +
                                               bytes(8))[:8]
        self.sequence = (self.sequence + 1) & 0xff
        out_data = protocol.Frame(
            frame_header=protocol.FrameHeader(source=self.source),
            frame_address=protocol.FrameAddress(target=target,
                                                res_required=True,
                                                sequence=self.sequence),
            protocol_header=protocol.ProtocolHeader(
                _type=protocol.PacketType.DEVICE_GET_SERVICE)).serialize()

        now = time.monotonic()
        timeout = now
        result = {}
        for i in range(GET_SERVICE_TRIES):
            self.socket.sendto(out_data, ('255.255.255.255', 56700))

            timeout += GET_SERVICE_TIMEOUT
            while now < timeout:
                r, _, _ = select.select([self.socket], [], [], timeout - now)
                if len(r):
                    in_data, in_addr = self.socket.recvfrom(0x1000)
                    frame = protocol.Frame()
                    frame.deserialize(in_data)
                    if (frame.frame_header.protocol == 1024
                            and frame.frame_header.addressable
                            and frame.frame_header.source == self.source
                            and frame.frame_address.sequence == self.sequence
                            and frame.protocol_header.type
                            == protocol.PacketType.DEVICE_STATE_SERVICE):
                        mac = frame.frame_address.target[:6].hex()
                        if mac not in result:
                            result[mac] = (in_addr, {})
                        result[mac][1][
                            frame.payload.service] = frame.payload.port
                now = time.monotonic()
        return result
コード例 #8
0
    def set_color(self, mac, addr, hsbk):
        target = (bytes.fromhex(mac) + bytes(8))[:8]
        self.sequence = (self.sequence + 1) & 0xff
        out_data = protocol.Frame(
            frame_header=protocol.FrameHeader(source=self.source),
            frame_address=protocol.FrameAddress(target=target,
                                                ack_required=True,
                                                sequence=self.sequence),
            protocol_header=protocol.ProtocolHeader(
                _type=protocol.PacketType.LIGHT_SET_COLOR),
            payload=protocol.LightSetColor(color=protocol.LightHsbk(
                hue=int(round((hsbk[HSBK_HUE] % 360.) * (0xffff / 360.))),
                saturation=int(round(hsbk[HSBK_SAT] * 0xffff)),
                brightness=int(round(hsbk[HSBK_BR] * 0xffff)),
                kelvin=int(round(hsbk[HSBK_KELV]))))).serialize()

        now = time.monotonic()
        timeout = now
        for i in range(SET_COLOR_TRIES):
            self.socket.sendto(out_data, addr)

            timeout += SET_COLOR_TIMEOUT
            while now < timeout:
                r, _, _ = select.select([self.socket], [], [], timeout - now)
                if len(r):
                    in_data, in_addr = self.socket.recvfrom(0x1000)
                    frame = protocol.Frame()
                    frame.deserialize(in_data)
                    if (in_addr == addr and frame.frame_header.protocol == 1024
                            and frame.frame_header.addressable
                            and frame.frame_header.source == self.source
                            and frame.frame_address.target == target
                            and frame.frame_address.sequence == self.sequence
                            and frame.protocol_header.type
                            == protocol.PacketType.DEVICE_ACKNOWLEDGEMENT):
                        return
                now = time.monotonic()
        raise UDPException()
コード例 #9
0
    def get_color(self, mac, addr):
        target = (bytes.fromhex(mac) + bytes(8))[:8]
        self.sequence = (self.sequence + 1) & 0xff
        out_data = protocol.Frame(
            frame_header=protocol.FrameHeader(source=self.source),
            frame_address=protocol.FrameAddress(target=target,
                                                res_required=True,
                                                sequence=self.sequence),
            protocol_header=protocol.ProtocolHeader(
                _type=protocol.PacketType.LIGHT_GET)).serialize()

        now = time.monotonic()
        timeout = now
        for i in range(GET_COLOR_TRIES):
            self.socket.sendto(out_data, addr)

            timeout += GET_COLOR_TIMEOUT
            while now < timeout:
                r, _, _ = select.select([self.socket], [], [], timeout - now)
                if len(r):
                    in_data, in_addr = self.socket.recvfrom(0x1000)
                    frame = protocol.Frame()
                    frame.deserialize(in_data)
                    if (in_addr == addr and frame.frame_header.protocol == 1024
                            and frame.frame_header.addressable
                            and frame.frame_header.source == self.source
                            and frame.frame_address.target == target
                            and frame.frame_address.sequence == self.sequence
                            and frame.protocol_header.type
                            == protocol.PacketType.LIGHT_STATE):
                        return numpy.array([
                            frame.payload.color.hue *
                            (360. / 0xffff), frame.payload.color.saturation *
                            (1. / 0xffff), frame.payload.color.brightness *
                            (1. / 0xffff), frame.payload.color.kelvin
                        ], numpy.double)
                now = time.monotonic()
        raise UDPException()
コード例 #10
0
ファイル: par.py プロジェクト: andrew-chen/csis349
def receiver3(ps):
    s = protocol.Frame()
    frame_expected = 0
    while True:
        time.sleep(1)  # should be less than the timeout_amount!
        event = ps.wait_for_event()
        if event == protocol.frame_arrival:
            r = ps.from_physical_layer()
            if r.seq == frame_expected:
                buffer = r.info
                ps.to_network_layer(buffer)
            s.ack = frame_expected
            frame_expected = 1 - frame_expected
            ps.to_physical_layer(s)  #indentation important!
コード例 #11
0
def send_frame(fk, frame_nr, frame_expected, buffer, ps):
    """
		Construct and send a data, ack, or nak frame
	"""
    global no_nak
    s = protocol.Frame()  # scratch variable
    s.kind = fk  # kind == data, ack, or nak
    if fk == protocol.data:
        s.info = buffer[frame_nr.value]
    s.seq = frame_nr  # only meaningful for data frames
    s.ack = frame_expected.copy().decr()
    if fk == protocol.nak:
        no_nak = False
    ps.to_physical_layer(s)
    if fk == protocol.data:
        ps.start_timer(frame_nr.value)
    ps.stop_ack_timer()
コード例 #12
0
ファイル: color.py プロジェクト: nick-lifx/hsbk_rgb
 def set_color(self, hsbk, now):
     target = (bytes.fromhex(self.mac) + bytes(8))[:8]
     self.sequence = (self.sequence + 1) & 0xff
     self.out_data = protocol.Frame(
         frame_header=protocol.FrameHeader(source=udp.source),
         frame_address=protocol.FrameAddress(target=target,
                                             ack_required=True,
                                             sequence=self.sequence),
         protocol_header=protocol.ProtocolHeader(
             _type=protocol.PacketType.LIGHT_SET_COLOR),
         payload=protocol.LightSetColor(color=protocol.LightHsbk(
             hue=int(round((hsbk[HSBK_HUE] % 360.) * (0xffff / 360.))),
             saturation=int(round(hsbk[HSBK_SAT] * 0xffff)),
             brightness=int(round(hsbk[HSBK_BR] * 0xffff)),
             kelvin=int(round(hsbk[HSBK_KELV]))))).serialize()
     if self.timeout is None:
         self.timeout = now
コード例 #13
0
ファイル: par.py プロジェクト: andrew-chen/csis349
def sender3(ps):
    buffer = protocol.Packet()
    ps.from_network_layer(buffer)
    next_frame_to_send = 0
    while True:
        s = protocol.Frame()
        s.info = buffer
        s.seq = next_frame_to_send
        print("sending", s.seq)
        ps.to_physical_layer(s)
        print("starting", s.seq)
        ps.start_timer(s.seq)
        event = ps.wait_for_event()
        if event == protocol.frame_arrival:
            s = ps.from_physical_layer()
            print("got ack", s.ack)
            if s.ack == next_frame_to_send:
                print("ack was desired")
                print("stopping", s.ack)
                ps.stop_timer(s.ack)
                ps.from_network_layer(buffer)
                # in stop-and-wait PAR, seq changes as
                next_frame_to_send = 1 - next_frame_to_send
コード例 #14
0
    for token in nlist:
        with open('data/' + token, 'rb') as f:
            status[nlist.index(token)] = pickle.load(f)
except:
    pass

t = []
try:
    with open('data/t', 'rb') as f:
        t = pickle.load(f)
except:
    pass

while True:
    string = filelockread(addr_list[0], lock)
    if frame.valid(p.Frame(string)):
        frame.update_frame(string)
        status_str = frame.data.decode('cp1252')
        token_sts = [
            status_str[8 * i:8 * (i + 1)]
            for i in range(int(len(status_str) / 8))
        ]
        token_sts_dict = dict(zip(nlist, token_sts))
        t.append(dt.datetime.now())
        with open('data/t', 'wb') as ft:
            pickle.dump(t, ft)
        for token, sts in token_sts_dict.items():
            i = nlist.index(token)
            status[i].append(stat(sts))
            with open('data/' + token, 'wb') as f:
                pickle.dump(status[i], f)
コード例 #15
0
ファイル: client.py プロジェクト: belal-bh/computer-networks
    PKT_BUFFER = 10
first_acknowledgement = str(PKT_BUFFER)
client.send(first_acknowledgement)

packet = protocol.Packet(PKT_BUFFER)

message = "Hello, I am a client. I am sending this message. Happy coding!"
packet.append_msg(message)

active = True
while active:
    # input()
    buffer = protocol.from_network_layer(packet)
    if buffer == None:
        print(f"[NOTE] No more message left.")
        more_msg = input(
            "Enter new message to send OR press ENTER to DISCONNECT: ")
        if more_msg:
            packet.append_msg(more_msg)
            continue
        active = False
        continue
    frame = protocol.Frame(frame_kind='data',
                           head=HEAD,
                           tail=TAIL,
                           packet=buffer)
    protocol.to_physical_layer(frame, client)
    protocol.wait_for_event(event=client, t=2)
    print()

client.send(DISCONNECT_MESSAGE)
コード例 #16
0
def protocol6(ps):

    global no_nak

    r = protocol.Frame()
    out_buf = mod_list(map(lambda x: protocol.Packet(), range(NR_BUFS)))
    in_buf = mod_list(map(lambda x: protocol.Packet(), range(NR_BUFS)))
    arrived = mod_list(map(lambda x: False, range(NR_BUFS)))

    def dump():
        for i in range(NR_BUFS):
            ps.println(str(("dump", i, "arrived", arrived[i])))
        for i in range(NR_BUFS):
            ps.println(str(("dump", i, "in_buf", in_buf[i].data)))
        for i in range(NR_BUFS):
            ps.println(str(("dump", i, "out_buf", out_buf[i].data)))

    ps.enable_network_layer()
    ack_expected = protocol.SequenceNumber(0, MAX_SEQ)
    next_frame_to_send = protocol.SequenceNumber(0, MAX_SEQ)
    frame_expected = protocol.SequenceNumber(0, MAX_SEQ)
    too_far = protocol.SequenceNumber(NR_BUFS, MAX_SEQ)
    nbuffered = 0

    while True:
        event = ps.wait_for_event()
        if event == protocol.network_layer_ready:
            nbuffered = nbuffered + 1
            ps.from_network_layer(out_buf[next_frame_to_send.value])
            send_frame(protocol.data, next_frame_to_send, frame_expected,
                       out_buf, ps)
            next_frame_to_send.inc()
        elif event == protocol.frame_arrival:
            r = ps.from_physical_layer()
            if r.kind == protocol.data:
                if r.seq != frame_expected and no_nak:
                    send_frame(protocol.nak,
                               protocol.SequenceNumber(0, MAX_SEQ),
                               frame_expected, out_buf, ps)
                else:
                    ps.start_ack_timer()
                if r.seq.between(frame_expected,
                                 too_far) and arrived[r.seq.value] == False:
                    arrived[r.seq.value] = True
                    in_buf[r.seq.value] = r.info
                    while (arrived[frame_expected.value]):
                        ps.to_network_layer(in_buf[frame_expected.value])
                        no_nak = False
                        arrived[frame_expected.value] = False
                        frame_expected.inc()
                        too_far.inc()
                        ps.start_ack_timer()
                else:
                    if r.seq.between(frame_expected, too_far):
                        pass
                    else:
                        pass
                    if arrived[r.seq.value] == False:
                        pass
                    else:
                        pass
            if r.kind == protocol.nak and r.ack.copy().inc().between(
                    ack_expected, next_frame_to_send):
                send_frame(protocol.data,
                           r.ack.copy().inc(), frame_expected, out_buf, ps)
            while r.ack.between(ack_expected, next_frame_to_send):
                nbuffered = nbuffered - 1
                ps.stop_timer(ack_expected.value)
                ack_expected.inc()
        elif event == protocol.cksum_err:
            if no_nak:
                send_frame(protocol.nak, protocol.SequenceNumber(0, MAX_SEQ),
                           frame_expected, out_buf, ps)
        elif event == protocol.timeout:
            send_frame(protocol.data,
                       protocol.SequenceNumber(event.value, MAX_SEQ),
                       frame_expected, out_buf, ps)
        elif event == protocol.ack_timeout:
            send_frame(protocol.ack, protocol.SequenceNumber(0, MAX_SEQ),
                       frame_expected, out_buf, ps)
        if nbuffered < NR_BUFS:
            ps.enable_network_layer()
        else:
            ps.disable_network_layer()