init netty client
This commit is contained in:
parent
98c83ea8c5
commit
14a2bcf170
|
@ -0,0 +1,39 @@
|
|||
package r_memcached;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.concurrent.Executors;
|
||||
import org.jboss.netty.bootstrap.ClientBootstrap;
|
||||
import org.jboss.netty.channel.ChannelFuture;
|
||||
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
|
||||
|
||||
public class Client
|
||||
{
|
||||
ClientBootstrap bootstrap;
|
||||
ChannelFuture channelFuture;
|
||||
String host;
|
||||
int port;
|
||||
int id;
|
||||
|
||||
public boolean init(String host, int port)
|
||||
{
|
||||
bootstrap = new ClientBootstrap(
|
||||
new NioClientSocketChannelFactory(
|
||||
Executors.newCachedThreadPool(),
|
||||
Executors.newCachedThreadPool()));
|
||||
// Set up the event pipeline factory.
|
||||
bootstrap.setPipelineFactory(new MClientPipelineFactory());
|
||||
// Start the connection attempt.
|
||||
channelFuture = bootstrap.connect(new InetSocketAddress(host, port));
|
||||
return channelFuture.isSuccess();
|
||||
}
|
||||
|
||||
public void stop()
|
||||
{
|
||||
channelFuture.awaitUninterruptibly();
|
||||
if (!channelFuture.isSuccess())
|
||||
{
|
||||
channelFuture.getCause().printStackTrace();
|
||||
}
|
||||
channelFuture.getChannel().getCloseFuture().awaitUninterruptibly();
|
||||
bootstrap.releaseExternalResources();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,81 @@
|
|||
package r_memcached;
|
||||
|
||||
|
||||
import messageBody.requestMsg.nr_Connected_mem;
|
||||
import messageBody.requestMsg.nr_Read_res;
|
||||
import messageBody.requestMsg.nr_write_res;
|
||||
|
||||
import org.jboss.netty.channel.ChannelHandlerContext;
|
||||
import org.jboss.netty.channel.ChannelStateEvent;
|
||||
import org.jboss.netty.channel.ExceptionEvent;
|
||||
import org.jboss.netty.channel.MessageEvent;
|
||||
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
|
||||
|
||||
import server.NetMsg;
|
||||
import server.webSession;
|
||||
import common.EMSGID;
|
||||
|
||||
|
||||
public class MClientHandler extends SimpleChannelUpstreamHandler
|
||||
{
|
||||
|
||||
private static int ticks=0;
|
||||
private static long diffTime = 0;
|
||||
|
||||
@Override
|
||||
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e)
|
||||
{
|
||||
nr_Connected_mem.Builder builder = nr_Connected_mem.newBuilder();
|
||||
NetMsg send = NetMsg.newMessage();
|
||||
send.setMsgID(EMSGID.nr_connected_mem);
|
||||
send.setMessageLite(builder);
|
||||
|
||||
e.getChannel().write(send);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
|
||||
{
|
||||
if (!(e.getMessage() instanceof NetMsg))
|
||||
{
|
||||
return;
|
||||
}
|
||||
webSession.getInstance().addSession(e);
|
||||
|
||||
|
||||
|
||||
// NetMsg msg = (NetMsg)e.getMessage();
|
||||
// if (msg.getMsgID() == EMSGID.nr_read_res || msg.getMsgID() == EMSGID.nr_write_res)
|
||||
// {
|
||||
// method();
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// webSession.getInstance().addSession(e);
|
||||
// }
|
||||
}
|
||||
|
||||
public synchronized static void method()
|
||||
{
|
||||
if (ticks == 0)
|
||||
{
|
||||
diffTime = System.nanoTime();
|
||||
}
|
||||
ticks++;
|
||||
if (ticks == 5000)
|
||||
{
|
||||
System.out.println((System.nanoTime()-diffTime)/1000000.0);
|
||||
ticks = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
|
||||
{
|
||||
if (e.getChannel().getLocalAddress() == null) {
|
||||
return;
|
||||
}
|
||||
webSession.getInstance().removeClientChannel(e.getChannel());
|
||||
e.getChannel().close();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package r_memcached;
|
||||
|
||||
import org.jboss.netty.channel.ChannelPipeline;
|
||||
import org.jboss.netty.channel.ChannelPipelineFactory;
|
||||
import org.jboss.netty.channel.Channels;
|
||||
|
||||
import server.MDecoder;
|
||||
import server.MEncoder;
|
||||
|
||||
public class MClientPipelineFactory implements ChannelPipelineFactory
|
||||
{
|
||||
|
||||
public ChannelPipeline getPipeline() throws Exception
|
||||
{
|
||||
ChannelPipeline pipeline = Channels.pipeline();
|
||||
|
||||
pipeline.addLast("decoder", new MDecoder());
|
||||
pipeline.addLast("encoder", new MEncoder());
|
||||
pipeline.addLast("handler", new MClientHandler());
|
||||
return pipeline;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue