"""
TODO fill this in

Test to make sure that Controller.get_connection works as expected

"""

include tcp.repy

def start_server():
  server = CTLR.get_connection(PORT2)
  server.listen()
  server.accept()
  
  received = server.recv(MAXLEN)
  if received != MESSAGE:
    print "Didnt receive expected message. Expected '%s', got '%s'" % (MESSAGE, received)
  server.disconnect()


def start_client():
  client = CTLR.get_connection(PORT1)
  client.connect(IP, PORT2)
  client.send(MESSAGE);
  client.disconnect()
  
  
if callfunc == 'initialize':
  IP = getmyip()
  CTLR_PORT  = 12345
  PORT1 = 12345
  PORT2 = 12346
  MAXLEN = 4096

  MESSAGE = "hello!"
  
  CTLR = Controller()
  CTLR.start(IP, CTLR_PORT)
  


  #set up a normal controller to listen
  settimer(0, start_server, ())

  start_client()

  exitall()


