link.jfire.socket.socketserver.handler.impl.ServerInfoHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jfire-socket Show documentation
Show all versions of jfire-socket Show documentation
Jfire - socket is a server-side framework based on AIO. Users only need a simple implementation of a business logic processing interface can be the business data processing. The framework provides the client and server at the same time. Have strong connection capacity. Single server provides tens of thousands of connections.
The newest version!
package link.jfire.socket.socketserver.handler.impl;
import java.nio.charset.Charset;
import javax.annotation.Resource;
import link.jfire.baseutil.collection.ByteCache;
import link.jfire.baseutil.simplelog.ConsoleLogFactory;
import link.jfire.baseutil.simplelog.Logger;
import link.jfire.codejson.JsonArray;
import link.jfire.codejson.JsonObject;
import link.jfire.socket.socketserver.bus.BusinessCommand;
import link.jfire.socket.socketserver.bus.Message;
import link.jfire.socket.socketserver.bus.MessageHandlerCenter;
import link.jfire.socket.socketserver.handler.MessageHandler;
import link.jfire.socket.socketserver.interceptor.MessageInterceptor;
@Resource
public class ServerInfoHandler implements MessageHandler
{
private Logger logger = ConsoleLogFactory.getLogger();
private Charset charset = Charset.forName("utf8");
@Resource
private MessageHandlerCenter requestHandlerCenter;
@Override
public byte interestedDataPacketType()
{
return BusinessCommand.SERVER_INFO;
}
@Override
public void handler(Message message)
{
ByteCache cache = message.getBusinessData();
String command = cache.toString(charset);
JsonObject jsonObject = new JsonObject();
switch (command)
{
case "all":
jsonObject.put("handlers", getHandlerNames());
jsonObject.put("interceptors", getInterceptorNames());
logger.info(jsonObject.toString());
break;
case "handler":
jsonObject.put("handlers", getHandlerNames());
break;
case "interceptor":
jsonObject.put("interceptors", getInterceptorNames());
break;
default:
break;
}
cache.clear().putArray(jsonObject.toString().getBytes(charset));
cache.startRead(0);
}
private JsonArray getHandlerNames()
{
JsonArray jsonArray = new JsonArray();
for (MessageHandler handler : requestHandlerCenter.getHandlerList())
{
jsonArray.add(handler.getClass().getName());
}
return jsonArray;
}
private JsonArray getInterceptorNames()
{
JsonArray jsonArray = new JsonArray();
for (MessageInterceptor interceptor : requestHandlerCenter.getInterceptorList())
{
jsonArray.add(interceptor.getClass().getName());
}
return jsonArray;
}
}