package leigh.esp.keikai.sdk;

import leigh.eo.EO;
import leigh.esp.sdk.ESPClient;
import leigh.mecha.log.MechaLogger;
import leigh.mecha.log.MechaLoggerFactory;
import leigh.mecha.util.UniversalJob;

public class KeikaiClient implements Runnable {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(KeikaiClient.class);
    public static String SRC_URI = "srcUri";
    public static String DST_URI = "dstUri";
    private final String kabeServer;
    private final String srcUri;
    private final String dstUri;

    public KeikaiClient(String kabeServer, String srcUri, String dstUri) {
        this.kabeServer = kabeServer;
        this.srcUri = srcUri;
        this.dstUri = dstUri;
    }

    public static void main(String[] args) {
        if (args.length != 3) {
            logger.info("Usage: KeikaiClient kabe_url src_uri dst_url");
            System.exit(UniversalJob.RET_BADARGS);
        }

        KeikaiClient client = new KeikaiClient(args[0], args[1], args[2]);
        client.run();
    }

    @Override
    public void run() {
        ESPClient ctx = new ESPClient(kabeServer);

        EO req = new EO();
        req.setValue(SRC_URI, srcUri);
        req.setValue(DST_URI, dstUri);

        logger.info("Attempting to register service... [kabe: {}] [src: {}] [dst: {}]", kabeServer, srcUri, dstUri);

        while (true) {
            try {

                ctx.request("/keikai/v1", req);
                logger.debug("Done.");
            } catch (Exception e) {
                // Do nothing
            }

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // Do nothing
            }
        }
    }
}
