me.mgray.universalremote.client.model.ServerConnector.java Source code

Java tutorial

Introduction

Here is the source code for me.mgray.universalremote.client.model.ServerConnector.java

Source

/*******************************************************************************
 * Copyright (c) 2014. Mathew Gray
 *
 * This work is licensed under a Creative Commons
 * Attribution-NonCommercial-ShareAlike 4.0 International License.
 ******************************************************************************/

package me.mgray.universalremote.client.model;

import me.mgray.universalremote.client.model.event.SessionIdRecievedEvent;
import me.mgray.universalremote.shared.Connection;
import org.bushe.swing.event.EventBus;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class ServerConnector {
    private int port;
    private String hostname;
    private Connection serverConnection = null;
    private Executor socketListener = Executors.newSingleThreadExecutor();
    private String sessionId;

    public static ServerConnector createNew() {
        ApplicationContext context = new ClassPathXmlApplicationContext("Client-Configuration.xml");
        return (ServerConnector) context.getBean("serverConnector");
    }

    public void connect() {
        try {
            serverConnection = new Connection(new Socket(hostname, port));
            // Upon connection, server will give the session id
            sessionId = serverConnection.read();
            EventBus.publish(new SessionIdRecievedEvent(sessionId));
            // Listen for commands
            socketListener.execute(new CommandListener(serverConnection));
        } catch (UnknownHostException e) {
            System.err.println(String.format("Don't know about host: %s", hostname));
            System.exit(1);
        } catch (IOException e) {
            System.err.println(String.format("Couldn't get I/O for the connection to: %s:%d", hostname, port));
            System.exit(1);
        }
    }

    public void setPort(int port) {
        this.port = port;
    }

    public void setHostname(String hostname) {
        this.hostname = hostname;
    }

    public String getSessionId() {
        return sessionId;
    }
}