/*****************************************************************************
 * Main.java FVLC VLC Java RMI Binding
 *****************************************************************************
 *
 * @author : Festastic, <info@festastic.com>
 *
 * This program is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *
 */

package com.festastic.fvlc;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import com.festastic.fvlc.rmi.FVLCPlayer;
import com.festastic.fvlc.rmi.FVLCPlayerImpl;
import com.festastic.fvlc.rmi.FVLMPlayer;
import com.festastic.fvlc.rmi.FVLMPlayerImpl;

public class Main {

    /**
     * Main method. Initialize JVLC and VLM instance, bind to Java RMI Registry
     * and wait for client request.
     *
     * @param args
     *            {String path, int port(optional)}
     */
    public static void main(String[] args) {
        String path;
        int port = 1099;

        // exit if args weren't given enough.
        if (args == null || args.length < 1) {
            System.out.println("Usage : jvlc.jar [VLC library path], [Java RMI Port (Optional)]");
            return;
        }

        path = args[0];

        // set port number if it is given.
        if (args.length < 1) {
            try {
                port = Integer.parseInt(args[1]);
                if (port < 0 || 65536 < port) {
                    System.err.println("Port number must be between 1 to 65535.");
                    return;
                }
            } catch (NumberFormatException e) {
                System.err.println("Port must be a number.");
                return;
            }
        }

        try {
            FVLCPlayer vlc = new FVLCPlayerImpl();
            FVLMPlayer vlm = new FVLMPlayerImpl();

            // create java rmi registry and bind remote objects
            Registry reg = LocateRegistry.createRegistry(port);
            reg.bind("FVLCPlayer", vlc);
            reg.bind("FVLMPlayer", vlm);

            // initialize remote objects
            ((FVLCPlayerImpl) vlc).init(path);
            ((FVLMPlayerImpl) vlm).init(((FVLCPlayerImpl) vlc).getVLC());

        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("An error occered. System exiting...");
        }
    }
}
