include tcp.repy

if callfunc == 'initialize':
  IP = '127.0.0.1' #  getmyip()
  PORT = 12345
  fn = "seattle.txt"
  fnout = "junk_test.out" # write if error
  fobj = open(fn, "r")
  MESSAGE = fobj.read()
  fobj.close()
  MAXLEN = 1000000 # all of it

  socket = Connection()
  socket.bind(IP, PORT)


def server():
  tries = 100
  while tries:
    mycontext['mess'] += socket.recv(MAXLEN)
    if mycontext['mess'] == MESSAGE:
      break
    tries -= 1
    sleep(.01)

  if mycontext['mess'] != MESSAGE:
    print "%s != " % mycontext['mess']
    print "%s" % MESSAGE
    fobj = open(fnout, "w")
    fobj.write(mycontext['mess'])
    fobj.close()

def client():
  socket.connect(IP, PORT)

  bytes = socket.send(MESSAGE)
  if bytes == 0:
    print "Expected some bytes"


def main():
  mycontext['mess'] = ""

  settimer(0, client, ())
  sleep(.01) 
  server()

  socket.disconnect()


if callfunc == 'initialize':
  main()
  exitall()
