/*
 * Server.java
 *
 * Created on 15. Juli 2001, 16:52
 */

package org.apache.log4j.rmi.test;

import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
import java.net.MalformedURLException;

/** Unicast remote object implementing java.rmi.Remote interface.
 *
 * @author Alessandro Di Maria
 * @version 1.0
 */
public class Server extends UnicastRemoteObject implements IServer {
    
    private static int hits=0;

    /** Constructs Server object and exports it on default port.
     */
    public Server() throws RemoteException {
        super();
    }

    /** Constructs Server object and exports it on specified port.
     * @param port The port for exporting
     */
    public Server(int port) throws RemoteException {
        super(port);
    }

    public void register(){
        try{      		
            Naming.rebind("//localhost/server",this);
            RemoteLog.main.info("Server bound in rmiRegistery");
        } catch(RemoteException re){
            RemoteLog.main.error("Server not bound in registery",re);
        } catch(MalformedURLException mue){
            RemoteLog.main.error("URL for Server malformed",mue);
        }        
    }

    /** Main method.
     */
    public static void main(String[] args) {
        RemoteLog.config("org/apache/log4j/rmi/test/server.lcf");
        RemoteLog.registerNode();
        System.setSecurityManager(new RMISecurityManager());
        try{
            Server server = new Server();
            server.register();
            RemoteLog.main.info("Server is running");
        } catch (RemoteException re){
            RemoteLog.main.error("Server init failed");
        }
    }
    
    public void receive(String message) throws RemoteException {
        RemoteLog.communicate.info("received message["+message+"]");
        hits+=1;
        RemoteLog.communicate.info("Been hit "+hits+" times");
    }
    
}

