📄 clientlistenhandler.java
字号:
/*
* Created on Jun 5, 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package org.GTADS.client;
import java.io.*;
import org.GTADS.protocol.*;
import org.GTADS.messenger.*;
import org.GTADS.client.preferences.PreferencesManager;
import org.GTADS.helper.*;
import org.GTADS.usermanager.*;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ClientListenHandler extends Thread {
String username = new String();
ChatWindowDialog chatWindowDialog;
public ClientListenHandler(ChatWindowDialog chatWindowDialog){
ClientLoginScreen.getInstance().writeStatusLabel("Connected!");
this.username = ClientLoginScreen.getInstance().getUsername();
this.chatWindowDialog = chatWindowDialog;
start();
}
public void run(){
String bufferInput = new String();
String recipient = new String();
String context = new String();
String session = new String();
String message = new String();
while(true){
try {
DataInputStream din = new DataInputStream(DSChatClient.ClientSocket.getInputStream());
bufferInput = din.readUTF();
System.out.println(bufferInput);
recipient = ProtocolHandler.getDataFromMessage(bufferInput,ProtocolHandler.RECIPIENT);
context = ProtocolHandler.getDataFromMessage(bufferInput,ProtocolHandler.CONTEXT);
session = ProtocolHandler.getDataFromMessage(bufferInput,ProtocolHandler.SESSION);
message = ProtocolHandler.getDataFromMessage(bufferInput,ProtocolHandler.MESSAGE);
message = Helper.clipCarriageReturn(message);
if (recipient != null && recipient.equalsIgnoreCase(MetaData.FROM_SERVER)){
if (context != null && context.equalsIgnoreCase(MetaData.CHATROOM + "")){
if (session != null && session.equalsIgnoreCase(MetaData.NO_SESSION_NAME)){
ChatWindowDialog.getInstance().appendToChatroom(0,message); // System Message
}
}
else if (context != null && context.equalsIgnoreCase(MetaData.GENERIC + "")){
if (session != null && session.equalsIgnoreCase(MetaData.PROXY_CHECK)){
if (message != null){
if (message.equalsIgnoreCase(MetaData.PROXY_FAIL)){
/*String proxyPortNotOpen = "Your proxy port not detected " +
" as being open.\n This can be attributed to the following things:\n" +
" - Your router or firewall is not fowarding udp port 8000 to your computer\n" +
" - Another program is using udp port 8000\n" +
" - You have an older version of the GTADS Client (Please Upgrade)\n\n" +
"Not having your proxy port open will not allow you to participate in\n" +
"playing games using GTADS";*/
String proxyPortNotOpen = "Your proxy port is not detected as being open.\n" +
"You will NOT be able to host any games but you will be allowed to join\n" +
"and participate in any games. If you would like to fix this problem\n" +
"you will need to port forward port " + PreferencesManager.getInstance().getProxyPort() +
" (UDP) to your PC or check if any software is using this port ";
DialogManager.ErrorDialog(proxyPortNotOpen);
}
// else {
// if (ClientConnectionHandler.clientProxyCheck(true)){
// DialogManager.SuccessDialog("Proxy Port Test Successful\nYour port" +
// PreferencesManager.getInstance().getProxyPort() + "/UDP is open to " +
// " play games");
// }
// }
}
}
if (session != null && session.equalsIgnoreCase(MetaData.JOIN_CHATROOM)){
if (message != null){
// Message is the chatroom
}
}
if (session != null && session.equalsIgnoreCase(MetaData.SEND_CHATROOM_LIST)){
if (message != null){
ChatroomListingWindow.getInstance().addChatroomToList(message);
}
}
if (session != null && session.equalsIgnoreCase(ErrorManager.ERROR_DEFINED)){
if (message != null){
if (message.equalsIgnoreCase(ErrorManager.CHATROOM_MAX)){
ChatWindowDialog.getInstance().appendToChatroom(0,
"Error Creating Chatroom: Cannot exceed maximum chatroom count");
}
else if (message.equalsIgnoreCase(ErrorManager.CHATROOM_FULL)){
ChatWindowDialog.getInstance().appendToChatroom(0,
"Chatroom full try again later");
}
else if (message.equalsIgnoreCase(ErrorManager.USER_NOT_FOUND)){
ChatWindowDialog.getInstance().appendToChatroom(0,
"User not Found");
}
else if (message.equalsIgnoreCase(ErrorManager.ACCESS_DENIED)){
ChatWindowDialog.getInstance().appendToChatroom(0,
"Access Denied");
}
else if (message.equalsIgnoreCase(ErrorManager.GAMEROOM_DENIED)){
DialogManager.ErrorDialog("The gameroom feature has been disabled because\n" +
"the server has identified your proxy port as being\n" +
"closed. Please take the necessary steps to open your port and relogin\n" +
"Error Code:" + ErrorManager.GAMEROOM_DENIED);
}
}
}
else if (session != null && session.equalsIgnoreCase("all")){
if (message != null && message.equalsIgnoreCase(MetaData.PING_REQUEST)){
MetaData pingReplyHeader = new MetaData(DSChatClient.clientUser.getUsername(), MetaData.GENERIC,MetaData.PING_REPLY);
MessageAdapter.sendData(null,DSChatClient.ClientSocket,pingReplyHeader,MetaData.PING_REQUEST);
}
}
}
else if (context != null && context.equalsIgnoreCase(MetaData.FRIENDSLIST + "")){
if (session != null && session.equalsIgnoreCase(FriendsList.STATE_ONLINE + "")){
if (message != null)
FriendsListWindow.getInstance().addNameToFriendsList(message);
}
if (session != null && session.equalsIgnoreCase(FriendsList.STATE_OFFLINE + "")){
if (message != null)
FriendsListWindow.getInstance().removeNameFromFriendsList(message);
}
}
else if (context != null && context.equalsIgnoreCase(MetaData.DASHBOARD + "")){
if (session != null && session.equalsIgnoreCase(MetaData.GET_NUM_USERS)){
ChatWindowDialog.getInstance().getDashboardPanel().setNumberOfUsers(message);
}
else if (session != null && session.equalsIgnoreCase(MetaData.GET_NUM_GAMEROOMS)){
ChatWindowDialog.getInstance().getDashboardPanel().setNumberOfGamerooms(message);
}
}
else if (context != null && context.equalsIgnoreCase(MetaData.GAME + "")){
if (session != null){
if (session.equalsIgnoreCase(GameroomManager.ADD_GAMEROOM + "")){
String[] gameInfo = new String[4];
Integer maxPlayers;
Integer currentPlayers;
if (message.split(",").length == 4){
gameInfo = message.split(",");
maxPlayers = Integer.valueOf(gameInfo[3].split("/")[1]);
currentPlayers = Integer.valueOf(gameInfo[3].split("/")[0]);
GameRoom g = new GameRoom(new User(gameInfo[1]),gameInfo[0],
gameInfo[2],maxPlayers.intValue());
for (int i = 0; i < currentPlayers.intValue() - 1; i++){
g.addUserToChatroom("DUMMY VALUE");
}
GameroomListWindow.getInstance().addGameRoomToList(g);
// ChatWindowDialog.getInstance().getDashboardPanel().setNumberOfGamerooms(GameroomListWindow.getInstance().getNumberInList() + "");
}
}
else {
String[] gameInfo = new String[4];
if (message.split(",").length == 4){
gameInfo = message.split(",");
GameroomListWindow.getInstance().removeGameRoomFromList(gameInfo[0]);
}
}
}
}
}
else if (recipient != null){
if (context != null && context.equalsIgnoreCase(MetaData.CHATROOM + "")){
if (session != null && session.equalsIgnoreCase(MetaData.JOIN_CHATROOM)){
if (message != null) {
addUserDialog(message);
userJoinedChatroom(message);
}
}
else if (session != null && session.equalsIgnoreCase(MetaData.PART_CHATROOM)){
if (message != null) {
removeUserDialog(message);
userPartChatroom(message);
}
}
else if (session != null && session.equalsIgnoreCase(MetaData.GREET)){
if (message != null)
greetUserToChatroom(message);
}
else if (session != null && session.equalsIgnoreCase(MetaData.SEND_LIST)){
if (message != null)
addUserDialog(message);
}
else {
if (message != null) {
if (session != null && session.startsWith("#"))
getChatMessage(recipient, message);
else {
if (recipient.equals(username)) {
InstantMessageWindow.getInstance(session).appendText(username, message);
}
else {
InstantMessageWindow.getInstance(recipient).appendText(recipient, message);
}
}
}
}
}
if (context != null && context.equalsIgnoreCase(MetaData.GENERIC + "")){
//sday,0,UPTIME,OFFLINE
if (session != null && session.equalsIgnoreCase(MetaData.UPTIME)){
if (message != null){
sendUptimeDisplay(recipient, message);
}
}
}
if (context != null && context.equalsIgnoreCase(MetaData.GAME + "")){
if (session != null && session.equalsIgnoreCase(MetaData.GREET)){
GameroomCreationWindow.clearInstance();
GameroomWindowDialog.clearInstance();
GameroomWindowDialog.getInstance(message).show();
GameroomWindowDialog.getInstance().
appendToChatroom("* Welcome to " + message);
}
else if (session != null && session.equalsIgnoreCase(MetaData.SEND_LIST)){
if (GameroomWindowDialog.hasInstance()){
GameroomWindowDialog.getInstance().addUserToList(message);
}
}
else if (session != null && session.equalsIgnoreCase(MetaData.JOIN_CHATROOM)){
if (GameroomWindowDialog.hasInstance()){
GameroomWindowDialog.getInstance().addUserToList(message);
}
}
else if (session != null && session.equalsIgnoreCase(MetaData.PART_CHATROOM)){
if (GameroomWindowDialog.hasInstance()){
GameroomWindowDialog.getInstance().removeUserFromList(message);
}
}
else if (session != null && session.equalsIgnoreCase(MetaData.DISPOSE_GAMEROOM)){
GameroomWindowDialog.clearInstance();
DialogManager.ErrorDialog("Gameroom destroyed by host");
}
else if (session != null && session.equalsIgnoreCase(MetaData.IS_HOST)){
if (!message.equals(ClientLoginScreen.getInstance().getUsername())){
if (GameroomWindowDialog.hasInstance()){
GameroomWindowDialog.getInstance().disableStartButton();
}
}
}
else if (session != null && session.equalsIgnoreCase(MetaData.START_PROXY)){
if (GameroomWindowDialog.hasInstance()){
GameroomWindowDialog.getInstance().startGameProxy(message);
}
}
else if (session.equalsIgnoreCase(MetaData.HOST_READY)){
if (GameroomWindowDialog.hasInstance()){
GameroomWindowDialog.getInstance().appendToChatroom("" +
"* Host has started Proxy");
}
}
else if (session.equalsIgnoreCase(MetaData.STOP_PROXY)){
if (GameroomWindowDialog.hasInstance()){
GameroomWindowDialog.getInstance().appendToChatroom("" +
"* Host has halted Proxy");
}
}
else if (session.equalsIgnoreCase(MetaData.PROXY_CONNECTED)){
if (GameroomWindowDialog.hasInstance()){
GameroomWindowDialog.getInstance().appendToChatroom("" +
"* " + message + " has successfully connected to Host");
}
}
else if (session.equalsIgnoreCase(MetaData.GAME_STARTED)){
if (GameroomWindowDialog.hasInstance()){
GameroomWindowDialog.getInstance().appendToChatroom("" +
"* " + "Host's game has been detected");
GameroomWindowDialog.getInstance().appendToChatroom("" +
"* " + "You may connect to 127.0.0.1 in your game");
}
}
else if (session.equalsIgnoreCase(MetaData.GAME_STOPPED)){
if (GameroomWindowDialog.hasInstance()){
GameroomWindowDialog.getInstance().appendToChatroom("" +
"* " + "Host's game has been closed");
}
}
else { // Assume a chat message
if (GameroomWindowDialog.hasInstance()){
if (GameroomWindowDialog.getInstance().getGameroomName().equals(session)){
GameroomWindowDialog.getInstance().appendToChatroom("[" + recipient
+ "] " + message);
}
}
}
}
}
} catch (IOException e){
if (!DSChatClient.isApplet){
ClientLoginScreen.getInstance().show();
ChatWindowDialog.destroyInstance();
chatWindowDialog.dispose();
GameroomListWindow.clearInstance();
GameroomCreationWindow.clearInstance();
}
return;
}
}
}
private void addUserDialog(String newUsername){
ChatWindowDialog.getInstance().addUserToList(newUsername);
}
private void removeUserDialog(String deleteUserName){
ChatWindowDialog.getInstance().removeUserFromList(deleteUserName);
}
private void userJoinedChatroom(String userName){
ChatWindowDialog.getInstance().appendToChatroom(1, userName);
}
private void userPartChatroom(String userName){
ChatWindowDialog.getInstance().appendToChatroom(2, userName);
}
private void greetUserToChatroom(String chatroom){
ChatWindowDialog.getInstance().appendToChatroom(0, "You have joined " + chatroom);
addUserDialog(username);
}
private void getChatMessage(String sender, String message){
ChatWindowDialog.getInstance().appendToChatroom(-1, "<" + sender + "> " + message);
}
public static void sendChatMessage(MetaData sendHeader, String message) throws IOException{
MessageAdapter.sendData(null, DSChatClient.ClientSocket, sendHeader, message);
}
public static void sendUptimeDisplay (String user, String uptime){
String displayText = new String();
long getDiffHours;
long getDiffDays;
long getDiffMinutes;
try {
if (uptime.equalsIgnoreCase("OFFLINE")){
displayText = "User " + user + " is not online";
}
else {
Long uptimeSeconds = Long.valueOf(uptime);
getDiffMinutes = uptimeSeconds.longValue() / 1000 / 60;
getDiffHours = getDiffMinutes / 60;
getDiffDays = getDiffHours / 24;
if (getDiffMinutes >= 60)
getDiffMinutes = getDiffMinutes % 60;
if (getDiffHours >= 24)
getDiffHours = getDiffHours % 24;
if (user.equalsIgnoreCase(MetaData.SYSTEM_VAR))
displayText = "Server has been up for ";
else
displayText = "User " + user + " has been online for ";
displayText += getDiffDays + " days, " + getDiffHours + " hours, " + " and "
+ getDiffMinutes + " Minutes";
}
ChatWindowDialog.getInstance().appendToChatroom(0, displayText);
} catch (NumberFormatException nfe) {}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -