Last active
December 18, 2015 18:39
-
-
Save azenla/5827612 to your computer and use it in GitHub Desktop.
TO PROVE I'M NOT STUPID!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.directmyfile.gcp.client.core; | |
import com.directmyfile.gcp.client.Main; | |
import com.directmyfile.gcp.client.connection.ConnectionHandler; | |
import com.directmyfile.gcp.client.data.DataHelper; | |
import com.directmyfile.gcp.client.event.*; | |
import com.directmyfile.gcp.client.gcp.GCPServerProfile; | |
import com.directmyfile.gcp.client.gcp.*; | |
import jpower.socket.EventHandler; | |
import jpower.socket.JPowerSocket; | |
import jpower.socket.event.ConnectEvent; | |
import jpower.socket.event.*; | |
import java.net.ConnectException; | |
import java.util.ArrayList; | |
/** | |
* TODO: Move most of this code to GCPEventHandler to make it more extensive | |
*/ | |
public class ConnectionEventHandler extends EventHandler { | |
private GCPServerProfile profile; | |
private GCPEventHandler eventHandler; | |
private JPowerSocket powerSocket; | |
private ConnectionHandler connectionHandler; | |
private static ArrayList<Class<? extends Event>> eventTypes = new ArrayList<Class<? extends Event>>(); | |
public ConnectionEventHandler(GCPServerProfile profile) { | |
this.eventHandler = new GCPEventHandler(this); | |
this.profile = profile; | |
} | |
public static void load() { | |
eventTypes.add(ClientJoinEvent.class); | |
eventTypes.add(UserJoinEvent.class); | |
eventTypes.add(com.directmyfile.gcp.client.event.ConnectEvent.class); | |
eventTypes.add(ClientLeaveEvent.class); | |
eventTypes.add(UserLeaveEvent.class); | |
eventTypes.add(StartupEvent.class); | |
eventTypes.add(MessageEvent.class); | |
eventTypes.add(PingEvent.class); | |
eventTypes.add(GCPLineReceivedEvent.class); | |
eventTypes.add(GCPLineSentEvent.class); | |
} | |
public GCPEventHandler getGCPEventHandler() { | |
return eventHandler; | |
} | |
@Override | |
public void onConnect(ConnectEvent event) { | |
this.powerSocket = event.getPowerSocket(); | |
this.connectionHandler = profile.getConnection(); | |
eventHandler.onConnect(new com.directmyfile.gcp.client.event.ConnectEvent(profile)); | |
} | |
@Override | |
public void onLineReceived(LineReceivedEvent event) { | |
GCPLine line = GCPHelper.getLineFromLine(event.getLine()); | |
String command = line.getCommand(); | |
if (command.equals(GCPConstants.JOIN)) { | |
String channel = line.getProperty("channel"); | |
String user = line.getProperty("user"); | |
eventHandler.onUserJoin(new UserJoinEvent(profile, user, channel)); | |
} else if (command.equals(GCPConstants.SYNTAXERROR)) { | |
System.out.println("SYNTAX ERROR: Data: " + line.getProperty("data")); | |
} else if (command.equals(GCPConstants.ERROR)) { | |
GCPError error = GCPHelper.parseError(line.getLine()); | |
if (error.isChannel()) { | |
System.out.println("ERROR: Code: 000" + error.getError() + " Type: " + error.getType() + " User: " + error.getUser()); | |
} else { | |
System.out.println("ERROR: Code: 000" + error.getError() + " Type: " + error.getType() + " Parameters: " + error.getParameters()); | |
} | |
} else if (command.equals(GCPConstants.LEAVE)) { | |
eventHandler.onUserLeave(new UserLeaveEvent(profile, line.getProperty(GCPConstants.CHANNEL_PROP), line.getProperty(GCPConstants.USER_PROP))); | |
} else if (command.equals(GCPConstants.MSG)) { | |
eventHandler.onMessage(new MessageEvent(profile, line.getProperty(GCPConstants.USER_PROP), line.getProperty(GCPConstants.TARGET_PROP), line.getProperty(GCPConstants.MESSAGE_PROP))); | |
} else if (command.equals(GCPConstants.PING)) { | |
if (line.getProperties().containsKey(GCPConstants.USER_PROP)) { | |
eventHandler.onPing(new PingEvent(profile, line.getProperty(GCPConstants.USER_PROP))); | |
} else { | |
eventHandler.onPing(new PingEvent(profile)); | |
} | |
} | |
eventHandler.onLineReceived(new GCPLineReceivedEvent(profile, line)); | |
if (Main.debug) { | |
System.out.println("Received: " + line.getLine()); | |
} | |
} | |
@Override | |
public void onLineSent(LineSentEvent event) { | |
GCPLine line = new GCPLine(event.getLine()); | |
if (Main.debug) { | |
System.out.println("Sent: " + line.getLine()); | |
} | |
//noinspection StatementWithEmptyBody | |
if (line.getCommand().equals("NICK")) { | |
// TODO: Waiting for GCP Nick Changing Specification | |
} else if (line.getCommand().equals(GCPConstants.JOIN)) { | |
eventHandler.onClientJoin(new ClientJoinEvent(profile, line.getProperty(GCPConstants.CHANNEL_PROP))); | |
} else if (line.getCommand().equals(GCPConstants.LEAVE)) { | |
eventHandler.onClientLeave(new ClientLeaveEvent(profile, line.getProperty(GCPConstants.CHANNEL_PROP))); | |
} else if (line.getCommand().equals(GCPConstants.REGISTER)) { // We have executed a register - we need to keep the password to ensure that everything works | |
DataHelper.set("password", line.getProperty(GCPConstants.PASSWORD_PROP)); | |
} | |
} | |
@Override | |
public void onError(ErrorEvent event) { | |
if (ConnectException.class.isInstance(event.getException())) { | |
if (event.getException().getMessage().equals("Connection refused")) { | |
System.out.println("Unable to connect to server " + profile.getServerHost() + "! Connection refused on port " + profile.getServerPort()); | |
System.exit(1); | |
} | |
} | |
} | |
@Override | |
public void onDisconnect(DisconnectEvent event) { | |
System.out.println("Disconnected From Server."); | |
} | |
public JPowerSocket getPowerSocket() { | |
return powerSocket; | |
} | |
public ConnectionHandler getConnectionHandler() { | |
return connectionHandler; | |
} | |
public static ArrayList<Class<? extends Event>> getEventTypes() { | |
return eventTypes; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment