def test_l2_recvfrom (self):
   #addr = ''
   #frame_queue = Queue.Queue()
   #external_address_queue = Queue.Queue()
   
   # To use receive from, we go like this:
   # frame, external_address = LinkLayer.l2
   node = Node.Node(1, 'localhost', 5555)
   node.SetMTU(1500)
   node.AddLink((2, 'localhost', 1))
   node.AddLink((3, 'localhost', 1))
   client_address, client_socket = LinkLayer.InitializeSocket(node)
   #print(client_address, client_socket.getsockname())
   some_frame = LinkLayer.Frame()
   some_frame.SetPayload('This is a payload.')
   #node.PrintContents()
   #some_frame.PrintContents()
   LinkLayer.l2_sendto(client_socket, 'localhost', some_frame, node)
   
   #frame_queue.put(result)
   #external_address_queue.put(addr)
   #thread_id = thread.start_new_thread(LinkLayer.l2_recvfrom, (client_socket, node))
   #frame = frame_queue.get()
   #external_address = external_address_queue.get()
   frame, external_address = LinkLayer.l2_recvfrom(client_socket, node)
   frame.PrintContents()
   #LinkLayer.l2_sendto(client_socket, 'localhost', some_frame, node)
   client_socket.close()
   pass
 def test_l2_sendto (self):
   node = Node.Node(1, 'localhost', 5556)
   node.SetMTU(1500)
   node.AddLink((2, 'localhost', 1))
   node.AddLink((3, 'localhost', 1))
   client_address, client_socket = LinkLayer.InitializeSocket(node)
   some_frame = LinkLayer.Frame()
   some_frame.SetPayload('This is a payload.')
   LinkLayer.l2_sendto(client_socket, 'localhost', some_frame, node)
   client_socket.close()
 def test_l2_sendto (self):
   # To use l2_sendto, we go like this:
   # <will add this tomorrow, April 02, 2010>
   node = Node.Node(1, 'localhost', 5556)
   node.SetMTU(1500)
   node.AddLink((2, 'localhost', 1))
   node.AddLink((3, 'localhost', 1))
   client_address, client_socket = LinkLayer.InitializeSocket(node)
   some_frame = LinkLayer.Frame()
   some_frame.SetPayload('This is a payload.')
   LinkLayer.l2_sendto(client_socket, 'localhost', some_frame, node)
   client_socket.close()
def l3_sendto(client_socket, destination_nid, destination_port, DVRP=None, segment=None, node=None):
    """
  This function will be used in Layer 4, the Transport layer. Nowhere in this Layer 3
  is this function used--rather, this layer purely uses l2_sendto from 
  the LinkLayer module.
  """
    # next_hop = DVRP.GetRoutingTable()[destination_nid]
    mtu = node.GetMTU()
    sequence_number = 1
    total_sequence_numbers = 1
    dest_hostname = ResolveNID(destination_nid, node)
    # dest_hostname = socket.gethostbyname(dest_hostname)

    while len(segment) > mtu:
        temp_segment = segment[: mtu - 1]
        datagram = Datagram(
            sequence_number,
            total_sequence_numbers,
            mtu,
            10,
            node.GetNID(),
            node.GetPort(),
            destination_nid,
            destination_port,
            temp_segment,
        )

        what_was_sent = LinkLayer.l2_sendto(client_socket, dest_hostname, datagram, node)
        segment = segment[mtu:]
        sequence_number += 1
        total_sequence_numbers += 1

    if len(segment) > 0:
        datagram = Datagram(
            sequence_number,
            total_sequence_numbers,
            mtu,
            10,
            node.GetNID(),
            node.GetPort(),
            destination_nid,
            destination_port,
            len(segment),
            segment,
        )

        what_was_sent = LinkLayer.l2_sendto(client_socket, dest_hostname, datagram, node)

    return datagram
  def test_communications (self):
    #node = Node.Node(1, 'localhost', 5555)
    #node.SetMTU(1500)
    #node.AddLink((2, 'localhost', 1))
    #node.AddLink((3, 'localhost', 1))
    node = Node.ConfigInitialNetworkTopology(sys.argv[3], int(sys.argv[1]))
    client_address, client_socket = LinkLayer.InitializeSocket(node)
    datagram = NetworkLayer.Datagram()
    datagram.SetMTU(node.GetMTU())

    itc_script = open('itc_test.txt')
    a_list = itc_script.readlines()
    itc_script.close()

    for entry in a_list:
      temp = entry.split(' ')
      if sys.argv[1] == temp[0]:
        datagram.SetSourcePort(int(temp[2]))
      if sys.argv[2] in temp[0]:
        datagram.SetDestPort(int(temp[2]))
    # YOU MUST PUT THE \r\n IN HERE OR IT WILL NOT WORK.
    #datagram.SetPayload('This is a payload.\r\n')
    inputs = [client_socket, sys.stdin]
    #print(what_was_sent.PrintContents())
    go=1
    while(go is 1):
      #sys.stdout.write(str(node.GetNID()) + '> ')
      inputready,outputready,exceptready = select.select(inputs,[],[])
      for s in inputready:
        if s == client_socket:
          length_of_buffer, received_frame, datagram_to_pass, external_address, received_segment = LinkLayer.l2_recvfrom(client_socket, node) # added node as a parameter 04-06-2010.
          print(datagram_to_pass.GetPayload())
        #  received_segment.PrintContents()
        elif s == sys.stdin:
          payload = list(sys.stdin.readline())
          payload.remove('\n')
          payload.append('\r')
          payload.append('\n')
          payload = ''.join(payload)

          if 'exit' in payload:
            print('exiting...')
            go=0

          datagram.SetPayload(payload)
          hn = NetworkLayer.ResolveNID(int(sys.argv[2]), node)
          what_was_sent = LinkLayer.l2_sendto(client_socket, hn, datagram, node)
      #print(length_of_buffer)
      #received_frame.PrintContents()
      #if received_frame.GetLength() > 0:
      #  pass # We need to deal with Layer 3 send here.
      #datagram_to_pass.PrintContents()
      #print(external_address)
    client_socket.close()