Skip to content

Commit

Permalink
【功能优化】优化log模块
Browse files Browse the repository at this point in the history
  • Loading branch information
Feng14 committed Apr 28, 2015
1 parent c7aa240 commit 4e3e1d2
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 42 deletions.
43 changes: 6 additions & 37 deletions applicationContext.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,12 @@
<ref bean="ServerModel" />
</property>
</bean>
<!-- <bean id="MinaServerHandle" class="server.MinaServerHandle" />

<bean id="MinaEncoder" class="server.MinaEncoder" />
<bean id="MinaDecoder" class="server.MinaDecoder" />
<bean id="snmpCodecFilter" class="org.apache.mina.filter.codec.ProtocolCodecFilter">
<constructor-arg index="0" ref="MinaEncoder" />
<constructor-arg index="1" ref="MinaDecoder" />
</bean> -->

<!-- The filter chain. -->
<!-- <bean id="filterChainBuilder"
class="org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder">
<property name="filters">
<map>
<entry key="codecFilter" value-ref="snmpCodecFilter" />
</map>
</property>
</bean>
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.net.SocketAddress">
<bean class="org.apache.mina.integration.beans.InetSocketAddressEditor" />
</entry>
</map>
<bean id="MyLogger" class="server.MyLogger">
<property name="serverModel">
<ref bean="ServerModel" />
</property>
</bean>
The IoAcceptor which binds to port 161
<bean id="ioAcceptor"
class="org.apache.mina.transport.socket.nio.NioSocketAcceptor"
init-method="bind" destroy-method="unbind">
<property name="defaultLocalAddress" value=":8081" />
<property name="handler" ref="MinaServerHandle" />
<property name="filterChainBuilder" ref="filterChainBuilder" />
</bean> -->



<bean id="Server" class="server.Server">
<property name="serverNetwork">
Expand All @@ -78,6 +44,9 @@
<property name="minaServerHandle">
<ref bean="MinaServerHandle" />
</property>
<property name="myLogger">
<ref bean="MyLogger" />
</property>
<!-- <property name="protocolCodecFilter">
<ref bean="snmpCodecFilter" />
</property> -->
Expand Down
2 changes: 1 addition & 1 deletion src/server/MinaEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void encode(IoSession ioSession, Object message, ProtocolEncoderOutput ou
buffer.flip();
output.write(buffer);

showPacket(ioSession, sizeOfAll, byteArray);
// showPacket(ioSession, sizeOfAll, byteArray);
return;
}

Expand Down
61 changes: 61 additions & 0 deletions src/server/MyLogger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package server;

import org.apache.log4j.Logger;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.core.write.WriteRequest;
import org.apache.mina.filter.logging.LoggingFilter;

import protocol.ProtoHead;
import tools.DataTypeTranslater;

public class MyLogger extends LoggingFilter {
private Logger logger = Logger.getLogger(this.getClass());
private ServerModel serverModel;

public ServerModel getServerModel() {
return serverModel;
}

public void setServerModel(ServerModel serverModel) {
this.serverModel = serverModel;
}

@Override
public void messageReceived(NextFilter nextFilter, IoSession session, Object message) throws Exception {
// TODO Auto-generated method stub
super.messageReceived(nextFilter, session, message);
}

@Override
public void messageSent(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception {


// TODO Auto-generated method stub
super.messageSent(nextFilter, session, writeRequest);
showPacket(session, writeRequest);
}

/**
* log要发送给client的包
* @param ioSession
* @param size
* @param byteArray
*/
private PacketFromServer packetFromServer;
private String userId;
private void showPacket(IoSession ioSession, WriteRequest writeRequest) {
userId = null;
try {
userId = serverModel.getClientUserFromTable(ioSession).userId;
} catch (Exception e) {
}
packetFromServer = (PacketFromServer)writeRequest.getMessage();
try {
logger.info("Server send packet to client(" + ServerModel.getIoSessionKey(ioSession) + "); MessageType : "
+ ProtoHead.ENetworkMessage.valueOf(packetFromServer.getMessageType()).toString() + "; MessageId : "
+ DataTypeTranslater.bytesToInt(packetFromServer.getMessageID(), 0) + "; UserId : " + userId.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
6 changes: 4 additions & 2 deletions src/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
*
*/
public class Server {
public static boolean keepAliveSwitch = true;
// public static boolean keepAliveSwitch = false;
// public static boolean keepAliveSwitch = true;
public static boolean keepAliveSwitch = false;
static Logger logger = Logger.getLogger(Server.class);

public static Server instance;
Expand Down Expand Up @@ -125,6 +125,8 @@ public static void main(String[] args) throws IOException {
// File file = new File("src/applicationContext.xml");
// System.out.println(file.exists());
ApplicationContext ctx = new FileSystemXmlApplicationContext("applicationContext.xml");
((ServerNetwork) ctx.getBean("ServerNetwork")).init();
((ServerModel)ctx.getBean("ServerModel")).init();
// ServerNetwork serverNetwork = (ServerNetwork)ctx.getBean("ServerNetwork");
}
}
2 changes: 1 addition & 1 deletion src/server/ServerModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class ServerModel extends Observable {
// Hashtable<String, WaitClientResponse>();

public ServerModel() {
init();
// init();
}

public ServerModel getServerModel() {
Expand Down
13 changes: 12 additions & 1 deletion src/server/ServerNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import org.apache.log4j.Logger;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.filterchain.IoFilter;
import org.apache.mina.core.future.IoFuture;
import org.apache.mina.core.future.IoFutureListener;
import org.apache.mina.core.future.WriteFuture;
Expand Down Expand Up @@ -39,6 +40,7 @@ public class ServerNetwork {
private ClientRequest_Dispatcher clientRequest_Dispatcher;
private MinaServerHandle minaServerHandle;
private ProtocolCodecFilter protocolCodecFilter;
private MyLogger myLogger;
private InetAddress addr;

Logger logger = Logger.getLogger(ServerNetwork.class);
Expand Down Expand Up @@ -74,6 +76,8 @@ public void init() {
acceptor = new NioSocketAcceptor();
// 指定编码解码器
acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MinaEncoder(), new MinaDecoder()));
acceptor.getFilterChain().addLast("Logging", myLogger);
// acceptor.getFilterChain().addLast("Logging", new MyLogger());
// System.out.println("codec " + (protocolCodecFilter == null));
// acceptor.getFilterChain().addLast("codec", protocolCodecFilter);
acceptor.getSessionConfig().setMaxReadBufferSize(1024 * 8);
Expand Down Expand Up @@ -117,7 +121,6 @@ public MinaServerHandle getMinaServerHandle() {

public void setMinaServerHandle(MinaServerHandle minaServerHandle) {
this.minaServerHandle = minaServerHandle;
init();
}

public ProtocolCodecFilter getProtocolCodecFilter() {
Expand All @@ -128,6 +131,14 @@ public void setProtocolCodecFilter(ProtocolCodecFilter protocolCodecFilter) {
this.protocolCodecFilter = protocolCodecFilter;
}

public MyLogger getMyLogger() {
return myLogger;
}

public void setMyLogger(MyLogger myLogger) {
this.myLogger = myLogger;
}

/**
* 添加一个等待客户端回复的监听(服务器向客户端发送消息后,要求客户端回复)
*
Expand Down

0 comments on commit 4e3e1d2

Please sign in to comment.