会话层基本通信层,负责对消息的具体处理。
底层通信类与该会话层通信入口
/**
*
* @param netMessage 网络消息类,负责对消息编码与解码
*/
@Override
public void dealMessage(NetMessage netMessage) {
try {
//消息处理器
NetCommandProcesser.dealNetMessage(this,netMessage);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
package edu.xupt.cs.core;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class NetCommandProcesser {
public static void dealNetMessage(Object object,NetMessage netMessage) throws NoSuchMethodException,
InvocationTargetException, IllegalAccessException {
ECammand cammand = netMessage.getCammand();
String[] subStrings = cammand.name().split("_");
StringBuffer methodName = new StringBuffer("deal");
for (String subString : subStrings) {
methodName.append(subString.substring(0,1))
.append(subString.substring(1).toLowerCase());
}
Method method = object.getClass().getDeclaredMethod(methodName.toString(), NetMessage.class);
method.invoke(object,netMessage);
}
}
服务器每接收一个客户端,首先主动询问客户端身份,客户端回应一段加密的数据,如果服务器解密成功,则给予客户端一个ID,否则结束通信。
void whoAreYou() {
sendMessage(new NetMessage().setCammand(ECammand.WHO_ARE_YOU));
}
//客户端发送ip与当前系统时间组成的hascode,检测客户端合法性,
boolean cheakClient(String message) {
int index = message.indexOf("#");
String hashCode = message.substring(index + 1);
String para = message.substring(0,index);
if (para.hashCode() == Integer.valueOf(hashCode)) {
return true;
}
return false;
}
/**
* 客户端验证成功,给予客户端一个ID值
* @param netMessage
*/
void dealIAm(NetMessage netMessage) {
String message = netMessage.getMessage();
if (cheakClient(message)) {
clientIp = message.substring(0,message.indexOf("!"));
id = clientIp + "_" + System.currentTimeMillis();
server.getTempClientPool().removeClient(this);
server.getClientPool().addClient(this);
sendMessage(new NetMessage().setCammand(ECammand.ONLINE_PASS)
.setMessage(id));
server.publishMessage("客户端[" + id + "] 上线");
return;
}
closeChannel();
}
群发与单发
void dealToOthers(NetMessage netMessage) {
String message = netMessage.getMessage();
Interactive interactive = gson.fromJson(message, Interactive.class);
server.talkToOthers(interactive.getSourceId(),message);
}
void dealToOne(NetMessage netMessage) {
String message = netMessage.getMessage();
Interactive interactive = gson.fromJson(message, Interactive.class);
server.talkToOne(interactive.getTargetId(),message);
}
服务器端会话层接收客户端资源请求,dealRequest方法将其转发到Dao层,向数据库请求资源。
void dealRequest(NetMessage netMessage) {
try {
String action = netMessage.getAction();
int index = action.indexOf("#");
String request = action.substring(0,index);
String response = action.substring(index + 1);
Object result = server.getActionProcess().dealRequst(request, netMessage.getMessage());
sendMessage(new NetMessage().setCammand(ECammand.RESPONSE).setAction(response)
.setMessage(gson.toJson(result)));
} catch (Exception e) {
e.printStackTrace();
}
}
void expressOne(String targetId,String message) {
Interactive interactive = new Interactive();
interactive.setSourceId(client.getId()).setTargetId(targetId).setMessage(message);
sendMessage(new NetMessage().setCammand(ECammand.TO_ONE)
.setMessage(gson.toJson(interactive)));
}
void expressAll(String message) {
Interactive interactive = new Interactive();
interactive.setSourceId(client.getId()).setMessage(message);
sendMessage(new NetMessage().setCammand(ECammand.TO_OTHERS)
.setMessage(gson.toJson(interactive)));
}
void dealWhoAreYou(NetMessage netMessage) {
String message = socket.getLocalAddress().getHostAddress() + "!" + System.currentTimeMillis();
message += "#" + message.hashCode();
sendMessage(new NetMessage().setCammand(ECammand.I_AM)
.setMessage(message));
}
package edu.xupt.cs.core;
import com.google.gson.Gson;
import java.lang.reflect.InvocationTargetException;
import java.net.Socket;
class ServerConversation extends Communicate{
private final Server server;
private String clientIp;
private String id;
private static final Gson gson = new Gson();
ServerConversation(Socket socket,Server server) {
super(socket);
this.server = server;
}
String getClientIp() {
return clientIp;
}
String getId() {
return id;
}
void forceDown() {
sendMessage(new NetMessage().setCammand(ECammand.FORCE_DOWN));
closeChannel();
}
@Override
public void dealPeerDown() {
server.publishMessage("客户端[" + id + "] 宕机");
server.getTempClientPool().removeClient(this);
server.getClientPool().removeClient(id);
}
/**
*
* @param netMessage 网络消息类,负责对消息编码与解码
*/
@Override
public void dealMessage(NetMessage netMessage) {
try {
NetCommandProcesser.dealNetMessage(this,netMessage);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
void dealRequest(NetMessage netMessage) {
try {
String action = netMessage.getAction();
int index = action.indexOf("#");
String request = action.substring(0,index);
String response = action.substring(index + 1);
Object result = server.getActionProcess().dealRequst(request, netMessage.getMessage());
sendMessage(new NetMessage().setCammand(ECammand.RESPONSE).setAction(response)
.setMessage(gson.toJson(result)));
} catch (Exception e) {
e.printStackTrace();
}
}
void dealToOthers(NetMessage netMessage) {
String message = netMessage.getMessage();
Interactive interactive = gson.fromJson(message, Interactive.class);
server.talkToOthers(interactive.getSourceId(),message);
}
void dealToOne(NetMessage netMessage) {
String message = netMessage.getMessage();
Interactive interactive = gson.fromJson(message, Interactive.class);
server.talkToOne(interactive.getTargetId(),message);
}
void toOne(String message) {
sendMessage(new NetMessage().setCammand(ECammand.TO_ONE)
.setMessage(message));
}
void talkToOthers(String message) {
sendMessage(new NetMessage().setCammand(ECammand.TO_OTHERS)
.setMessage(message));
}
void dealOffline(NetMessage netMessage) {
sendMessage(new NetMessage().setCammand(ECammand.OFFLINE));
server.getClientPool().removeClient(id);
closeChannel();
server.publishMessage("客户端[" + id + "] 下线");
}
/**
* 客户端验证成功,给予客户端一个ID值
* @param netMessage
*/
void dealIAm(NetMessage netMessage) {
String message = netMessage.getMessage();
if (cheakClient(message)) {
clientIp = message.substring(0,message.indexOf("!"));
id = clientIp + "_" + System.currentTimeMillis();
server.getTempClientPool().removeClient(this);
server.getClientPool().addClient(this);
sendMessage(new NetMessage().setCammand(ECammand.ONLINE_PASS)
.setMessage(id));
server.publishMessage("客户端[" + id + "] 上线");
return;
}
closeChannel();
}
boolean cheakClient(String message) {
int index = message.indexOf("#");
String hashCode = message.substring(index + 1);
String para = message.substring(0,index);
return Integer.parseInt(hashCode) == para.hashCode();
}
void whoAreYou() {
sendMessage(new NetMessage().setCammand(ECammand.WHO_ARE_YOU));
}
void outOfRoom() {
sendMessage(new NetMessage().setCammand(ECammand.OUT_OF_ROOM));
closeChannel();
}
}
package edu.xupt.cs.core;
import com.google.gson.Gson;
import java.lang.reflect.InvocationTargetException;
import java.net.Socket;
class ClientConversation extends Communicate{
private Client client;
private String serverIp;
private static final Gson gson = new Gson();
ClientConversation(Socket socket,Client client) {
super(socket);
this.client = client;
serverIp = socket.getInetAddress().getHostAddress();
}
@Override
public void dealMessage(NetMessage netMessage) {
try {
NetCommandProcesser.dealNetMessage(this,netMessage);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
void dealToOthers(NetMessage netMessage) {
Interactive interactive = gson.fromJson(netMessage.getMessage(), Interactive.class);
client.getClientAction().dealTalkToOthers(interactive.getSourceId(),interactive.getMessage());
}
void dealOnlinePass(NetMessage netMessage) {
client.setId(netMessage.getMessage());
client.getClientAction().afterConnectionToServer();
}
void dealResponse(NetMessage netMessage) {
try {
client.getActionProcess().dealResponse(netMessage.getAction(),netMessage.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
}
void dealOffline(NetMessage netMessage) {
closeChannel();
client.getClientAction().afterOffLine();
}
void dealToOne(NetMessage netMessage) {
Interactive interactive = gson.fromJson(netMessage.getMessage(), Interactive.class);
client.getClientAction().dealTalkToOne(interactive.getSourceId(),interactive.getMessage());
}
void dealOutOfRoom(NetMessage netMessage) {
closeChannel();
client.getClientAction().serverOutOfRoom();
}
void expressOne(String targetId,String message) {
Interactive interactive = new Interactive();
interactive.setSourceId(client.getId()).setTargetId(targetId).setMessage(message);
sendMessage(new NetMessage().setCammand(ECammand.TO_ONE)
.setMessage(gson.toJson(interactive)));
}
void expressAll(String message) {
Interactive interactive = new Interactive();
interactive.setSourceId(client.getId()).setMessage(message);
sendMessage(new NetMessage().setCammand(ECammand.TO_OTHERS)
.setMessage(gson.toJson(interactive)));
}
void dealForceDown(NetMessage netMessage) {
closeChannel();
client.getClientAction().serverForceDrop();
}
void dealWhoAreYou(NetMessage netMessage) {
String message = socket.getLocalAddress().getHostAddress() + "!" + System.currentTimeMillis();
message += "#" + message.hashCode();
sendMessage(new NetMessage().setCammand(ECammand.I_AM)
.setMessage(message));
}
void offLine() {
client.getClientAction().offLine();
sendMessage(new NetMessage().setCammand(ECammand.OFFLINE));
}
@Override
public void dealPeerDown() {
client.getClientAction().serverPeerDrop();
}
void request(String requset, String response, String parameter) {
sendMessage(new NetMessage().setCammand(ECammand.REQUEST)
.setAction(requset + "#" + response).setMessage(parameter));
}
}