use of com.datatorrent.netlet.AbstractLengthPrependerClient in project apex-core by apache.
the class Server method handleResetRequest.
private void handleResetRequest(ResetRequestTuple request, final AbstractLengthPrependerClient ctx) throws IOException {
DataList dl;
dl = publisherBuffers.remove(request.getIdentifier());
byte[] message;
if (dl == null) {
message = ("Invalid identifier '" + request.getIdentifier() + "'").getBytes();
} else {
AbstractLengthPrependerClient channel = publisherChannels.remove(request.getIdentifier());
if (channel != null) {
eventloop.disconnect(channel);
}
dl.reset();
message = ("Request sent for processing: " + request).getBytes();
}
final byte[] tuple = PayloadTuple.getSerializedTuple(0, message.length);
System.arraycopy(message, 0, tuple, tuple.length - message.length, message.length);
if (ctx.write(tuple)) {
ctx.write();
} else {
logger.error("Failed to deliver reset ack message. {} send buffers are full.", ctx);
throw new RuntimeException("Failed to deliver reset ack message. " + ctx + "send buffers are full.");
}
}
use of com.datatorrent.netlet.AbstractLengthPrependerClient in project apex-malhar by apache.
the class FlumeSinkTest method testServer.
@Test
@SuppressWarnings("SleepWhileInLoop")
public void testServer() throws InterruptedException, IOException {
final CountDownLatch countdown = new CountDownLatch(1);
Discovery<byte[]> discovery = new Discovery<byte[]>() {
@Override
public void unadvertise(Service<byte[]> service) {
logger.info("Unadvertise invoked");
countdown.countDown();
}
@Override
public void advertise(Service<byte[]> service) {
logger.info("Advertise invoked");
port = service.getPort();
logger.debug("listening at {}", service);
countdown.countDown();
}
@Override
@SuppressWarnings("unchecked")
public synchronized Collection<Service<byte[]>> discover() {
logger.info("Discover invoked");
try {
countdown.await(30, TimeUnit.SECONDS);
logger.info("Discover wait completed");
} catch (InterruptedException ie) {
throw new RuntimeException(ie);
}
return Collections.EMPTY_LIST;
}
};
DefaultEventLoop eventloop = new DefaultEventLoop("Eventloop-TestClient");
eventloop.start();
FlumeSink sink = new FlumeSink();
sink.setName("TeskSink");
sink.setHostname(hostname);
sink.setPort(0);
sink.setAcceptedTolerance(2000);
sink.setChannel(new MemoryChannel());
sink.setDiscovery(discovery);
sink.start();
logger.info("Sink started");
AbstractLengthPrependerClient client = new AbstractLengthPrependerClient() {
private byte[] array;
private int offset = 2;
@Override
public void onMessage(byte[] buffer, int offset, int size) {
Slice received = new Slice(buffer, offset, size);
logger.debug("Client Received = {}", received);
Assert.assertEquals(received, new Slice(Arrays.copyOfRange(array, this.offset, array.length), 0, Server.Request.FIXED_SIZE));
synchronized (FlumeSinkTest.this) {
FlumeSinkTest.this.notify();
}
}
@Override
public void connected() {
super.connected();
array = new byte[Server.Request.FIXED_SIZE + offset];
array[offset] = Server.Command.ECHO.getOrdinal();
array[offset + 1] = 1;
array[offset + 2] = 2;
array[offset + 3] = 3;
array[offset + 4] = 4;
array[offset + 5] = 5;
array[offset + 6] = 6;
array[offset + 7] = 7;
array[offset + 8] = 8;
Server.writeLong(array, offset + Server.Request.TIME_OFFSET, System.currentTimeMillis());
write(array, offset, Server.Request.FIXED_SIZE);
logger.info("Connect complete");
}
};
discovery.discover();
try {
eventloop.connect(new InetSocketAddress(hostname, port), client);
try {
synchronized (this) {
this.wait();
}
} finally {
eventloop.disconnect(client);
}
} finally {
eventloop.stop();
}
sink.stop();
}
use of com.datatorrent.netlet.AbstractLengthPrependerClient in project apex-core by apache.
the class Server method handlePublisherRequest.
/**
*
* @param request
* @param connection
* @return
*/
public DataList handlePublisherRequest(PublishRequestTuple request, AbstractLengthPrependerClient connection) {
String identifier = request.getIdentifier();
DataList dl = publisherBuffers.get(identifier);
if (dl != null) {
/*
* close previous connection with the same identifier which is guaranteed to be unique.
*/
AbstractLengthPrependerClient previous = publisherChannels.put(identifier, connection);
if (previous != null) {
eventloop.disconnect(previous);
}
try {
dl.rewind(request.getBaseSeconds(), request.getWindowId());
} catch (IOException ie) {
throw new RuntimeException(ie);
}
} else {
dl = Tuple.FAST_VERSION.equals(request.getVersion()) ? new FastDataList(identifier, blockSize, numberOfCacheBlocks, BACK_PRESSURE_ENABLED) : new DataList(identifier, blockSize, numberOfCacheBlocks, BACK_PRESSURE_ENABLED);
DataList odl = publisherBuffers.putIfAbsent(identifier, dl);
if (odl != null) {
dl = odl;
}
}
dl.setSecondaryStorage(storage, storageHelperExecutor);
return dl;
}
Aggregations