Search in sources :

Example 1 with NXCPMsgWaitQueue

use of org.netxms.base.NXCPMsgWaitQueue in project netxms by netxms.

the class NXCSession method connect.

/**
 * Connect to NetMS server. Establish connection with the server and set up encryption if required.
 * Versions of protocol components given in *componentVersions* will be validated. Login must be
 * performed before using session after successful connect.
 *
 * @param componentVersions The versions of the components
 * @throws IOException  if socket I/O error occurs
 * @throws UnknownHostException if the host is unknown
 * @throws NXCException if NetXMS server returns an error or operation was timed out
 * @throws IllegalStateException if the state is illegal
 */
public void connect(int[] componentVersions) throws IOException, UnknownHostException, NXCException, IllegalStateException {
    if (connected)
        throw new IllegalStateException("Session already connected");
    if (disconnected)
        throw new IllegalStateException("Session already disconnected and cannot be reused");
    encryptionContext = null;
    Logger.info("NXCSession.connect", "Connecting to " + connAddress + ":" + connPort);
    try {
        socket = new Socket();
        socket.connect(new InetSocketAddress(connAddress, connPort), connectTimeout);
        msgWaitQueue = new NXCPMsgWaitQueue(commandTimeout);
        recvThread = new ReceiverThread();
        housekeeperThread = new HousekeeperThread();
        new NotificationProcessor();
        // get server information
        Logger.debug("NXCSession.connect", "connection established, retrieving server info");
        NXCPMessage request = newMessage(NXCPCodes.CMD_GET_SERVER_INFO);
        sendMessage(request);
        NXCPMessage response = waitForMessage(NXCPCodes.CMD_REQUEST_COMPLETED, request.getMessageId());
        protocolVersion = new ProtocolVersion(response);
        if (!ignoreProtocolVersion) {
            if (!protocolVersion.isCorrectVersion(ProtocolVersion.INDEX_BASE) || ((componentVersions != null) && !validateProtocolVersions(componentVersions))) {
                Logger.warning("NXCSession.connect", "connection failed (" + protocolVersion.toString() + ")");
                throw new NXCException(RCC.BAD_PROTOCOL, protocolVersion.toString());
            }
        } else {
            Logger.debug("NXCSession.connect", "protocol version ignored");
        }
        serverVersion = response.getFieldAsString(NXCPCodes.VID_SERVER_VERSION);
        serverId = response.getFieldAsInt64(NXCPCodes.VID_SERVER_ID);
        serverTimeZone = response.getFieldAsString(NXCPCodes.VID_TIMEZONE);
        serverTime = response.getFieldAsInt64(NXCPCodes.VID_TIMESTAMP) * 1000;
        serverTimeRecvTime = System.currentTimeMillis();
        serverChallenge = response.getFieldAsBinary(NXCPCodes.VID_CHALLENGE);
        tileServerURL = response.getFieldAsString(NXCPCodes.VID_TILE_SERVER_URL);
        if (tileServerURL != null) {
            if (!tileServerURL.endsWith("/"))
                tileServerURL = tileServerURL.concat("/");
        } else {
            tileServerURL = "http://tile.openstreetmap.org/";
        }
        dateFormat = response.getFieldAsString(NXCPCodes.VID_DATE_FORMAT);
        if ((dateFormat == null) || (dateFormat.length() == 0))
            dateFormat = "dd.MM.yyyy";
        timeFormat = response.getFieldAsString(NXCPCodes.VID_TIME_FORMAT);
        if ((timeFormat == null) || (timeFormat.length() == 0))
            timeFormat = "HH:mm:ss";
        shortTimeFormat = response.getFieldAsString(NXCPCodes.VID_SHORT_TIME_FORMAT);
        if ((shortTimeFormat == null) || (shortTimeFormat.length() == 0))
            shortTimeFormat = "HH:mm";
        int count = response.getFieldAsInt32(NXCPCodes.VID_NUM_COMPONENTS);
        long fieldId = NXCPCodes.VID_COMPONENT_LIST_BASE;
        for (int i = 0; i < count; i++) serverComponents.add(response.getFieldAsString(fieldId++));
        // Setup encryption if required
        if (connUseEncryption) {
            request = newMessage(NXCPCodes.CMD_REQUEST_ENCRYPTION);
            request.setFieldInt16(NXCPCodes.VID_USE_X509_KEY_FORMAT, 1);
            sendMessage(request);
            waitForRCC(request.getMessageId());
        }
        Logger.debug("NXCSession.connect", "Connected to server version " + serverVersion);
        connected = true;
    } finally {
        if (!connected)
            disconnect(SessionNotification.USER_DISCONNECT);
    }
}
Also used : NXCPMsgWaitQueue(org.netxms.base.NXCPMsgWaitQueue) InetSocketAddress(java.net.InetSocketAddress) NXCPMessage(org.netxms.base.NXCPMessage) Socket(java.net.Socket) ConnectionPoint(org.netxms.client.topology.ConnectionPoint) AccessPoint(org.netxms.client.objects.AccessPoint)

Example 2 with NXCPMsgWaitQueue

use of org.netxms.base.NXCPMsgWaitQueue in project netxms by netxms.

the class Session method connect.

/**
 * Connect to server.
 *
 * @throws IOException
 * @throws UnknownHostException
 * @throws MobileAgentException if server returns an error or request was timed out
 */
public void connect() throws IOException, UnknownHostException, MobileAgentException {
    Logger.info("Session.connect", "Connecting to " + connAddress + ":" + connPort);
    try {
        connSocket = new Socket(connAddress, connPort);
        msgWaitQueue = new NXCPMsgWaitQueue(commandTimeout);
        recvThread = new ReceiverThread();
        // get server information
        Logger.debug("Session.connect", "connection established, retrieving server info");
        NXCPMessage request = newMessage(NXCPCodes.CMD_GET_SERVER_INFO);
        sendMessage(request);
        NXCPMessage response = waitForMessage(NXCPCodes.CMD_REQUEST_COMPLETED, request.getMessageId());
        if (response.getFieldAsInt32(NXCPCodes.VID_PROTOCOL_VERSION) != PROTOCOL_VERSION) {
            Logger.warning("Session.connect", "connection failed, server protocol version is " + response.getFieldAsInt32(NXCPCodes.VID_PROTOCOL_VERSION));
            throw new MobileAgentException(RCC.BAD_PROTOCOL);
        }
        String serverVersion = response.getFieldAsString(NXCPCodes.VID_SERVER_VERSION);
        // Setup encryption if required
        if (connUseEncryption) {
            request = newMessage(NXCPCodes.CMD_REQUEST_ENCRYPTION);
            request.setFieldInt16(NXCPCodes.VID_USE_X509_KEY_FORMAT, 1);
            sendMessage(request);
            waitForRCC(request.getMessageId());
        }
        // Login to server
        Logger.debug("Session.connect", "Connected to server version " + serverVersion + ", trying to login");
        request = newMessage(NXCPCodes.CMD_LOGIN);
        request.setField(NXCPCodes.VID_DEVICE_ID, connDeviceId);
        request.setField(NXCPCodes.VID_LOGIN_NAME, connLoginName);
        request.setField(NXCPCodes.VID_PASSWORD, connPassword);
        request.setField(NXCPCodes.VID_LIBNXCL_VERSION, NXCommon.VERSION);
        request.setField(NXCPCodes.VID_OS_INFO, System.getProperty("os.name") + " " + System.getProperty("os.version"));
        sendMessage(request);
        response = waitForMessage(NXCPCodes.CMD_LOGIN_RESP, request.getMessageId());
        int rcc = response.getFieldAsInt32(NXCPCodes.VID_RCC);
        Logger.debug("Session.connect", "CMD_LOGIN_RESP received, RCC=" + rcc);
        if (rcc != RCC.SUCCESS) {
            Logger.warning("NXCSession.connect", "Login failed, RCC=" + rcc);
            throw new MobileAgentException(rcc);
        }
        Logger.info("Session.connect", "succesfully connected and logged in");
        isConnected = true;
    } finally {
        if (!isConnected)
            disconnect();
    }
}
Also used : NXCPMsgWaitQueue(org.netxms.base.NXCPMsgWaitQueue) NXCPMessage(org.netxms.base.NXCPMessage) Socket(java.net.Socket)

Aggregations

Socket (java.net.Socket)2 NXCPMessage (org.netxms.base.NXCPMessage)2 NXCPMsgWaitQueue (org.netxms.base.NXCPMsgWaitQueue)2 InetSocketAddress (java.net.InetSocketAddress)1 AccessPoint (org.netxms.client.objects.AccessPoint)1 ConnectionPoint (org.netxms.client.topology.ConnectionPoint)1