Example usage for java.rmi.server RemoteServer getClientHost

List of usage examples for java.rmi.server RemoteServer getClientHost

Introduction

In this page you can find the example usage for java.rmi.server RemoteServer getClientHost.

Prototype

public static String getClientHost() throws ServerNotActiveException 

Source Link

Document

Returns a string representation of the client host for the remote method invocation being processed in the current thread.

Usage

From source file:SuperPeer.java

@Override
public synchronized Key join() throws Exception {
    lg.log(Level.FINEST, "join Entry.");
    Key rv;/*from   w ww. j  av  a  2s  .  c om*/
    String ip = RemoteServer.getClientHost();
    // XXX: ID collisions need to be detected using peertable!
    rv = hasher.getHash(new Integer(prng.nextInt()).toString());
    PeerInfo pi = new PeerInfo(rv, ip);
    while (peertable.contains(pi)) {
        rv = hasher.getHash(new Integer(prng.nextInt()).toString());
        pi = new PeerInfo(rv, ip);
        lg.log(Level.WARNING, " Random Peer Key Collision, is your key space big enough?");
    }
    peertable.add(pi);
    lg.log(Level.INFO, "!!!! Allocating Node ID " + rv + " to client at " + ip + " !!!!");
    lg.log(Level.FINEST, "join Exit.");
    return rv;
}

From source file:org.pegadi.server.ServerImpl.java

/**
 * Sends an object to the server, and returns the same object. The object must implement
 * <code>serializable</code>.
 *
 * @param obj  An object/*from www .j  a  va 2s  .c  o m*/
 * @return  The object returned
 */
public Object ping(Object obj) {
    try {
        log.info("Ping from {}", RemoteServer.getClientHost());
    } catch (ServerNotActiveException sne) {
        log.error("Server not active, returning null.");
        return null;
    }

    // Log object
    if (obj != null) {
        log.info("Ping: Object is {}", obj.toString());
    } else {
        log.info("ping: Object is NULL!");
    }

    // Return
    if (obj instanceof Serializable) {
        return obj;
    } else {
        log.info("Ping: Object is not serializable, returning null.");
        return null;
    }
}

From source file:org.pegadi.server.ServerImpl.java

/**
 * Returns <code>true</code> if the server is alive. This method will always return
 * true if the client can contact the server, so instead of returning false an exception will
 * be thrown.//  www.j a va 2s. c  om
 *
 * @return <code>true</code> if the server is alive.
 * @see #ping(Object)
 */
public boolean ping() {
    try {
        log.info("Ping from {}", RemoteServer.getClientHost());
    } catch (ServerNotActiveException sne) {
        log.error("Server not active, returning false.");
        return false;
    }
    return true;
}

From source file:org.pegadi.server.ServerImpl.java

/**
 * Authenticates the identitiy of the user. This method returns a session
 * key if successful, <code>null</code> on failure.<br>
 * The session key that is returned must be used on all subsequent calls
 * that require authentication.//from w ww .jav a2  s. c o  m
 *
 * @param user  Username
 * @param pass  Password
 * @see org.pegadi.server.user.UserServerImpl#login
 * @see org.pegadi.server.Session
 *
 * @return A key for this session, <code>null</code> on failure.
 */
public String login(String user, String pass) {
    log.info("Login: user {} trying to log in", user);
    String ID = userServer.login(user);
    log.info("Login: user ID returned for userServer is: {}", ID);
    if (ID != null) {
        boolean authenticated = false;
        if (System.getProperty("developerMode") == null) {
            log.debug("Tryin to authenticate user");
            try {
                authenticated = ldapTemplate.authenticate(String.format("uid=%s,ou=people", user),
                        "objectClass=*", pass);
                log.info("Login: user authenticated: {}", authenticated);
            } catch (Exception e) {
                log.error("Caught exception while checking identity", e);
            }
        } else {
            log.info("In developermode, correct credentials not required!");
            authenticated = true;
        }

        if (authenticated) {

            String clientHost;
            try {
                clientHost = RemoteServer.getClientHost();
                log.info("Client host is: {}", clientHost);
            } catch (ServerNotActiveException sna) {
                clientHost = "";
            }
            Session sess = addSession(ID, clientHost);
            log.debug("Logging in user {}. Clienthost is {}", user, clientHost);
            logServer.logLogin(sess.getKey(), clientHost, ID);
            return sess.getKey();

        } else {
            log.info("Login: Password check failed!");
            return null;
        }
    } else {
        log.info("Login: No user name '{}' found.", user);
        return null;
    }
}

From source file:org.pegadi.server.ServerImpl.java

/**
 * Returns the callers host, or an empty string if an exception
 * occured when getting the host name. This is a convenience method
 * to use when we don't want to deal with the
 * {@link ServerNotActiveException java.rmi.server.ServerNotActiveException}
 * that can occur in a call to <code>getClientHost</code>.
 *
 * @return The host.//from w w  w .j av a2s. c  o m
 */
private String getSafeHost() {
    try {
        return RemoteServer.getClientHost();
    } catch (ServerNotActiveException e) {
        return "";
    }

}