use of org.jboss.netty.buffer.ChannelBuffer in project NabAlive by jcheype.
the class RecordController method init.
@PostConstruct
void init() {
restHandler.post(new Route("/vl/record.jsp") {
@Override
public void handle(Request request, Response response, Map<String, String> map) throws Exception {
String mac = checkNotNull(request.getParam("sn")).toLowerCase();
if (!connectionManager.containsKey(mac))
throw new HttpException(HttpResponseStatus.NOT_FOUND, "sn is not connected");
Nabaztag nabaztag = checkNotNull(nabaztagDAO.findOne("macAddress", mac));
ChannelBuffer content = request.request.getContent();
logger.debug("record orig size: {}", content.readableBytes());
ChannelBufferInputStream inputStream = new ChannelBufferInputStream(content);
TmpData sound = new TmpData();
sound.setData(ByteStreams.toByteArray(inputStream));
tmpDataDAO.save(sound, WriteConcern.SAFE);
String host = request.request.getHeader("Host");
String url = "http://" + host + "/record/" + sound.getId().toString();
logger.debug("sound url: {}", url);
final String command = "ST " + url + "\nMW\n";
Query<Nabaztag> query = nabaztagDAO.createQuery();
query.filter("subscribe.objectId", nabaztag.getId().toString());
List<Nabaztag> nabaztags = nabaztagDAO.find(query).asList();
logger.debug("sending to {} subscribers", nabaztags.size());
for (Nabaztag nab : nabaztags) {
if (connectionManager.containsKey(nab.getMacAddress())) {
final Nabaztag nabTmp = nab;
Runnable runnable = new Runnable() {
@Override
public void run() {
logger.debug("sending to {}", nabTmp.getMacAddress());
logger.debug("command {}", command);
messageService.sendMessage(nabTmp.getMacAddress(), command);
}
};
ses.schedule(runnable, 500, TimeUnit.MILLISECONDS);
}
}
response.write("ok");
}
}).get(new Route("/record/:recordId") {
@Override
public void handle(Request request, Response response, Map<String, String> map) throws Exception {
ObjectId recordId = new ObjectId(checkNotNull(map.get("recordId")));
TmpData sound = checkNotNull(tmpDataDAO.get(recordId));
response.write(sound.getData());
}
});
}
use of org.jboss.netty.buffer.ChannelBuffer in project NabAlive by jcheype.
the class ChorBuilder method build.
public ChannelBuffer build() {
ChannelBuffer res = ChannelBuffers.dynamicBuffer();
res.writeInt(channelBuffer.readableBytes());
res.writeBytes(channelBuffer);
res.writeZero(4);
return res;
}
use of org.jboss.netty.buffer.ChannelBuffer in project NabAlive by jcheype.
the class NabaliveServerHandler method messageReceived.
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
final ChannelBuffer messageCB = (ChannelBuffer) e.getMessage();
String message = messageCB.toString(CharsetUtil.UTF_8);
final Status status = getStatus(ctx);
if (status.getJid() != null)
logger.debug(status.getJid().getUser() + " <<<<<<<<<<< {}", message);
else
logger.debug("<<<<<<<<<<< {}", message);
if (message.startsWith("<?xml ")) {
onStreamOpen(ctx, e, message);
} else if (message.startsWith("<auth ")) {
onAuth(ctx, e, message);
} else if (message.startsWith("<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'")) {
onResponse(ctx, e, message);
} else if (message.startsWith("<iq ")) {
iqHandler.onMessage(ctx, e, status, message);
} else if (message.startsWith("<presence ")) {
presenceHandler.onMessage(ctx, e, status, message);
} else if (message.startsWith("<message ")) {
messageHandler.onMessage(ctx, e, status, message);
}
}
use of org.jboss.netty.buffer.ChannelBuffer in project NabAlive by jcheype.
the class BasePacket method getFullData.
public ChannelBuffer getFullData() {
final ChannelBuffer channelBuffer = ChannelBuffers.dynamicBuffer();
channelBuffer.writeByte(0x7f);
channelBuffer.writeByte(getType());
final ChannelBuffer encryptedData = getData();
int len = encryptedData.readableBytes();
byte[] lenBytes = new byte[] { (byte) (len >>> 16), (byte) (len >>> 8), (byte) len };
channelBuffer.writeBytes(lenBytes);
channelBuffer.writeBytes(encryptedData);
channelBuffer.writeByte(0xFF);
return channelBuffer;
}
use of org.jboss.netty.buffer.ChannelBuffer in project NabAlive by jcheype.
the class HttpApiServerHandler method messageReceived.
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent event) {
HttpRequest httpRequest = (HttpRequest) event.getMessage();
logger.debug("Request: {}", httpRequest);
QueryStringDecoder qs = new QueryStringDecoder(httpRequest.getUri());
final Request request = new Request(ctx, httpRequest, qs);
final Response response = new Response(ctx, httpRequest);
try {
execBeforeFilters(request, response);
execHttpMethod(request, response);
execAfterFilters(request, response);
} catch (HttpException e) {
HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, e.getStatus());
ctx.getChannel().write(httpResponse).addListener(ChannelFutureListener.CLOSE);
} catch (Exception e) {
HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR);
if (logger.isDebugEnabled()) {
ChannelBuffer channelBuffer = ChannelBuffers.dynamicBuffer();
PrintWriter printWriter = new PrintWriter(new ChannelBufferOutputStream(channelBuffer));
e.printStackTrace(printWriter);
printWriter.close();
httpResponse.setContent(channelBuffer);
}
logger.error("web server error: ", e);
ctx.getChannel().write(httpResponse).addListener(ChannelFutureListener.CLOSE);
}
}
Aggregations