- public void close() throws IOException {
- if (connectionHandler != null) {
- connectionHandler.close();
- }
- }
-
- //
- // ABSTRACTEXECUTIONTHREADSERVICE METHODS
- //
-
- @Override
- protected void startUp() throws IllegalStateException {
- checkState(username.isPresent(), "username must be set");
- checkState(realName.isPresent(), "realName must be set");
- }
-
- @Override
- protected void run() {
-
- /* connect to remote socket. */
- try {
- Socket socket = socketFactory.createSocket(hostname, port);
- socket.setSoTimeout((int) TimeUnit.MINUTES.toMillis(3));
- connectionHandler = new ConnectionHandler(socket.getInputStream(), socket.getOutputStream());
-
- /* register connection. */
- if (password.isPresent()) {
- connectionHandler.sendCommand("PASSWORD", password.get());
- }
- connectionHandler.sendCommand("USER", username.get(), "8", "*", realName.get());
- nickname = nicknameChooser.getNickname();
- connectionHandler.sendCommand("NICK", nickname);
-
- } catch (IOException ioe1) {
- eventBus.post(new ConnectionFailed(this, ioe1));
- return;
- }
-
- eventBus.register(this);
- /* now read replies and react. */
- try {
- /* some status variables. */
- boolean connected = true;
-
- PrefixHandler prefixHandler = new PrefixHandler();
- List<Handler> handlers = asList(
- new MessageHandler(eventBus, this, prefixHandler),
- new CtcpHandler(eventBus, this),
- new ChannelNickHandler(eventBus, this, prefixHandler),
- new SimpleCommandHandler(eventBus)
- .addCommand("431",
- (s, p) -> new NoNicknameGivenReceived(
- this))
- .addCommand("NICK",
- (s, p) -> new NicknameChanged(this,
- s.get(), p.get(0)))
- .addCommand("JOIN",
- (s, p) -> new ChannelJoined(this,
- p.get(0), s.get())),
- new MotdHandler(eventBus, this),
- new ChannelNotJoinedHandler(eventBus, this),
- new ConnectionEstablishHandler(eventBus, this),
- prefixHandler
- );
-
- while (connected) {
- Reply reply = connectionHandler.readReply();
- eventBus.post(new ReplyReceived(this, reply));
- logger.trace(String.format("<< %s", reply));
- String command = reply.command();
- List<String> parameters = reply.parameters();
-
- for (Handler handler : handlers) {
- if (handler.willHandle(reply)) {
- handler.handleReply(reply);
- break;
- }
- }
-
- /* 43x replies are for nick change errors. */
- if (command.equals("433")) {
- if (!established.get()) {
- nickname = nicknameChooser.getNickname();
- connectionHandler.sendCommand("NICK", nickname);
- } else {
- eventBus.post(new NicknameInUseReceived(this, reply));
- }
-
- /* channel stuff. */
- } else if (command.equals("331")) {
- /* no topic is set. */
- } else if (command.equals("332")) {
- eventBus.post(new ChannelTopic(this, parameters.get(1), parameters.get(2)));
- } else if (command.equalsIgnoreCase("PART")) {
- eventBus.post(new ChannelLeft(this, parameters.get(0), reply.source().get(), getOptional(parameters, 1)));
- } else if (command.equalsIgnoreCase("QUIT")) {
- eventBus.post(new ClientQuit(this, reply.source().get(), parameters.get(0)));
-
- /* basic connection housekeeping. */
- } else if (command.equalsIgnoreCase("PING")) {
- connectionHandler.sendCommand("PONG", getOptional(parameters, 0), getOptional(parameters, 1));
-
- } else if (command.equalsIgnoreCase("KICK")) {
- eventBus.post(new KickedFromChannel(this, parameters.get(0), reply.source().get(), parameters.get(1), getOptional(parameters, 2)));
-
- /* okay, everything else. */
- } else {
- eventBus.post(new UnknownReplyReceived(this, reply));
- }
- }
- eventBus.post(new ConnectionClosed(this));
- } catch (IOException ioe1) {
- logger.warn("I/O error", ioe1);
- eventBus.post(new ConnectionClosed(this, ioe1));
- } catch (RuntimeException re1) {
- logger.error("Runtime error", re1);
- eventBus.post(new ConnectionClosed(this, re1));
- } finally {
- established.set(false);
- eventBus.unregister(this);
- logger.info("Closing Connection.");
- try {
- Closeables.close(connectionHandler, true);
- } catch (IOException ioe1) {
- /* will not be thrown. */
- }
- }
-
- }
-
- @Subscribe
- public void connectionEstablished(ConnectionEstablished connectionEstablished) {
- if (connectionEstablished.connection() == this) {
- established.set(true);
- }
- }
-
- //
- // PRIVATE METHODS
- //