예제 #1
0
파일: Link.py 프로젝트: sifatoooo/CPE_701
    def handle(self):
      global mynode, link1_port, link2_port, link1_flag, link2_flag

      data = self.request[0].strip()
      #socket = self.request[1]

      # set message and split
      message = data
      message = message.split()

      # look for 'hello' message
      if message[0] == "hhhhh":

        # set link flags
        if message[1] == link1_port:
          link1_flag = True
        if message[1] == link2_port:
          link2_flag = True

      # not hello, forward to network layer
      else:
        Network.l3_recvfrom(mynode, data)
예제 #2
0
def l2_recvfrom (client_socket=None, node=None):
  """
  This function will be used in Layer 3, the Network layer. Nowhere in this Layer 2
  is this function used--rather, this layer purely uses good ol' UDP recvfrom.
  """
  while 1:
    data = ''.encode()
    buffer = ''.encode()
    
    # Read a bunch of bytes up to the MTU or length of data.
    while len(data) <= 4096:
      buffer, external_address = client_socket.recvfrom(4096-len(data))

      if buffer:
        data += buffer

      else:
        #thread.interrupt_main()
        break
        
      # This is our protocol. Stop reading when we see \r\n.
      if '\r\n'.encode() in buffer:
        #thread.interrupt_main()
        break
    
    # Split the headers.
    packet = data.decode().split('@@')
    frame_header = packet[0].split('@')
    datagram_header = packet[1].split('@')    
    
    # Now we should have something like [Frame, Datagram, Segment].
    # Step 1. Build a new Frame.
    frame = Frame(frame_header[2], int(frame_header[3]), frame_header[0], int(frame_header[1]), 
                            int(frame_header[4]), packet[1])
                            
    # Step 2. Build a new Datagram so we can pass it to l3_recvfrom().
    # The order goes 6, 7, then 4, 5 because 6/7 is the destination of this packet, which is 
    # destined for where the source is. It's backwards since we are receiving, not sending.
    datagram = Network.Datagram(int(datagram_header[0]), int(datagram_header[1]), int(datagram_header[2]), 
                            int(datagram_header[3]), int(datagram_header[6]), int(datagram_header[7]), 
                            int(datagram_header[4]), int(datagram_header[5]), int(datagram_header[8]), 
                            packet[2])
    
    # l3_recvfrom will return something, but right now, we are not storing that value yet.
    #NetworkLayer.l3_recvfrom(datagram)
    
    return len(buffer), frame, datagram, external_address, \
           Network.l3_recvfrom(client_socket, str(packet[1]+'@@'+packet[2]), node)