/**
 * Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies
 *
 * Please see distribution for license.
 */
package com.opengamma.component;

import java.net.InetAddress;
import java.net.DatagramSocket;
import java.net.DatagramPacket;
import java.net.SocketException;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CommandMonitor extends Thread {
  private static final Logger s_logger = LoggerFactory.getLogger(ComponentManager.class);
  private static final String SECRET_PROPERTY="commandmonitor.secret";
  private static final String PORT_PROPERTY="commandmonitor.port";
  private static final String DEFAULT_SECRET="OpenGamma";
  private static final int DEFAULT_PORT=8079;
  private static final int COMMAND_LENGTH=256;

  private ComponentRepository _repo;
  private String _secret;
  private int _port;
  private DatagramSocket _socket;

  private CommandMonitor(ComponentRepository repo, String secret, int port) {
    _repo = repo;
    _secret = secret;
    _port = port;

    setDaemon(true);
    setName("CommandMonitor");
    try {
      _socket = new DatagramSocket(port, InetAddress.getLoopbackAddress());
    } catch (SocketException e) {
      s_logger.warn("Failed to create listening socket, CommandMonitor will not be available");
      return;
    }

    this.start();
  }

  @Override
  public void run() {
    while(true) {
      byte[] buffer = new byte[COMMAND_LENGTH];
      DatagramPacket packet = new DatagramPacket(buffer,buffer.length);
      try {
      _socket.receive(packet);
      } catch (IOException e) {
        s_logger.warn("Error while receiving command packet");
        continue;
      }
      String received = new String(packet.getData(), 0, packet.getLength());

      if(!received.matches("secret:"+_secret+"\\s+command:\\w+\\s*")) {
        s_logger.debug("Malformed command or wrong secret");
        continue;
      }

      String command=received.replaceAll("secret:"+_secret+"\\s+command:(\\w+)\\s*","$1");
      s_logger.debug("Received command \"{}\"", command);

      if(command.equals("stop")) {
        /* What do we do if the repo isn't running? */
        if(_repo.isRunning()) {
          _repo.stop();
        }
      } else if(command.equals("exit")) {
        if(_repo.isRunning()) {
          _repo.stop();
        }
        System.exit(0);
      } else {
        s_logger.debug("Unknown command \"{}\"", command);
      }
    }
  }

  public static void create(ComponentRepository repo) {
    String secret = System.getProperty(SECRET_PROPERTY, DEFAULT_SECRET);
    String port = System.getProperty(PORT_PROPERTY, Integer.toString(DEFAULT_PORT));
    new CommandMonitor(repo, secret, Integer.parseInt(port));
  }

  public static void create(ComponentRepository repo, String secret, int port) {
    new CommandMonitor(repo, secret, port);
  }

  public static void main(String[] args) throws IOException {
    byte[] buffer = new byte[COMMAND_LENGTH];
    InetAddress address = InetAddress.getLoopbackAddress();

    String secret = System.getProperty(SECRET_PROPERTY, DEFAULT_SECRET);
    String port = System.getProperty(PORT_PROPERTY, Integer.toString(DEFAULT_PORT));
    String command = (args.length==1)?args[0]:"exit";

    String toSend = "secret:" + secret + " command:" + command + "\n";
    byte[] sBuffer = toSend.getBytes();

    System.out.println("Sending "+toSend);
    try {
      DatagramSocket socket = new DatagramSocket();
      DatagramPacket packet = new DatagramPacket(sBuffer, sBuffer.length, address, Integer.parseInt(port));
      socket.send(packet);
    } catch (Exception e) {
      System.out.println("Send failed...");
    }
  }
}

@@ -287,24 +287,62 @@
 tException;%0A
+import java.net.UnknownHostException;%0A
 import java.
@@ -1214,32 +1214,34 @@
 ress.get
-LoopbackAddress(
+ByName(%22127.0.0.1%22
 ));%0A    
@@ -1249,22 +1249,16 @@
  catch (
-Socket
 Exceptio
@@ -2979,16 +2979,37 @@
 xception
+,UnknownHostException
  %7B%0A    b
@@ -3094,24 +3094,26 @@
 .get
-LoopbackAddress(
+ByName(%22127.0.0.1%22
 );%0A%0A

Remove java 7 api calls