Skip to content

Commit

Permalink
删除无用代码
Browse files Browse the repository at this point in the history
  • Loading branch information
ohun committed Dec 29, 2015
1 parent c589edb commit 0435b60
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 272 deletions.
45 changes: 0 additions & 45 deletions mpush-api/src/main/java/com/shinemo/mpush/api/AbstractHandler.java

This file was deleted.

This file was deleted.

@@ -1,14 +1,13 @@
package com.shinemo.mpush.core.handler;


import com.shinemo.mpush.api.protocol.Handler;
import com.shinemo.mpush.api.protocol.Packet;
import com.shinemo.mpush.api.AbstractHandler;
import com.shinemo.mpush.api.Connection;
import com.shinemo.mpush.api.Receiver;
import com.shinemo.mpush.core.ConnectionManager;
import com.shinemo.mpush.core.NettyConnection;

import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

import org.slf4j.Logger;
Expand All @@ -17,7 +16,7 @@
/**
* Created by ohun on 2015/12/19.
*/
public class ServerHandler extends AbstractHandler implements Handler {
public class ServerHandler extends ChannelHandlerAdapter {

private static final Logger log = LoggerFactory.getLogger(ServerHandler.class);

Expand Down
Expand Up @@ -2,7 +2,6 @@

import com.shinemo.mpush.api.Connection;
import com.shinemo.mpush.api.protocol.Command;
import com.shinemo.mpush.api.protocol.Handler;
import com.shinemo.mpush.api.protocol.Packet;
import com.shinemo.mpush.core.NettyConnection;
import com.shinemo.mpush.core.message.HandShakeMessage;
Expand All @@ -11,6 +10,7 @@
import com.shinemo.mpush.core.security.CipherBox;
import com.shinemo.mpush.tools.Strings;

import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;

Expand All @@ -23,7 +23,7 @@
/**
* Created by ohun on 2015/12/24.
*/
public class ClientHandler implements Handler {
public class ClientHandler extends ChannelHandlerAdapter {
private static final Logger LOGGER = LoggerFactory.getLogger(ClientHandler.class);
private byte[] clientKey = CipherBox.INSTANCE.randomAESKey();
private byte[] iv = CipherBox.INSTANCE.randomAESIV();
Expand Down Expand Up @@ -89,34 +89,4 @@ private String getToken() {
}
return Strings.EMPTY;
}

@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {

}

@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {

}

@Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {

}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {

}

@Override
public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) throws Exception {

}

@Override
public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {

}
}
Expand Up @@ -3,14 +3,14 @@

import java.util.concurrent.locks.LockSupport;

import io.netty.channel.ChannelHandler;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.shinemo.mpush.api.Client;
import com.shinemo.mpush.api.protocol.Handler;
import com.shinemo.mpush.netty.client.NettyClientFactory;

/**
Expand All @@ -22,7 +22,7 @@ public class NettyClientTest {

private String host = "127.0.0.1";
private int port = 3000;
private Handler handler = new ClientHandler();
private ChannelHandler handler = new ClientHandler();

@Test
public void testClient() throws Exception {
Expand Down
@@ -1,10 +1,10 @@
package com.shinemo.mpush.core.netty;

import com.shinemo.mpush.api.Receiver;
import com.shinemo.mpush.api.protocol.Handler;
import com.shinemo.mpush.core.MessageDispatcher;
import com.shinemo.mpush.core.handler.ServerHandler;
import com.shinemo.mpush.netty.server.NettyServer;
import io.netty.channel.ChannelHandler;
import org.junit.Test;

/**
Expand All @@ -21,7 +21,7 @@ public void testStop() throws Exception {
public void testStart() throws Exception {

Receiver receiver = new MessageDispatcher();
Handler handler = new ServerHandler(receiver);
ChannelHandler handler = new ServerHandler(receiver);

final NettyServer server = new NettyServer(3000, handler);
server.start();
Expand Down
Expand Up @@ -4,6 +4,7 @@
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

import io.netty.channel.ChannelHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -12,72 +13,72 @@
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;
import com.shinemo.mpush.api.Client;
import com.shinemo.mpush.api.protocol.Handler;

public abstract class AbstractNettyClientFactory {

private static final String format = "%s:%s";

private static final Logger log = LoggerFactory.getLogger(AbstractNettyClientFactory.class);

/**
* host:port
*/
private final Cache<String, Client> cachedClients = CacheBuilder.newBuilder()//
.maximumSize(2 << 17)// 最大是65535*2
.expireAfterAccess(2*60, TimeUnit.MINUTES)// 如果经过120分钟没有访问,释放掉连接,缓解内存和服务器连接压力
.removalListener(new RemovalListener<String, Client>() {
@Override
public void onRemoval(RemovalNotification<String, Client> notification) {
if (notification.getValue().isConnected()) {
notification.getValue().close("[Remoting] removed from cache");
}
}
})//
.build();

/**
* 不存在,则创建
* @param remoteHost
* @param port
* @param handler
* @return
* @throws Exception
*/
public Client get(final String remoteHost,final int port,final Handler handler) throws Exception {
final String key = String.format(format, remoteHost,port);
private static final String format = "%s:%s";

private static final Logger log = LoggerFactory.getLogger(AbstractNettyClientFactory.class);

/**
* host:port
*/
private final Cache<String, Client> cachedClients = CacheBuilder.newBuilder()//
.maximumSize(2 << 17)// 最大是65535*2
.expireAfterAccess(2 * 60, TimeUnit.MINUTES)// 如果经过120分钟没有访问,释放掉连接,缓解内存和服务器连接压力
.removalListener(new RemovalListener<String, Client>() {
@Override
public void onRemoval(RemovalNotification<String, Client> notification) {
if (notification.getValue().isConnected()) {
notification.getValue().close("[Remoting] removed from cache");
}
}
})//
.build();

/**
* 不存在,则创建
*
* @param remoteHost
* @param port
* @param handler
* @return
* @throws Exception
*/
public Client get(final String remoteHost, final int port, final ChannelHandler handler) throws Exception {
final String key = String.format(format, remoteHost, port);
Client client = cachedClients.get(key, new Callable<Client>() {
@Override
public Client call() throws Exception {
Client client = createClient(remoteHost,port,handler);
Client client = createClient(remoteHost, port, handler);
if (client != null) {
client.startHeartBeat();
}
return client;
}
});
if (client == null || !client.isConnected()) {
cachedClients.invalidate(key);
cachedClients.invalidate(key);
return null;
}
return client;
}
public Client get(final String remoteHost,final int port) throws Exception {
return get(remoteHost, port, null);

public Client get(final String remoteHost, final int port) throws Exception {
return get(remoteHost, port, null);
}
protected Client createClient(final String remoteHost,final int port) throws Exception{
return createClient(remoteHost, port, null);
}
protected abstract Client createClient(final String remoteHost,final int port,Handler handler) throws Exception;


protected Client createClient(final String remoteHost, final int port) throws Exception {
return createClient(remoteHost, port, null);
}

protected abstract Client createClient(final String remoteHost, final int port, ChannelHandler handler) throws Exception;


public void remove(Client client) {
if (client != null) {
cachedClients.invalidate(client.getUrl());
cachedClients.invalidate(client.getUrl());
log.warn(MessageFormat.format("[Remoting] {0} is removed", client));
}
}
Expand Down
Expand Up @@ -3,22 +3,15 @@
import java.net.InetSocketAddress;

import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.shinemo.mpush.api.Client;
import com.shinemo.mpush.api.protocol.Handler;
import com.shinemo.mpush.netty.codec.PacketDecoder;
import com.shinemo.mpush.netty.codec.PacketEncoder;
import com.shinemo.mpush.netty.util.NettySharedHandler;
import com.shinemo.mpush.netty.util.NettySharedHolder;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
Expand All @@ -29,10 +22,9 @@ public class NettyClientFactory extends AbstractNettyClientFactory {

public static NettyClientFactory instance = new NettyClientFactory();

protected Client createClient(final String host, final int port, final Handler handler) throws Exception {
protected Client createClient(final String host, final int port, final ChannelHandler handler) throws Exception {
EventLoopGroup workerGroup = new NioEventLoopGroup();
final Bootstrap bootstrap = new Bootstrap();
final NettySharedHandler nettySharedHandler = new NettySharedHandler(handler);
bootstrap.group(workerGroup)//
.option(ChannelOption.TCP_NODELAY, true)//
.option(ChannelOption.SO_REUSEADDR, true)//
Expand All @@ -46,7 +38,7 @@ protected Client createClient(final String host, final int port, final Handler h
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new PacketDecoder());
ch.pipeline().addLast(PacketEncoder.INSTANCE);
ch.pipeline().addLast(nettySharedHandler);
ch.pipeline().addLast(handler);
}
});

Expand Down

0 comments on commit 0435b60

Please sign in to comment.