Newer
Older
ServerTester / src / main / java / info / istlab / ServerTester / App.java
package info.istlab.ServerTester;

import java.lang.reflect.InvocationTargetException;
import java.net.InetAddress;
import java.net.NetworkInterface;

public class App {
    public static boolean isWindows;
    public static String userhome;
    public static String userdir;
    public static boolean isJAR = false;
    public static String ipAddr;
    public static NetworkInterface primaryInterface;
    public static InetAddress primaryInetAddr;

    static {
        String os = System.getProperty("os.name").toLowerCase();
        isWindows = os.contains("windows");
        userhome = System.getProperty("user.home");
        userdir = System.getProperty("user.dir");
    }

    public static void main(String[] args) {
        Class<?> clazz = App.class; // Mainクラスを指定
        String location = clazz.getProtectionDomain().getCodeSource().getLocation().getPath();
        isJAR = location.endsWith(".jar");

        // ネットワークインタフェースの調査と選択
        primaryInterface = NetworkInterfaceSelector.getPrimaryInterface();
        if (primaryInterface == null) {
            System.out.println("ネットワークインタフェースが見つかりませんでした。");
            System.exit(1);
        }
        primaryInetAddr = NetworkInterfaceSelector.getPrimaryInetAddr(primaryInterface);

        Host myhost = new Host();

        if (args.length > 0) { // 実行時の引数で Web とか Time とか指定された場合、動的にサーバを生成
            for (String arg : args) {
                System.out.println("実行時の引数で " + arg+" が指定されました。"+arg+"Server を起動します。");
                try {
                    Object serv = Class.forName("info.istlab.ServerTester." + arg+"Server").getConstructor(String.class)
                    .newInstance("0.0.0.0");
                    if (serv instanceof SimpleWebSocketServer) {
                        myhost.addPanel(new ServerPanel4WS((SimpleWebSocketServer) serv, myhost));
                    } else {
                        myhost.addPanel(new ServerPanel((Server) serv, myhost));
                    }
                } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
                        | InvocationTargetException | NoSuchMethodException | SecurityException
                        | ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        } else {

            WebServer web = new WebServer(8081);
            myhost.addPanel(new ServerPanel(web, myhost));

            WebServer web2 = new WebServer(8081, "0.0.0.0");
            myhost.addPanel(new ServerPanel(web2, myhost));

            TimeServer time = new TimeServer(10123, "127.0.0.1");
            myhost.addPanel(new ServerPanel(time, myhost));

            ThreadTimeServer ttime = new ThreadTimeServer(11123);
            myhost.addPanel(new ServerPanel(ttime, myhost));

            EchoServer echo = new EchoServer(10008, "127.0.0.1");
            myhost.addPanel(new ServerPanel(echo, myhost));

            ThreadEchoServer techo = new ThreadEchoServer(11008);
            myhost.addPanel(new ServerPanel(techo, myhost));

            WhiteBoardServer wb = new WhiteBoardServer(11111, "0.0.0.0");
            myhost.addPanel(new ServerPanel(wb, myhost));

            SimpleWebSocketServer ws = new SimpleWebSocketServer(8887, "0.0.0.0");
            myhost.addPanel(new ServerPanel4WS(ws, myhost));
        }

        // 近隣のホスト情報を集めるUDPサーバ
        // myhost.setUdpServer(new UDPServer(54320));
        myhost.setMulticastReceiver(new MulticastReceiver(54320));
    }
}