use of com.firenio.component.ChannelConnector in project baseio by generallycloud.
the class TestProtobaseHeartBeat method main.
public static void main(String[] args) throws Exception {
IoEventHandle eventHandleAdaptor = new IoEventHandle() {
@Override
public void accept(Channel ch, Frame frame) throws Exception {
DebugUtil.debug("______________" + frame);
}
};
NioEventLoopGroup group = new NioEventLoopGroup();
group.setIdleTime(20);
ChannelConnector context = new ChannelConnector(group, "127.0.0.1", 8300);
context.addChannelIdleEventListener(new ChannelActiveListener());
context.addChannelEventListener(new LoggerChannelOpenListener());
context.addProtocolCodec(new ProtobaseCodec());
context.setIoEventHandle(eventHandleAdaptor);
Channel ch = context.connect();
String param = "tttt";
long old = Util.now_f();
for (int i = 0; i < 5; i++) {
Frame f = new ProtobaseFrame();
f.setString(param, ch);
ch.writeAndFlush(f);
Util.sleep(300);
}
System.out.println("Time:" + (Util.past(old)));
Thread.sleep(2000);
Util.close(context);
}
use of com.firenio.component.ChannelConnector in project baseio by generallycloud.
the class TestReconnectClient method main.
public static void main(String[] args) throws Exception {
IoEventHandle eventHandleAdaptor = new IoEventHandle() {
@Override
public void accept(Channel ch, Frame frame) throws Exception {
}
};
ChannelConnector context = new ChannelConnector(8300);
ReConnector connector = new ReConnector(context);
connector.setRetryTime(5000);
context.setIoEventHandle(eventHandleAdaptor);
context.addChannelEventListener(new LoggerChannelOpenListener());
context.addProtocolCodec(new LengthValueCodec());
// context.addChannelEventListener(new CloseConnectorSEListener(connector.getRealConnector()));
connector.connect();
int count = 99999;
for (int i = 0; ; i++) {
Util.sleep(1000);
if (i > count) {
break;
}
}
Util.close(connector);
}
use of com.firenio.component.ChannelConnector in project baseio by generallycloud.
the class TestReconnectClient2 method main.
public static void main(String[] args) throws Exception {
IoEventHandle eventHandleAdaptor = new IoEventHandle() {
@Override
public void accept(Channel ch, Frame frame) throws Exception {
}
};
ChannelConnector context = new ChannelConnector(8087);
context.setIoEventHandle(eventHandleAdaptor);
context.addChannelEventListener(new LoggerChannelOpenListener());
context.addProtocolCodec(new LengthValueCodec());
context.connect();
int count = 5;
for (int i = 0; i < count; i++) {
context.getChannel().close();
context.connect();
}
Util.sleep(Long.MAX_VALUE);
Util.close(context);
}
use of com.firenio.component.ChannelConnector in project baseio by generallycloud.
the class HttpProxyServer method strtup.
public synchronized void strtup(NioEventLoopGroup group, int port) throws Exception {
if (context != null && context.isActive()) {
return;
}
IoEventHandle eventHandle = new IoEventHandle() {
@Override
public void accept(Channel ch_src, Frame frame) throws Exception {
final HttpFrame f = (HttpFrame) frame;
if (f.getMethod() == HttpMethod.CONNECT) {
ch_src.writeAndFlush(CONNECT_RES_BUF.duplicate());
HttpProxyAttr s = HttpProxyAttr.get(ch_src);
String[] arr = f.getHost().split(":");
s.host = arr[0];
s.port = Integer.parseInt(arr[1]);
s.handshakeFinished = true;
} else {
String host = f.getHost();
String[] arr = host.split(":");
int port = 80;
if (arr.length == 2) {
port = Integer.parseInt(arr[1]);
}
if (f.getRequestHeaders().remove(HttpHeader.Proxy_Connection.getId()) == null) {
return;
}
NioEventLoop el = ch_src.getEventLoop();
ChannelConnector context = new ChannelConnector(el, arr[0], port);
context.addProtocolCodec(new ClientHttpCodec());
context.setIoEventHandle(new IoEventHandle() {
@Override
public void accept(Channel ch, Frame frame) throws Exception {
ClientHttpFrame res = (ClientHttpFrame) frame;
IntObjectMap<String> hs = res.getResponse_headers();
for (hs.scan(); hs.hasNext(); ) {
String v = hs.getValue();
if (v == null) {
continue;
}
if (hs.getKey() == HttpHeader.Content_Length.getId() || hs.getKey() == HttpHeader.Connection.getId() || hs.getKey() == HttpHeader.Transfer_Encoding.getId() || hs.getKey() == HttpHeader.Content_Encoding.getId()) {
continue;
}
f.setResponseHeader(hs.getKey(), v.getBytes());
}
if (res.getContent() != null) {
f.setContent(res.getContent());
} else if (res.isChunked()) {
f.setString("not support chunked now.", ch);
}
ch_src.writeAndFlush(f);
ch.close();
}
});
String url = parseRequestURL(f.getRequestURL());
context.setPrintConfig(false);
context.addChannelEventListener(new LoggerChannelOpenListener());
context.connect((ch, ex) -> {
if (ex == null) {
ClientHttpFrame req = new ClientHttpFrame(url, f.getMethod());
req.setRequestHeaders(f.getRequestHeaders());
req.getRequestHeaders().remove(HttpHeader.Proxy_Connection.getId());
if (f.getMethod() == HttpMethod.POST) {
req.setContent(f.getContent());
}
try {
ch.writeAndFlush(req);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
};
context = new ChannelAcceptor(group, 8088);
context.addProtocolCodec(new HttpProxyCodec());
context.setIoEventHandle(eventHandle);
context.addChannelEventListener(new LoggerChannelOpenListener());
context.bind();
}
Aggregations