use of org.jboss.netty.channel.ChannelFuture in project dubbo by alibaba.
the class NettyClient method doConnect.
protected void doConnect() throws Throwable {
long start = System.currentTimeMillis();
ChannelFuture future = bootstrap.connect(getConnectAddress());
try {
boolean ret = future.awaitUninterruptibly(getConnectTimeout(), TimeUnit.MILLISECONDS);
if (ret && future.isSuccess()) {
Channel newChannel = future.getChannel();
newChannel.setInterestOps(Channel.OP_READ_WRITE);
try {
// Close old channel
// copy reference
Channel oldChannel = NettyClient.this.channel;
if (oldChannel != null) {
try {
if (logger.isInfoEnabled()) {
logger.info("Close old netty channel " + oldChannel + " on create new netty channel " + newChannel);
}
oldChannel.close();
} finally {
NettyChannel.removeChannelIfDisconnected(oldChannel);
}
}
} finally {
if (NettyClient.this.isClosed()) {
try {
if (logger.isInfoEnabled()) {
logger.info("Close new netty channel " + newChannel + ", because the client closed.");
}
newChannel.close();
} finally {
NettyClient.this.channel = null;
NettyChannel.removeChannelIfDisconnected(newChannel);
}
} else {
NettyClient.this.channel = newChannel;
}
}
} else if (future.getCause() != null) {
throw new RemotingException(this, "client(url: " + getUrl() + ") failed to connect to server " + getRemoteAddress() + ", error message is:" + future.getCause().getMessage(), future.getCause());
} else {
throw new RemotingException(this, "client(url: " + getUrl() + ") failed to connect to server " + getRemoteAddress() + " client-side timeout " + getConnectTimeout() + "ms (elapsed: " + (System.currentTimeMillis() - start) + "ms) from netty client " + NetUtils.getLocalHost() + " using dubbo version " + Version.getVersion());
}
} finally {
if (!isConnected()) {
future.cancel();
}
}
}
use of org.jboss.netty.channel.ChannelFuture in project Protocol-Adapter-OSLP by OSGP.
the class OslpChannelHandler method send.
public OslpEnvelope send(final InetSocketAddress address, final OslpEnvelope request, final String deviceIdentification) throws IOException, DeviceSimulatorException {
LOGGER.info("Sending OSLP request: {}", request.getPayloadMessage());
final Callback callback = new Callback(this.connectionTimeout);
this.lock.lock();
// Open connection and send message
ChannelFuture channelFuture = null;
try {
channelFuture = this.bootstrap.connect(address);
channelFuture.awaitUninterruptibly(this.connectionTimeout, TimeUnit.MILLISECONDS);
if (channelFuture.getChannel() != null && channelFuture.getChannel().isConnected()) {
LOGGER.info("Connection established to: {}", address);
} else {
LOGGER.info("The connnection to the device {} is not successfull", deviceIdentification);
LOGGER.warn("Unable to connect to: {}", address);
throw new IOException("Unable to connect");
}
this.callbacks.put(channelFuture.getChannel().getId(), callback);
channelFuture.getChannel().write(request);
} finally {
this.lock.unlock();
}
// wait for response and close connection
try {
final OslpEnvelope response = callback.get(deviceIdentification);
LOGGER.info("Received OSLP response (after callback): {}", response.getPayloadMessage());
/*
* Devices expect the channel to be closed if (and only if) the
* platform initiated the conversation. If the device initiated the
* conversation it needs to close the channel itself.
*/
channelFuture.getChannel().close();
return response;
} catch (final IOException | DeviceSimulatorException e) {
LOGGER.error("send exception", e);
// Remove callback when exception has occurred
this.callbacks.remove(channelFuture.getChannel().getId());
throw e;
}
}
use of org.jboss.netty.channel.ChannelFuture in project Protocol-Adapter-OSLP by OSGP.
the class OslpChannelHandlerClient method send.
public void send(final InetSocketAddress address, final OslpEnvelope request, final OslpResponseHandler responseHandler, final String deviceIdentification) throws IOException {
LOGGER.info("Sending OSLP request: {}", request.getPayloadMessage());
// Open connection and send message.
final ChannelFuture channelFuture = this.bootstrap.connect(address);
this.callbackHandlers.put(channelFuture.getChannel().getId(), new OslpCallbackHandler(responseHandler));
channelFuture.addListener(new ChannelFutureListener() {
@Autowired
protected DeviceResponseMessageSender responseMessageSender;
@Override
public void operationComplete(final ChannelFuture future) throws Exception {
if (future.isSuccess()) {
OslpChannelHandlerClient.this.write(future, address, request);
// What is this call below good for?
future.getChannel().getId();
} else {
LOGGER.info("The connection to the device {} is not successful", deviceIdentification);
throw new IOException("ChannelFuture - Unable to connect");
}
}
});
}
use of org.jboss.netty.channel.ChannelFuture in project load-balancer by RestComm.
the class TestHA method writeResponse.
private void writeResponse(MessageEvent e, HttpResponseStatus status, String command) {
JsonObject jo = new JsonObject();
jo.addProperty(command, Protocol.OK);
ChannelBuffer buf = ChannelBuffers.copiedBuffer(jo.toString(), Charset.forName("UTF-8"));
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status);
response.setHeader(HttpHeaders.Names.CONTENT_TYPE, APPLICATION_JSON);
response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, buf.readableBytes());
response.setContent(buf);
ChannelFuture future = e.getChannel().write(response);
future.addListener(ChannelFutureListener.CLOSE);
}
use of org.jboss.netty.channel.ChannelFuture in project load-balancer by RestComm.
the class TestNodeRegister method writeResponse.
private void writeResponse(MessageEvent e, HttpResponseStatus status, String command) {
Packet packet = null;
switch(command) {
case Protocol.HEARTBEAT:
packet = new HeartbeatResponsePacket(Protocol.OK);
break;
case Protocol.START:
packet = new StartResponsePacket(Protocol.OK);
break;
case Protocol.SHUTDOWN:
packet = new ShutdownResponsePacket(Protocol.OK);
break;
}
ChannelBuffer buf = ChannelBuffers.copiedBuffer(gson.toJson(packet), Charset.forName("UTF-8"));
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status);
response.setHeader(HttpHeaders.Names.CONTENT_TYPE, APPLICATION_JSON);
response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, buf.readableBytes());
response.setContent(buf);
ChannelFuture future = e.getChannel().write(response);
future.addListener(ChannelFutureListener.CLOSE);
}
Aggregations