//# 0 errors, 46 messages
//#
/*
    //#RemoteServer.java:1:1: class: com.dmdirc.commandline.RemoteServer
 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.dmdirc.commandline;

import com.dmdirc.logger.ErrorLevel;
import com.dmdirc.logger.Logger;
import com.dmdirc.util.IrcAddress;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.List;

/**
 * An RMI server that allows other clients to interact with DMDirc.
 * 
 * @author chris
 */
public class RemoteServer implements RemoteInterface {
    
    /** The minimum port to use for RMI binding. */
    private static final int MINPORT = 3634;
    /** The maximum port to use for RMI binding. */
    private static final int MAXPORT = MINPORT + 5;
    /** The interface we're exposing. */
    private static final RemoteServer SERVER = new RemoteServer();
    //#RemoteServer.java:48: method: com.dmdirc.commandline.RemoteServer.com.dmdirc.commandline.RemoteServer__static_init
    //#output(com.dmdirc.commandline.RemoteServer__static_init): SERVER
    //#output(com.dmdirc.commandline.RemoteServer__static_init): __Descendant_Table[com/dmdirc/commandline/RemoteServer]
    //#output(com.dmdirc.commandline.RemoteServer__static_init): __Dispatch_Table.connect(Ljava/util/List;)V
    //#output(com.dmdirc.commandline.RemoteServer__static_init): com/dmdirc/commandline/RemoteInterface.__Descendant_Table[com/dmdirc/commandline/RemoteServer]
    //#output(com.dmdirc.commandline.RemoteServer__static_init): new RemoteServer(RemoteServer__static_init#1) num objects
    //#output(com.dmdirc.commandline.RemoteServer__static_init): SERVER.__Tag
    //#new obj(com.dmdirc.commandline.RemoteServer__static_init): new RemoteServer(RemoteServer__static_init#1)
    //#post(com.dmdirc.commandline.RemoteServer__static_init): SERVER == &new RemoteServer(RemoteServer__static_init#1)
    //#post(com.dmdirc.commandline.RemoteServer__static_init): __Descendant_Table[com/dmdirc/commandline/RemoteServer] == &__Dispatch_Table
    //#post(com.dmdirc.commandline.RemoteServer__static_init): com/dmdirc/commandline/RemoteInterface.__Descendant_Table[com/dmdirc/commandline/RemoteServer] == &__Dispatch_Table
    //#post(com.dmdirc.commandline.RemoteServer__static_init): __Dispatch_Table.connect(Ljava/util/List;)V == &connect
    //#post(com.dmdirc.commandline.RemoteServer__static_init): new RemoteServer(RemoteServer__static_init#1) num objects == 1
    //#post(com.dmdirc.commandline.RemoteServer__static_init): SERVER.__Tag == com/dmdirc/commandline/RemoteServer
    //#RemoteServer.java:48: end of method: com.dmdirc.commandline.RemoteServer.com.dmdirc.commandline.RemoteServer__static_init
    
    /**
     * Creates a new instance of RemoteServer.
     */
    public RemoteServer() {
        super();
    //#RemoteServer.java:54: method: void com.dmdirc.commandline.RemoteServer.com.dmdirc.commandline.RemoteServer()
    }
    //#RemoteServer.java:55: end of method: void com.dmdirc.commandline.RemoteServer.com.dmdirc.commandline.RemoteServer()
    
    /** {@inheritDoc} */
    @Override
    public void connect(final List<IrcAddress> addresses) throws RemoteException {
        for (IrcAddress address : addresses) {
    //#RemoteServer.java:60: method: void com.dmdirc.commandline.RemoteServer.connect(List)
    //#input(void connect(List)): addresses
    //#pre[1] (void connect(List)): addresses != null
    //#presumption(void connect(List)): java.util.Iterator:next(...)@60 != null
    //#test_vector(void connect(List)): java.util.Iterator:hasNext(...)@60: {0}, {1}
            address.connect();
    //#RemoteServer.java:61: Warning: method not available - call not analyzed
    //#    call on void com.dmdirc.util.IrcAddress:connect()
    //#    severity: INFORMATIONAL
    //#    class: com.dmdirc.commandline.RemoteServer
    //#    method: void connect(List)
    //#    unanalyzed callee: void com.dmdirc.util.IrcAddress:connect()
        }
    }    
    //#RemoteServer.java:63: end of method: void com.dmdirc.commandline.RemoteServer.connect(List)
    
    /**
     * Binds to the RMI registry so that other clients may find this remote
     * server.
     */
    public static void bind() {
        RemoteInterface stub;
        
        try {
            stub = (RemoteInterface) UnicastRemoteObject.exportObject(SERVER, 0);
    //#RemoteServer.java:73: method: void com.dmdirc.commandline.RemoteServer.bind()
    //#input(void bind()): SERVER
    //#input(void bind()): com.dmdirc.logger.ErrorLevel.MEDIUM
    //#input(void bind()): com/dmdirc/commandline/RemoteInterface.__Descendant_Table[com/dmdirc/commandline/RemoteInterface]
    //#input(void bind()): com/dmdirc/commandline/RemoteInterface.__Descendant_Table[com/dmdirc/commandline/RemoteServer]
    //#input(void bind()): com/dmdirc/commandline/RemoteInterface.__Descendant_Table[others]
    //#presumption(void bind()): init'ed(com.dmdirc.logger.ErrorLevel.MEDIUM)
    //#presumption(void bind()): java.rmi.registry.LocateRegistry:createRegistry(...)@81 != null
    //#presumption(void bind()): java.rmi.server.UnicastRemoteObject:exportObject(...).__Tag@73 in {com/dmdirc/commandline/RemoteInterface, com/dmdirc/commandline/RemoteServer}
        } catch (RemoteException ex) {
            Logger.appError(ErrorLevel.MEDIUM, "Unable to export remote interface", ex);
    //#RemoteServer.java:75: Warning: method not available - call not analyzed
    //#    call on void com.dmdirc.logger.Logger:appError(ErrorLevel, String, Throwable)
    //#    severity: INFORMATIONAL
    //#    class: com.dmdirc.commandline.RemoteServer
    //#    method: void bind()
    //#    unanalyzed callee: void com.dmdirc.logger.Logger:appError(ErrorLevel, String, Throwable)
            return;
        }
        
        for (int port = MINPORT; port < MAXPORT; port++) {
            try {
                final Registry registry = LocateRegistry.createRegistry(port);
                registry.rebind("DMDirc", stub);
                return;
            } catch (RemoteException ex) {
                continue;
            }
        }
    }
    //#RemoteServer.java:88: end of method: void com.dmdirc.commandline.RemoteServer.bind()
    
    /**
     * Retrieves a reference to an existing RemoteServer, if there is one.
     * Note that this must be called before bind(), unless you want a reference
     * to our own client for some reason.
     * 
     * @return The RemoteServer instance, or null if none was available
     */
    public static RemoteInterface getServer() {
        for (int port = MINPORT; port < MAXPORT; port++) {
    //#RemoteServer.java:98: method: RemoteInterface com.dmdirc.commandline.RemoteServer.getServer()
    //#input(RemoteInterface getServer()): com/dmdirc/commandline/RemoteInterface.__Descendant_Table[com/dmdirc/commandline/RemoteInterface]
    //#input(RemoteInterface getServer()): com/dmdirc/commandline/RemoteInterface.__Descendant_Table[com/dmdirc/commandline/RemoteServer]
    //#input(RemoteInterface getServer()): com/dmdirc/commandline/RemoteInterface.__Descendant_Table[others]
    //#output(RemoteInterface getServer()): return_value
    //#presumption(RemoteInterface getServer()): java.rmi.registry.LocateRegistry:getRegistry(...)@100 != null
    //#presumption(RemoteInterface getServer()): java.rmi.registry.Registry:lookup(...).__Tag@101 in {com/dmdirc/commandline/RemoteInterface, com/dmdirc/commandline/RemoteServer}
    //#post(RemoteInterface getServer()): init'ed(return_value)
            try {
                final Registry registry = LocateRegistry.getRegistry("localhost", port);
                final RemoteInterface iface = (RemoteInterface) registry.lookup("DMDirc");
                
                if (iface == null) {
                    continue;
                } else {
                    return iface;
                }
            } catch (RemoteException ex) {
                continue;
            } catch (NotBoundException ex) {
                continue;
            }
        }
        
        // No RMI server found
        return null;
    //#RemoteServer.java:116: end of method: RemoteInterface com.dmdirc.commandline.RemoteServer.getServer()
    }

}
    //#RemoteServer.java:: end of class: com.dmdirc.commandline.RemoteServer
