def helper():
  msg = None
  msg = lind_test_server.recv_syscall(sockets[0], 1024, 0)
  assert msg != None, "Sockpair recv failed in helper..."

  lind_test_server.send_syscall(sockets[0], "SocketPair test.", 0)

  return
def helper():
    msg = None
    msg = lind_test_server.recv_syscall(sockets[0], 1024, 0)
    assert msg != None, "Sockpair recv failed in helper..."

    lind_test_server.send_syscall(sockets[0], "SocketPair test.", 0)

    return
Beispiel #3
0
def process_request():
  """
  <Purpose>
    Server thread for processing connections using select ...
  """
  while True:
    #Pass list of Inputs, Outputs for select which returns if any activity
    #occurs on any socket.
    ret = lind_test_server.select_syscall(11, inputs, outputs, excepts, 5)

    #Check for any activity in any of the Input sockets...
    for sock in ret[1]:
      #If the socket returned was listerner socket, then there's a new conn.
      #so we accept it, and put the client socket in the list of Inputs.
      if sock is serversockfd:
        newsockfd = lind_test_server.accept_syscall(sock)
        inputs.append(newsockfd[2])
      #Write to a file...
      elif sock is filefd:
        emultimer.sleep(1)
        assert lind_test_server.write_syscall(sock, 'test') == 4, \
          "Failed to write into a file..."
        lind_test_server.lseek_syscall(sock, 0, SEEK_SET)
        inputs.remove(sock)
      #If the socket is in established conn., then we recv the data, if
      #there's no data, then close the client socket.
      else:
        data = lind_test_server.recv_syscall(sock, 100, 0)
        if data:
          assert data == "test", "Recv failed in select..."
          #We make the ouput ready, so that it sends out data... 
          if sock not in outputs:
            outputs.append(sock)
        else:         
          #No data means remote socket closed, hence close the client socket
          #in server, also remove this socket from readfd's. 
          lind_test_server.close_syscall(sock)
          inputs.remove(sock)
    
    #Check for any activity in any of the output sockets...
    for sock in ret[2]:
      if sock is filefd:
        assert lind_test_server.read_syscall(sock, 4) == "test", \
          "Failed to read from a file..."
        #test for file finished, remove from monitoring.
        outputs.remove(sock)
      else:
        lind_test_server.send_syscall(sock, data, 0)
        #Data is sent out this socket, it's no longer ready for writing
        #remove this socket from writefd's. 
        outputs.remove(sock)

  lind_test_server.close_syscall(serversockfd)
Beispiel #4
0
def process_request():
    """
  <Purpose>
    Server thread for processing connections using select ...
  """
    while True:
        #Pass list of Inputs, Outputs for select which returns if any activity
        #occurs on any socket.
        ret = lind_test_server.select_syscall(11, inputs, outputs, excepts, 5)

        #Check for any activity in any of the Input sockets...
        for sock in ret[1]:
            #If the socket returned was listerner socket, then there's a new conn.
            #so we accept it, and put the client socket in the list of Inputs.
            if sock is serversockfd:
                newsockfd = lind_test_server.accept_syscall(sock)
                inputs.append(newsockfd[2])
            #Write to a file...
            elif sock is filefd:
                emultimer.sleep(1)
                assert lind_test_server.write_syscall(sock, 'test') == 4, \
                  "Failed to write into a file..."
                lind_test_server.lseek_syscall(sock, 0, SEEK_SET)
                inputs.remove(sock)
            #If the socket is in established conn., then we recv the data, if
            #there's no data, then close the client socket.
            else:
                data = lind_test_server.recv_syscall(sock, 100, 0)
                if data:
                    assert data == "test", "Recv failed in select..."
                    #We make the ouput ready, so that it sends out data...
                    if sock not in outputs:
                        outputs.append(sock)
                else:
                    #No data means remote socket closed, hence close the client socket
                    #in server, also remove this socket from readfd's.
                    lind_test_server.close_syscall(sock)
                    inputs.remove(sock)

        #Check for any activity in any of the output sockets...
        for sock in ret[2]:
            if sock is filefd:
                assert lind_test_server.read_syscall(sock, 4) == "test", \
                  "Failed to read from a file..."
                #test for file finished, remove from monitoring.
                outputs.remove(sock)
            else:
                lind_test_server.send_syscall(sock, data, 0)
                #Data is sent out this socket, it's no longer ready for writing
                #remove this socket from writefd's.
                outputs.remove(sock)

    lind_test_server.close_syscall(serversockfd)
Beispiel #5
0
def do_server():
    newsocketfd = lind_test_server.accept_syscall(serversockfd)
    fd = newsocketfd[2]
    lind_test_server.send_syscall(fd, 'jhasdfhjsa', 0)
    lind_test_server.setshutdown_syscall(fd, SHUT_RDWR)

    try:
        # should fail!!!
        lind_test_server.send_syscall(fd, 'jhasdfhjsa', 0)
    except:
        pass
    else:
        print "send after shutdown didn't fail!!!"
def do_server():
    newsocketfd = lind_test_server.accept_syscall(serversockfd)
    fd = newsocketfd[2]
    lind_test_server.send_syscall(fd, 'jhasdfhjsa', 0)
    lind_test_server.setshutdown_syscall(fd, SHUT_RDWR)

    try:
        # should fail!!!
        lind_test_server.send_syscall(fd, 'jhasdfhjsa', 0)
    except:
        pass
    else:
        print "send after shutdown didn't fail!!!"
def client1():
  """
  <Purpose>
    Thread for client 1 to connect to server, and send/recv data...
  """
  lind_test_server.connect_syscall(clientsockfd, '127.0.0.1', 50300)

  lind_test_server.send_syscall(clientsockfd, "test", 0)
  #Short sleeps are not working, give enough time...
  emultimer.sleep(1)
  assert lind_test_server.recv_syscall(clientsockfd, 100, 0) == "test", \
     "Write failed in select while processing client 1..."

  lind_test_server.close_syscall(clientsockfd)
def tester():
  # need simultaneous communication...
  emultimer.createthread(helper)
  emultimer.sleep(0.1)

  lind_test_server.send_syscall(sockets[1], "SocketPair test.", 0)

  msg = None
  msg = lind_test_server.recv_syscall(sockets[1], 1024, 0)
  assert msg != None, "Sockpair recv failed in tester..."

  emultimer.sleep(0.1)

  lind_test_server.close_syscall(sockets[1])
  lind_test_server.close_syscall(sockets[0])

  return
def tester():
    # need simultaneous communication...
    emultimer.createthread(helper)
    emultimer.sleep(0.1)

    lind_test_server.send_syscall(sockets[1], "SocketPair test.", 0)

    msg = None
    msg = lind_test_server.recv_syscall(sockets[1], 1024, 0)
    assert msg != None, "Sockpair recv failed in tester..."

    emultimer.sleep(0.1)

    lind_test_server.close_syscall(sockets[1])
    lind_test_server.close_syscall(sockets[0])

    return
    except Exception, e:
        print 'Test:- W:100 P:50 : ', e

    #Gracefully close the socket
    lind_test_server.close_syscall(newsockfd)


#Run the server in a seperate thread, since client/server should be started
#simultaneously.
emultimer.createthread(process_request)

#connect to server
lind_test_server.connect_syscall(clientsockfd, '127.0.0.1', 50300)

#send each test with some delay, so that server processes each test cleanly.
lind_test_server.send_syscall(clientsockfd, "A" * 100, 0)
emultimer.sleep(0.1)
lind_test_server.send_syscall(clientsockfd, "A" * 100, 0)
emultimer.sleep(0.1)
lind_test_server.send_syscall(clientsockfd, "A" * 100, 0)
emultimer.sleep(0.1)
lind_test_server.send_syscall(clientsockfd, "A" * 50, 0)
emultimer.sleep(0.1)

lind_test_server.close_syscall(clientsockfd)

lind_test_server.connect_syscall(clientsockfd2, emulcomm.getmyip(), 50300)

#send each test with some delay, so that server processes each test cleanly.
lind_test_server.send_syscall(clientsockfd2, "A" * 100, 0)
emultimer.sleep(0.1)
def process_request():
  """
  <Purpose>
    Server thread for processing connections using poll ...
  """
  while True:
    #Pass list of sockets that needs for polling which returns if any activity
    #occurs on any socket.
    poll_vals = lind_test_server.poll_syscall(polled, 5)
    ret = [poll_vals[0],[],[],[]]
      
    ret[1] = [x['fd'] for x in poll_vals[1] if x['revents'] & lind_test_server.POLLIN]
    ret[2] = [x['fd'] for x in poll_vals[1] if x['revents'] & lind_test_server.POLLOUT]

    #Check for any activity in any of the Input sockets...
    for sock in ret[1]:
      
      #If the socket returned was listerner socket, then there's a new conn.
      #so we accept it, and put the client socket in the list of Inputs.
      if sock is serversockfd:
        newsockfd = lind_test_server.accept_syscall(sock)
        new_poll =  {'fd':newsockfd[2], 'events':lind_test_server.POLLIN, 'revents':0}
        polled.append(new_poll)
      #Write to a file...
      elif sock is filefd:
        assert lind_test_server.write_syscall(sock, 'test') == 4, \
          "Failed to write into a file..."
        lind_test_server.lseek_syscall(sock, 0, SEEK_SET)
        #Once the write is successful into a file, Modify the file descriptor
        #so that its ready for reading out of the file.
        [x for x in polled if x['fd'] == sock][0]['events'] = \
          lind_test_server.POLLOUT
      #If the socket is in established conn., then we recv the data, if
      #there's no data, then close the client socket.
      else:
        data = lind_test_server.recv_syscall(sock, 100, 0)
        if data:
          assert data == "test", "Recv failed in select..."
          #This socket is ready for writing, modify the socket descriptor
          #to be in read-write mode. This socket can write data out to network 
          [x for x in polled if x['fd'] == sock][0]['events'] = \
            lind_test_server.POLLIN | lind_test_server.POLLOUT
        else:
          #No data means remote socket closed, hence close the client socket
          #in server, also remove this socket from polling.    
          lind_test_server.close_syscall(sock)
          to_go = [x for x in polled if x['fd'] == sock]
          map(polled.remove, to_go)
    
    #Check for any activity in any of the output sockets...
    for sock in ret[2]:
      if sock is filefd:
        assert lind_test_server.read_syscall(sock, 4) == "test", \
          "Failed to read from a file..."
        #test for file finished, remove from polling.
        to_go = [x for x in polled if x['fd'] == sock]
        map(polled.remove, to_go)
      else:
        lind_test_server.send_syscall(sock, data, 0)
        #Data is sent out of this socket, it's no longer ready for writing,
        #modify it only read mode.
        [x for x in polled if x['fd'] == sock][0]['events'] = \
          lind_test_server.POLLIN

  lind_test_server.close_syscall(serversockfd)
  lind_test_server.connect_syscall(clientsockfd, '127.0.0.1', 50300)

  lind_test_server.send_syscall(clientsockfd, "test", 0)
  #Short sleeps are not working, give enough time...
  emultimer.sleep(1)
  assert lind_test_server.recv_syscall(clientsockfd, 100, 0) == "test", \
     "Write failed in select while processing client 1..."

  lind_test_server.close_syscall(clientsockfd)

#Thread for running client 1, I did so because to show that server can handle
#multiple connections simultaneously...
emultimer.createthread(client1)
emultimer.sleep(.1)

#Client 2 connects to server, and send/recv data...
lind_test_server.connect_syscall(client2sockfd, '127.0.0.1', 50300)

lind_test_server.send_syscall(client2sockfd, "test", 0)
#Short sleeps are not working, give enough time...
emultimer.sleep(1)
assert lind_test_server.recv_syscall(client2sockfd, 100, 0) == "test", \
  "Write failed in select while processing client 2..."

lind_test_server.close_syscall(client2sockfd)

#Exit all threads, no better way to close server thread which is in infinity
#loop...
emulmisc.exitall()

#Both client and server are run from this file, hence opening sockets for both
listensockfd = lind_test_server.socket_syscall(AF_INET, SOCK_DGRAM, 0)
sendsockfd = lind_test_server.socket_syscall(AF_INET, SOCK_DGRAM, 0)

#Bind the socket to an address, this is important in repy, because the recvfrom
#_syscall() doesn't work properly without binding the address first.
lind_test_server.bind_syscall(listensockfd, '127.0.0.1', 50300)

def process_request():
  msg = None
  # Read the data in the socket.
  try:
    msg = lind_test_server.recvfrom_syscall(listensockfd, 1024, 0)
    assert msg != None, "Socket failed to recv message."
  except Exception, e:
    print "UDP Connect Test : ", e

#Run the listen socket in seperate thread, since send/listen should be started
#simultaneously.
emultimer.createthread(process_request)

emultimer.sleep(0.1)

#This is another way to send message in UDP, instead of sendto_syscall().
lind_test_server.connect_syscall(sendsockfd, '127.0.0.1', 50300)
lind_test_server.send_syscall(sendsockfd, "UDP Connect Test", 0)

#close send & listen sockets...
lind_test_server.close_syscall(listensockfd)
lind_test_server.close_syscall(sendsockfd)
Beispiel #14
0
sendsockfd = lind_test_server.socket_syscall(AF_INET, SOCK_DGRAM, 0)

#Bind the socket to an address, this is important in repy, because the recvfrom
#_syscall() doesn't work properly without binding the address first.
lind_test_server.bind_syscall(listensockfd, '127.0.0.1', 50300)


def process_request():
    msg = None
    # Read the data in the socket.
    try:
        msg = lind_test_server.recvfrom_syscall(listensockfd, 1024, 0)
        assert msg != None, "Socket failed to recv message."
    except Exception, e:
        print "UDP Connect Test : ", e


#Run the listen socket in seperate thread, since send/listen should be started
#simultaneously.
emultimer.createthread(process_request)

emultimer.sleep(0.1)

#This is another way to send message in UDP, instead of sendto_syscall().
lind_test_server.connect_syscall(sendsockfd, '127.0.0.1', 50300)
lind_test_server.send_syscall(sendsockfd, "UDP Connect Test", 0)

#close send & listen sockets...
lind_test_server.close_syscall(listensockfd)
lind_test_server.close_syscall(sendsockfd)
  try:
    msg = lind_test_server.recvfrom_syscall(newsockfd, 100, MSG_PEEK)
    assert len(msg[2]) == 50, "Length expected 50 bytes to peek, but only" \
      + " got " + str(len(msg[2])) + " bytes."
  except Exception, e:
    print 'Test:- W:100 P:50 : ', e
  
  #Gracefully close the socket
  lind_test_server.close_syscall(newsockfd)

#Run the server in a seperate thread, since client/server should be started
#simultaneously.
emultimer.createthread(process_request)

#connect to server
lind_test_server.connect_syscall(clientsockfd, '127.0.0.1', 50300)

#send each test with some delay, so that server processes each test cleanly.
lind_test_server.send_syscall(clientsockfd, "A" * 100, 0)
emultimer.sleep(0.1)
lind_test_server.send_syscall(clientsockfd, "A" * 100, 0)
emultimer.sleep(0.1)
lind_test_server.send_syscall(clientsockfd, "A" * 100, 0)
emultimer.sleep(0.1)
lind_test_server.send_syscall(clientsockfd, "A" * 50, 0)
emultimer.sleep(0.1)

#close the client & server sockets...  
lind_test_server.close_syscall(clientsockfd)
lind_test_server.close_syscall(serversockfd)