Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
黄志磊
committed
Jan 19, 2016
1 parent
c984999
commit 1b435c9
Showing
15 changed files
with
357 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,25 @@ | ||
package com.shinemo.mpush.api; | ||
|
||
import io.netty.channel.ChannelHandler; | ||
|
||
public interface Client { | ||
|
||
void init(); | ||
|
||
void start(); | ||
boolean isConnected(); | ||
|
||
void stop(); | ||
String getHost(); | ||
|
||
boolean isConnected(); | ||
int getPort(); | ||
|
||
void close(String cause); | ||
|
||
boolean isEnabled(); | ||
|
||
void resetHbTimes(); | ||
|
||
int inceaseAndGetHbTimes(); | ||
|
||
String getUri(); | ||
String getUrl(); | ||
|
||
ChannelHandler getHandler(); | ||
void startHeartBeat() throws Exception; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 83 additions & 69 deletions
152
mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,109 @@ | ||
package com.shinemo.mpush.netty.client; | ||
|
||
import java.net.InetSocketAddress; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import com.shinemo.mpush.netty.codec.PacketDecoder; | ||
import com.shinemo.mpush.netty.codec.PacketEncoder; | ||
import io.netty.bootstrap.Bootstrap; | ||
import io.netty.buffer.PooledByteBufAllocator; | ||
import io.netty.channel.*; | ||
import io.netty.channel.nio.NioEventLoopGroup; | ||
import io.netty.channel.socket.SocketChannel; | ||
import io.netty.channel.socket.nio.NioSocketChannel; | ||
import io.netty.util.Timeout; | ||
import io.netty.util.TimerTask; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.shinemo.mpush.api.Client; | ||
import com.shinemo.mpush.api.protocol.Packet; | ||
import com.shinemo.mpush.netty.util.NettySharedHolder; | ||
import com.shinemo.mpush.tools.config.ConfigCenter; | ||
|
||
public class NettyClient implements Client { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(NettyClient.class); | ||
|
||
private static final String format = "%s:%s"; | ||
|
||
private static final Logger log = LoggerFactory.getLogger(NettyClient.class); | ||
|
||
private final ChannelHandler handler; | ||
private final String host; | ||
private final int port; | ||
private Channel channel; | ||
private int hbTimes; | ||
|
||
public NettyClient(final String host, final int port, ChannelHandler handler) { | ||
public NettyClient(final String host, final int port, Channel channel) { | ||
this.host = host; | ||
this.port = port; | ||
this.handler = handler; | ||
this.channel = channel; | ||
} | ||
|
||
@Override | ||
public void init() { | ||
this.stop(); | ||
EventLoopGroup workerGroup = new NioEventLoopGroup(); | ||
final Bootstrap bootstrap = new Bootstrap(); | ||
bootstrap.group(workerGroup)// | ||
.option(ChannelOption.TCP_NODELAY, true)// | ||
.option(ChannelOption.SO_REUSEADDR, true)// | ||
.option(ChannelOption.SO_KEEPALIVE, true)// | ||
.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)// | ||
.channel(NioSocketChannel.class) | ||
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000); | ||
|
||
bootstrap.handler(new ChannelInitializer<SocketChannel>() { // (4) | ||
@Override | ||
public void initChannel(SocketChannel ch) throws Exception { | ||
ch.pipeline().addLast(new PacketDecoder()); | ||
ch.pipeline().addLast(PacketEncoder.INSTANCE); | ||
ch.pipeline().addLast(handler); | ||
} | ||
}); | ||
|
||
ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); | ||
if (future.awaitUninterruptibly(4000) && future.isSuccess() && future.channel().isActive()) { | ||
channel = future.channel(); | ||
} else { | ||
future.cancel(true); | ||
future.channel().close(); | ||
LOGGER.warn("[remoting] failure to connect:" + host + "," + port); | ||
} | ||
} | ||
public void close(String cause) { | ||
if (!StringUtils.isBlank(cause) && !"null".equals(cause.trim())) { | ||
log.error("close channel:"+cause); | ||
} | ||
this.channel.close(); | ||
} | ||
|
||
@Override | ||
public void start() { | ||
if (channel != null) { | ||
try { | ||
channel.closeFuture().sync(); | ||
} catch (InterruptedException e) { | ||
} | ||
} | ||
} | ||
@Override | ||
public boolean isEnabled() { | ||
return channel.isWritable(); | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
if (channel != null) { | ||
channel.close(); | ||
} | ||
} | ||
@Override | ||
public boolean isConnected() { | ||
return channel.isActive(); | ||
} | ||
|
||
@Override | ||
public boolean isConnected() { | ||
return channel.isActive(); | ||
} | ||
@Override | ||
public void resetHbTimes() { | ||
hbTimes = 0; | ||
} | ||
|
||
@Override | ||
public String getUri() { | ||
return host + ":" + port; | ||
} | ||
@Override | ||
public int inceaseAndGetHbTimes() { | ||
return ++hbTimes; | ||
} | ||
|
||
@Override | ||
public void startHeartBeat() throws Exception { | ||
NettySharedHolder.HASHED_WHEEL_TIMER.newTimeout(new TimerTask() { | ||
@Override | ||
public void run(Timeout timeout) throws Exception { | ||
try { | ||
ChannelFuture channelFuture = channel.writeAndFlush(Packet.getHBPacket()); | ||
channelFuture.addListener(new ChannelFutureListener() { | ||
|
||
@Override | ||
public void operationComplete(ChannelFuture future) throws Exception { | ||
if (!future.isSuccess()) { | ||
if (!channel.isActive()) { | ||
log.warn("client send hb msg false:" + channel.remoteAddress().toString() + ",channel is not active"); | ||
} | ||
log.warn("client send msg hb false:" + channel.remoteAddress().toString()); | ||
} else { | ||
log.warn("client send msg hb success:" + channel.remoteAddress().toString()); | ||
} | ||
} | ||
}); | ||
} finally { | ||
if (channel.isActive()) { | ||
NettySharedHolder.HASHED_WHEEL_TIMER.newTimeout(this, ConfigCenter.holder.scanConnTaskCycle()/1000, TimeUnit.SECONDS); | ||
} | ||
} | ||
} | ||
}, ConfigCenter.holder.scanConnTaskCycle()/1000, TimeUnit.SECONDS); | ||
} | ||
|
||
|
||
@Override | ||
public String getUrl() { | ||
return String.format(format, host, port); | ||
} | ||
|
||
@Override | ||
public String getHost() { | ||
return host; | ||
} | ||
|
||
@Override | ||
public int getPort() { | ||
return port; | ||
} | ||
|
||
@Override | ||
public ChannelHandler getHandler() { | ||
return handler; | ||
} | ||
} |
Oops, something went wrong.