#!/usr/bin/python
#coding: utf-8
'''In our topology, we assume the container running this script has ip address 10.10.10.10
   (useful information according to our topology: useless otherwise)
'''
from time import sleep
from flask import Flask, jsonify
import requests
from threading import Thread

app = Flask(__name__)

SVR_IP = '10.0.0.1' #name = srv

GWI1_IP = '10.2.2.1' #name = gwi1
GWI2_IP = '10.2.2.2' #name = gwi2 (dc)

GWF1_IP = '10.0.1.100' #name = gwf1
DEV1_GWF1_IP = '10.0.1.1' #name = dev1gwf1
DEV2_GWF1_IP = '10.0.1.2' #name = dev2gwf1
DEV3_GWF1_IP = '10.0.1.3' #name = dev3gwf1

GWF2_IP = '10.0.2.100' #name = gwf2
DEV1_GWF2_IP = '10.0.2.1' #name = dev1gwf2
DEV2_GWF2_IP = '10.0.2.2' #name = dev2gwf2
DEV3_GWF2_IP = '10.0.2.3' #name = dev3gwf2

GWF3_IP = '10.0.3.100' #name = gwf3
DEV1_GWF3_IP = '10.0.3.1' #name = dev1gwf3
DEV2_GWF3_IP = '10.0.3.2' #name = dev2gwf3
DEV3_GWF3_IP = '10.0.3.3' #name = dev3gwf3

'''
Formats of configs:
SERVER =    "--local_ip 127.0.0.1 --local_port 8080 --local_name %s"
GATEWAY_I = "--local_ip 127.0.0.1 --local_port 8181 --local_name %s --remote_ip %s --remote_port 8080 --remote_name %s"
GATEWAY_F = "--local_ip 127.0.0.1​ --local_port 8282 --local_name %s --remote_ip %s --remote_port 8181 --remote_name %s"
DEV =       "--local_ip 127.0.0.1​ --local_port 9001 --local_name %s --remote_ip %s --remote_port 8282 --remote_name %s --send_period 3000"
'''

SVR_READY   = False
GWI1_READY  = False
GWI2_READY  = False
GWF1_READY  = False
GWF2_READY  = False
GWF3_READY  = False

@app.route("/getmyconfig/<string:my_ip>")
def configurations_giver(my_ip="127.0.0.1"):
    global SVR_READY
    global GWI1_READY
    global GWI2_READY
    global GWF1_READY
    global GWF2_READY
    global GWF3_READY
    configs = {"local_ip":my_ip, "verdict":"oops"}
    if my_ip==SVR_IP:
        configs["local_port"] = 8080
        configs["local_name"] = "srv"
        configs["verdict"]    = "yes"
        SVR_READY = True
    elif my_ip==GWI1_IP:
        if SVR_READY:
            configs["local_port"]  = 8181
            configs["local_name"]  = "gwi1"
            configs["remote_ip"]   = SVR_IP
            configs["remote_port"] = 8080
            configs["remote_name"] = "srv"
            configs["verdict"]     = "yes"
            GWI1_READY = True
    elif my_ip==GWF1_IP:
        if GWI1_READY:
            configs["local_port"]  = 8282
            configs["local_name"]  = "gwf1"
            configs["remote_ip"]   = GWI1_IP
            configs["remote_port"] = 8181
            configs["remote_name"] = "gwi1"
            configs["verdict"]     = "yes"
            GWF1_READY = True
    elif my_ip==GWF2_IP:
        if GWI1_READY:
            configs["local_port"]  = 8282
            configs["local_name"]  = "gwf2"
            configs["remote_ip"]   = GWI1_IP
            configs["remote_port"] = 8181
            configs["remote_name"] = "gwi1"
            configs["verdict"]     = "yes"
            GWF2_READY = True
    elif my_ip==GWF3_IP:
        if GWI1_READY:
            configs["local_port"]  = 8282
            configs["local_name"]  = "gwf3"
            configs["remote_ip"]   = GWI1_IP
            configs["remote_port"] = 8181
            configs["remote_name"] = "gwi1"
            configs["verdict"]     = "yes"
            GWF3_READY = True
    elif my_ip==DEV1_GWF1_IP:
        if GWF1_READY:
            configs["local_port"]  = 9001
            configs["local_name"]  = "dev1gwf1"
            configs["remote_ip"]   = GWF1_IP
            configs["remote_port"] = 8282
            configs["remote_name"] = "gwf1"
            configs["send_period"] = 3000
            configs["verdict"]     = "yes"
    elif my_ip==DEV2_GWF1_IP:
        if GWF1_READY:
            configs["local_port"]  = 9001
            configs["local_name"]  = "dev2gwf1"
            configs["remote_ip"]   = GWF1_IP
            configs["remote_port"] = 8282
            configs["remote_name"] = "gwf1"
            configs["send_period"] = 3000
            configs["verdict"]     = "yes"
    elif my_ip==DEV3_GWF1_IP:
        if GWF1_READY:
            configs["local_port"]  = 9001
            configs["local_name"]  = "dev3gwf1"
            configs["remote_ip"]   = GWF1_IP
            configs["remote_port"] = 8282
            configs["remote_name"] = "gwf1"
            configs["send_period"] = 3000
            configs["verdict"]     = "yes"
    elif my_ip==DEV1_GWF2_IP:
        if GWF2_READY:
            configs["local_port"]  = 9001
            configs["local_name"]  = "dev1gwf2"
            configs["remote_ip"]   = GWF2_IP
            configs["remote_port"] = 8282
            configs["remote_name"] = "gwf2"
            configs["send_period"] = 3000
            configs["verdict"]     = "yes"
    elif my_ip==DEV2_GWF2_IP:
        if GWF2_READY:
            configs["local_port"]  = 9001
            configs["local_name"]  = "dev2gwf2"
            configs["remote_ip"]   = GWF2_IP
            configs["remote_port"] = 8282
            configs["remote_name"] = "gwf2"
            configs["send_period"] = 3000
            configs["verdict"]     = "yes"
    elif my_ip==DEV3_GWF2_IP:
        if GWF2_READY:
            configs["local_port"]  = 9001
            configs["local_name"]  = "dev3gwf2"
            configs["remote_ip"]   = GWF2_IP
            configs["remote_port"] = 8282
            configs["remote_name"] = "gwf2"
            configs["send_period"] = 3000
            configs["verdict"]     = "yes"
    elif my_ip==DEV1_GWF3_IP:
        if GWF3_READY:
            configs["local_port"]  = 9001
            configs["local_name"]  = "dev1gwf3"
            configs["remote_ip"]   = GWF3_IP
            configs["remote_port"] = 8282
            configs["remote_name"] = "gwf3"
            configs["send_period"] = 3000
            configs["verdict"]     = "yes"
    elif my_ip==DEV2_GWF3_IP:
        if GWF3_READY:
            configs["local_port"]  = 9001
            configs["local_name"]  = "dev2gwf3"
            configs["remote_ip"]   = GWF3_IP
            configs["remote_port"] = 8282
            configs["remote_name"] = "gwf3"
            configs["send_period"] = 3000
            configs["verdict"]     = "yes"
    elif my_ip==DEV3_GWF3_IP:
        if GWF3_READY:
            configs["local_port"]  = 9001
            configs["local_name"]  = "dev3gwf3"
            configs["remote_ip"]   = GWF3_IP
            configs["remote_port"] = 8282
            configs["remote_name"] = "gwf3"
            configs["send_period"] = 3000
            configs["verdict"]     = "yes"
    return jsonify(configs)

@app.route("/getmyconfig/")
def configurations_giver_to_dc():
    global GWI2_READY
    configs = {"local_ip":GWI2_IP, "verdict":"oops"}
    if SVR_READY:
        configs["local_port"]  = 8181
        configs["local_name"]  = "gwi2"
        configs["remote_ip"]   = SVR_IP
        configs["remote_port"] = 8080
        configs["remote_name"] = "srv"
        configs["verdict"] = "yes"
        GWI2_READY = True
    return jsonify(configs)

def registergwfs():
    while not GWI2_READY:
        sleep(3)
    #sleep(5)
    requests.post('http://172.17.0.17:8181/gateways/register', json={"Name": "gwf2", "PoC":"http://10.0.2.100"})
    requests.post('http://172.17.0.17:8181/gateways/register', json={"Name": "gwf3", "PoC":"http://10.0.3.100"})

if __name__=='__main__':
    registrar = Thread(target=registergwfs)
    registrar.start()
    app.run(debug=False, host='0.0.0.0', port=5555)

