Search in sources :

Example 1 with ClientHandler

use of de.janrufmonitor.service.client.http.simple.ClientHandler in project janrufmonitor by tbrandt77.

the class Client method connect.

public synchronized boolean connect() {
    if (this.isConnected)
        return true;
    this.m_httpSrv = new SimplePortListener(new ClientHandler(), this.getListenPort());
    this.m_httpSrv.start();
    try {
        Thread.sleep(500);
    } catch (InterruptedException e) {
        this.m_logger.severe(e.getMessage());
    }
    // register client at server
    if (this.m_httpSrv != null) {
        try {
            IHttpRequest rq = new RegisterGetHandler(InetAddress.getLocalHost().getHostName(), InetAddress.getLocalHost().getHostAddress(), this.m_httpSrv.getPort(), this.getRegisterEvents());
            try {
                this.m_logger.info("Connecting to server with URI: " + rq.getURI());
            } catch (Exception e1) {
                this.m_logger.log(Level.SEVERE, e1.getMessage(), e1);
            }
            IRequester r = RequesterFactory.getInstance().createRequester(rq);
            r.request();
            int responseCode = r.request().getCode();
            if (responseCode == 200) {
                this.isConnected = true;
                this.m_logger.info("Registration of client " + this.m_httpSrv.getServerIP() + " successfull.");
                this.m_logger.info("Client is started and connected with server ...");
                ClientStateManager.getInstance().fireState(IClientStateMonitor.CONNECTION_OK, "");
                return true;
            }
            if (responseCode == 0) {
                this.m_logger.severe("Registration of client " + this.m_httpSrv.getServerIP() + " failed. Server not reachable.");
                ClientStateManager.getInstance().fireState(IClientStateMonitor.SERVER_NOT_FOUND, "Server: " + r.getServer() + "\nPort: " + r.getPort());
            }
            if (responseCode == 403) {
                this.m_logger.severe("Client " + InetAddress.getLocalHost().getHostName() + " (" + InetAddress.getLocalHost().getHostAddress() + ") is not authorized to connect to server: " + r.getServer());
                ClientStateManager.getInstance().fireState(IClientStateMonitor.SERVER_NOT_AUTHORIZED, "Client " + InetAddress.getLocalHost().getHostName() + " (" + InetAddress.getLocalHost().getHostAddress() + ") is not authorized to connect to server: " + r.getServer());
            }
            this.m_logger.severe("Response code from server was not OK: " + responseCode);
        } catch (UnknownHostException e) {
            this.m_logger.severe(e.getMessage());
            ClientStateManager.getInstance().fireState(IClientStateMonitor.SERVER_NOT_FOUND, e.getMessage());
        }
        this.m_httpSrv.stop();
        this.m_httpSrv = null;
    }
    return false;
}
Also used : IHttpRequest(de.janrufmonitor.service.commons.http.IHttpRequest) UnknownHostException(java.net.UnknownHostException) SimplePortListener(de.janrufmonitor.service.commons.http.simple.SimplePortListener) ClientHandler(de.janrufmonitor.service.client.http.simple.ClientHandler) IRequester(de.janrufmonitor.service.commons.http.IRequester) UnknownHostException(java.net.UnknownHostException)

Aggregations

ClientHandler (de.janrufmonitor.service.client.http.simple.ClientHandler)1 IHttpRequest (de.janrufmonitor.service.commons.http.IHttpRequest)1 IRequester (de.janrufmonitor.service.commons.http.IRequester)1 SimplePortListener (de.janrufmonitor.service.commons.http.simple.SimplePortListener)1 UnknownHostException (java.net.UnknownHostException)1