#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (c) 2017, NORDUnet A/S.
# See LICENSE for licensing information.

import socket
import sys
import signal

def send_message(plopcontrolfile, message):
    plopcontrolport = int(open(plopcontrolfile).read())
    print >>sys.stderr, "plopcontrolport", plopcontrolport
    s = socket.socket(socket.AF_INET)
    print >>sys.stderr, "opened socket"
    s.connect(("127.0.0.1", plopcontrolport))
    print >>sys.stderr, "connected, sending message", message
    s.sendall(message + "\n")
    print >>sys.stderr, "sent message"
    data = ""
    while "\n" not in data:
        data = data + s.recv(1024)
        print >>sys.stderr, "received data"
    data = data.rstrip("\n")
    print >>sys.stderr, "received all data"
    s.close()
    return data

def main():
    signal.alarm(5)
    reply = send_message(sys.argv[1], sys.argv[2])
    print reply

if __name__ == '__main__':
    main()