Search in sources :

Example 1 with ClientCommandSet

use of com.cosylab.acs.maci.manager.recovery.ClientCommandSet in project ACS by ACS-Community.

the class ManagerImpl method clientLogin.

/**
	 * Client specific login method.
	 * @param	name	name of the client
	 * @param	reply	reply to authenticate method
	 * @param	client	client that is logging in
	 * @return	ClientInfo	client info. of newly logged client
	 */
private ClientInfo clientLogin(String name, AuthenticationData reply, Client client, long timeStamp, long executionId) throws AcsJNoPermissionEx {
    assert (name != null);
    assert (client != null);
    TimerTaskClientInfo clientInfo = null;
    synchronized (clients) {
        // check if client is already logged in,
        // if it is, return existing info
        int h = clients.first();
        while (h != 0) {
            ClientInfo loggedClientInfo = (ClientInfo) clients.get(h);
            if (client.equals(loggedClientInfo.getClient()))
                return loggedClientInfo;
            h = clients.next(h);
        }
        // check thread resources
        int usage = threadsUsedPercentage.get();
        if (usage > 90) {
            throw new NoResourcesException("Thread usage too high (%" + usage + "), rejecting login.");
        }
        // allocate new handle
        // !!! ACID 2
        Integer objHandle = (Integer) executeCommand(new ClientCommandAllocate());
        int handle;
        //int handle = clients.allocate();
        if (objHandle == null || (handle = objHandle.intValue()) == 0) {
            NoResourcesException af = new NoResourcesException("Generation of new handle failed, too many clients logged in.");
            throw af;
        }
        // generate external handle
        h = handle | CLIENT_MASK;
        // add generated key
        h |= (random.nextInt(0x100)) << 16;
        // create new client info
        clientInfo = new TimerTaskClientInfo(h, name, client);
        clientInfo.setAccessRights(AccessRights.REGISTER_COMPONENT);
        // register client to the heartbeat manager
        PingTimerTask task = new PingTimerTask(this, logger, clientInfo, null);
        clientInfo.setTask(task);
        heartbeatTask.schedule(task, clientPingInterval, clientPingInterval);
        // !!! ACID - register AddClientCommand
        executeCommand(new ClientCommandSet(handle, clientInfo));
    // store info
    //clients.set(handle, clientInfo);
    }
    // notify administrators about the login
    notifyClientLogin(clientInfo, timeStamp, executionId);
    logger.log(Level.INFO, "Client '" + name + "' logged in.");
    return clientInfo;
}
Also used : NoResourcesException(com.cosylab.acs.maci.NoResourcesException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClientCommandAllocate(com.cosylab.acs.maci.manager.recovery.ClientCommandAllocate) ClientInfo(com.cosylab.acs.maci.ClientInfo) ClientCommandSet(com.cosylab.acs.maci.manager.recovery.ClientCommandSet)

Aggregations

ClientInfo (com.cosylab.acs.maci.ClientInfo)1 NoResourcesException (com.cosylab.acs.maci.NoResourcesException)1 ClientCommandAllocate (com.cosylab.acs.maci.manager.recovery.ClientCommandAllocate)1 ClientCommandSet (com.cosylab.acs.maci.manager.recovery.ClientCommandSet)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1