/**
 * MulticastServerTR72W.java
 * copyright Koji Wada 2005
 */
import java.net.*;
import java.awt.*;
import java.io.*;
import javax.swing.*;

public class MulticastServerTR72W {
  public static final String MCAST_ADDRESS = "224.0.1.1";	// マルチキャスト
  public static int MCAST_PORT = 10011;	                    // ポート番号
  public static final int PACKET_SIZE = 1024;
  public static final String ENCODING = "EUCJIS";
  public static MulticastSocket socket = null;
  public static String ServerIP = "";
  public static String ip = "";
  public static InetAddress mcastAddress;
  public static double th = 0, hy = 0;
  static String option = "";
  
  public static void main(String args[]) {
    // hostip.dat から TR72W の IP を取得
    try {
      FileInputStream fp = new FileInputStream("hostip.dat");
      InputStreamReader ir = new InputStreamReader(fp);
      BufferedReader br = new BufferedReader(ir);
      ip = br.readLine();
      fp.close();
    } catch(Exception e) {
      e.printStackTrace();
    }
    if (args.length > 0) {
      //System.out.println(args[0]);
      option = args[0];
    }
    SendData sd = new SendData();
    Thread t = new Thread((Runnable) sd);
    t.start();
    try {
      InetAddress host = InetAddress.getLocalHost();
      byte[]addr = host.getAddress();
      for (int i = 0; i < addr.length; i++) {
        int ub = addr[i] < 0 ? addr[i] + 256 : addr[i];
        ServerIP += ServerIP.equals("") ? "" : ".";
        ServerIP += Integer.toString(ub);
      }
    } catch(IOException e) {
      e.printStackTrace();
    }
    
    while (true) {
      try {
        //マルチキャスト・ソケットを作成
        socket = new MulticastSocket(MCAST_PORT);
        mcastAddress =
          InetAddress.getByName(MCAST_ADDRESS);
        if (option.equals("-d")) {
          System.out.println ("MulticastServerTR72W Server started. (ServerIP="+ MCAST_ADDRESS + " port=" + socket.getLocalPort() + ")");
        }
        DatagramPacket sendPacket;
        // 参加開始
        try {
          socket.joinGroup(mcastAddress);
          String joinMessage = ServerIP + " " + "HOST";
          byte[]buf = joinMessage.getBytes(ENCODING);
          sendPacket = new DatagramPacket(buf, buf.length, mcastAddress, MCAST_PORT);
          socket.send(sendPacket);
        }
        catch(IOException e) {
          e.printStackTrace();
        }
        
        // 送信開始
        byte buf[] = new byte[PACKET_SIZE];
        while (true) {
          DatagramPacket receivePacket = new DatagramPacket(buf, buf.length);
          socket.receive(receivePacket);
          //受信データグラム・パケットの内容
          String receiveMessage = new String(buf, 0, receivePacket.getLength(), ENCODING);
          //受信したデータの表示
          if (option.equals("-d")) {
            System.out.println(receiveMessage);
          }
        }
      }
      catch(IOException e) {
        e.printStackTrace();
      }
      finally {
        if (socket != null) {
          socket.close();
        }
      }
    }
  }
  
  public static class SendData implements Runnable {
    public void SendData() {
      String sendMessage;
      DatagramPacket sendPacket;
      try {
        sendMessage = ServerIP + " " + "DATA" + " " + Double.toString(th) + " " +  Double.toString(hy);;
        byte[] buf = sendMessage.getBytes(ENCODING);
        sendPacket = new DatagramPacket(buf, buf.length, mcastAddress, MCAST_PORT);
        try {
          socket.send(sendPacket);
        } catch    (IOException e) {
          e.printStackTrace();
        }
      } catch(IOException e) {
        e.printStackTrace();
      }
    }

    public void run() {
      while (true) {
        th = UrlRead.getdata(ip, "T");
        hy = UrlRead.getdata(ip, "H");
        /*
         * DEBUG
         * th = (int)(100 * Math.random());
		 * hy = (int)(100 * Math.random());
		 * System.out.println(rr + " " + ss);
		 */
        this.SendData();
        try {
          Thread.sleep(1000);
        }
        catch(InterruptedException e) {
        }
      }
    }
  }
}

//EOF 
