use of org.jboss.netty.channel.Channel in project weave by continuuity.
the class TrackerService method startUp.
@Override
protected void startUp() throws Exception {
Executor bossThreads = Executors.newFixedThreadPool(NUM_BOSS_THREADS, new ThreadFactoryBuilder().setDaemon(true).setNameFormat("boss-thread").build());
Executor workerThreads = Executors.newCachedThreadPool(new ThreadFactoryBuilder().setDaemon(true).setNameFormat("worker-thread#%d").build());
ChannelFactory factory = new NioServerSocketChannelFactory(bossThreads, workerThreads);
bootstrap = new ServerBootstrap(factory);
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() {
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("decoder", new HttpRequestDecoder());
pipeline.addLast("aggregator", new HttpChunkAggregator(MAX_INPUT_SIZE));
pipeline.addLast("encoder", new HttpResponseEncoder());
pipeline.addLast("compressor", new HttpContentCompressor());
pipeline.addLast("handler", new ReportHandler(resourceReport));
return pipeline;
}
});
Channel channel = bootstrap.bind(new InetSocketAddress(host, 0));
bindAddress = (InetSocketAddress) channel.getLocalAddress();
url = URI.create(String.format("http://%s:%d", host, bindAddress.getPort())).resolve(TrackerService.PATH).toURL();
channelGroup.add(channel);
}
use of org.jboss.netty.channel.Channel in project socket.io-netty by ibdknox.
the class GenericIOClient method disconnect.
public void disconnect() {
Channel chan = ctx.getChannel();
if (chan.isOpen()) {
chan.close();
}
this.open = false;
}
use of org.jboss.netty.channel.Channel in project socket.io-netty by ibdknox.
the class PollingIOClient method _write.
private void _write(String message) {
if (!this.open)
return;
HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.OK);
res.addHeader(CONTENT_TYPE, "text/plain; charset=UTF-8");
res.addHeader("Access-Control-Allow-Origin", "*");
res.addHeader("Access-Control-Allow-Credentials", "true");
res.addHeader("Connection", "keep-alive");
res.setContent(ChannelBuffers.copiedBuffer(message, CharsetUtil.UTF_8));
setContentLength(res, res.getContent().readableBytes());
// Send the response and close the connection if necessary.
Channel chan = ctx.getChannel();
if (chan.isOpen()) {
ChannelFuture f = chan.write(res);
if (!isKeepAlive(req) || res.getStatus().getCode() != 200) {
f.addListener(ChannelFutureListener.CLOSE);
}
}
this.connected = false;
}
use of org.jboss.netty.channel.Channel in project databus by linkedin.
the class HttpRelay method disconnectDBusClients.
public synchronized void disconnectDBusClients(Channel exceptThis) {
LOG.info("disconnectDBusClients");
if (_httpChannelGroup != null) {
LOG.info("Total " + _httpChannelGroup.size() + " channels");
for (Channel channel : _httpChannelGroup) {
// Keep the server channel and REST channel
if ((channel instanceof ServerChannel) || (exceptThis != null && channel.getId().equals(exceptThis.getId()))) {
LOG.info("Skipping closing channel" + channel.getId());
} else {
LOG.info("closing channel" + channel.getId());
channel.close();
}
}
}
}
use of org.jboss.netty.channel.Channel in project databus by linkedin.
the class RelayCommandRequestProcessor method process.
@Override
public DatabusRequest process(DatabusRequest request) throws IOException, RequestProcessingException {
String command = request.getParams().getProperty(DatabusRequest.PATH_PARAM_NAME);
if (null == command) {
throw new InvalidRequestParamValueException(COMMAND_NAME, "command", "null");
}
String reply = "Command " + command + " completed ";
LOG.info("got relayCommand = " + command);
if (command.equals(SAVE_META_STATE_PARAM)) {
_relay.saveBufferMetaInfo(true);
} else if (command.equals(SHUTDOWN_RELAY_PARAM)) {
String msg = "received shutdown curl request from: " + request.getRemoteAddress() + ". Shutting down\n";
LOG.warn(msg);
request.getResponseContent().write(ByteBuffer.wrap(msg.getBytes("UTF-8")));
request.getResponseContent().close();
_relay.shutdown();
} else if (command.equals(VALIDATE_RELAY_BUFFER_PARAM)) {
_relay.validateRelayBuffers();
} else if (command.equals(DISCONNECT_CLIENTS)) {
Channel rspChannel = request.getResponseContent().getRawChannel();
_relay.disconnectDBusClients(rspChannel);
} else if (command.equals(RUN_GC_PARAM)) {
Runtime rt = Runtime.getRuntime();
long mem = rt.freeMemory();
LOG.info("mem before gc = " + rt.freeMemory() + " out of " + rt.totalMemory());
long time = System.currentTimeMillis();
System.gc();
time = System.currentTimeMillis() - time;
mem = rt.freeMemory() - mem;
reply = new String("GC run. Took " + time + " millsecs. Freed " + mem + " bytes out of " + rt.totalMemory());
} else if (command.startsWith(RESET_RELAY_BUFFER_PARAM)) {
// We expect the request to be of the format:
// resetRelayBuffer/<dbName>/<partitionId>?prevScn=<long>&binlogOffset=<long>
String[] resetCommands = command.split("/");
if (resetCommands.length != 3) {
throw new InvalidRequestParamValueException(COMMAND_NAME, "command", command);
}
String dbName = resetCommands[1];
String dbPart = resetCommands[2];
long prevScn = request.getRequiredLongParam(PREV_SCN_PARAM);
long binlogOffset = request.getOptionalLongParam(BINLOG_OFFSET_PARAM, 0L);
LOG.info("reset command = " + dbName + " part =" + dbPart);
try {
_relay.resetBuffer(new PhysicalPartition(Integer.parseInt(dbPart), dbName), prevScn, binlogOffset);
} catch (BufferNotFoundException e) {
reply = new String("command " + command + ":" + e.getMessage());
}
} else if (command.startsWith(GET_BINLOG_OFFSET_PARAM)) {
String[] getOfsArgs = command.split("/");
if (getOfsArgs.length != 2) {
throw new InvalidRequestParamValueException(GET_BINLOG_OFFSET_PARAM, "Server ID", "");
}
int serverId;
try {
serverId = Integer.parseInt(getOfsArgs[1]);
int[] offset = _relay.getBinlogOffset(serverId);
if (offset.length != 2) {
reply = "Error getting binlog offset";
} else {
reply = new String("RelayLastEvent(" + offset[0] + "," + offset[1] + ")");
}
} catch (NumberFormatException e) {
throw new InvalidRequestParamValueException(GET_BINLOG_OFFSET_PARAM, "Server ID", getOfsArgs[1]);
} catch (DatabusException e) {
reply = new String("command " + command + "failed with:" + e.getMessage());
}
} else if (command.startsWith(PRINT_RELAY_INFO_PARAM)) {
try {
Map<String, String> infoMap = _relay.printInfo();
reply = makeJsonResponse(infoMap, request);
} catch (Exception e) {
reply = new String("command " + command + " failed with:" + e.getMessage());
}
} else {
// invalid command
reply = new String("command " + command + " is invalid. Valid commands are: " + SAVE_META_STATE_PARAM + "|" + SHUTDOWN_RELAY_PARAM + "|" + VALIDATE_RELAY_BUFFER_PARAM + "|" + RUN_GC_PARAM + "|" + RESET_RELAY_BUFFER_PARAM + "|" + GET_BINLOG_OFFSET_PARAM + "|" + PRINT_RELAY_INFO_PARAM + "|" + DISCONNECT_CLIENTS);
}
byte[] responseBytes = new byte[(reply.length() + 2)];
System.arraycopy(reply.getBytes("UTF-8"), 0, responseBytes, 0, reply.length());
int idx = reply.length();
responseBytes[idx] = (byte) '\r';
responseBytes[idx + 1] = (byte) '\n';
request.getResponseContent().write(ByteBuffer.wrap(responseBytes));
return request;
}
Aggregations