use of io.vertx.core.net.NetSocket in project vert.x by eclipse.
the class NetSocketImpl method sendFile.
@Override
public NetSocket sendFile(String filename, long offset, long length, final Handler<AsyncResult<Void>> resultHandler) {
File f = vertx.resolveFile(filename);
if (f.isDirectory()) {
throw new IllegalArgumentException("filename must point to a file and not to a directory");
}
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile(f, "r");
ChannelFuture future = super.sendFile(raf, Math.min(offset, f.length()), Math.min(length, f.length() - offset));
if (resultHandler != null) {
future.addListener(fut -> {
final AsyncResult<Void> res;
if (future.isSuccess()) {
res = Future.succeededFuture();
} else {
res = Future.failedFuture(future.cause());
}
vertx.runOnContext(v -> resultHandler.handle(res));
});
}
} catch (IOException e) {
try {
if (raf != null) {
raf.close();
}
} catch (IOException ignore) {
}
if (resultHandler != null) {
vertx.runOnContext(v -> resultHandler.handle(Future.failedFuture(e)));
} else {
log.error("Failed to send file", e);
}
}
return this;
}
use of io.vertx.core.net.NetSocket in project vert.x by eclipse.
the class VertxHttp2NetSocket method sendFile.
@Override
public NetSocket sendFile(String filename, long offset, long length, Handler<AsyncResult<Void>> resultHandler) {
synchronized (conn) {
Context resultCtx = resultHandler != null ? vertx.getOrCreateContext() : null;
File file = vertx.resolveFile(filename);
if (!file.exists()) {
if (resultHandler != null) {
resultCtx.runOnContext((v) -> resultHandler.handle(Future.failedFuture(new FileNotFoundException())));
} else {
// log.error("File not found: " + filename);
}
return this;
}
RandomAccessFile raf;
try {
raf = new RandomAccessFile(file, "r");
} catch (IOException e) {
if (resultHandler != null) {
resultCtx.runOnContext((v) -> resultHandler.handle(Future.failedFuture(e)));
} else {
//log.error("Failed to send file", e);
}
return this;
}
long contentLength = Math.min(length, file.length() - offset);
FileStreamChannel fileChannel = new FileStreamChannel(ar -> {
if (resultHandler != null) {
resultCtx.runOnContext(v -> {
resultHandler.handle(Future.succeededFuture());
});
}
}, this, offset, contentLength);
drainHandler(fileChannel.drainHandler);
handlerContext.channel().eventLoop().register(fileChannel);
fileChannel.pipeline().fireUserEventTriggered(raf);
}
return this;
}
use of io.vertx.core.net.NetSocket in project java-chassis by ServiceComb.
the class TestHighwayServerConnection method testRequestNormal.
@Test
public void testRequestNormal() throws Exception {
header.setMsgType(MsgType.REQUEST);
Buffer headerBuffer = createBuffer(requestHeaderSchema, header);
Buffer bodyBuffer = Buffer.buffer();
Holder<Boolean> holder = new Holder<>();
new MockUp<HighwayServerInvoke>() {
@Mock
public boolean init(NetSocket netSocket, long msgId, RequestHeader header, Buffer bodyBuffer) {
return true;
}
@Mock
public void execute() {
holder.value = true;
}
};
connection.handle(0, headerBuffer, bodyBuffer);
Assert.assertEquals(null, connection.getProtocol());
Assert.assertEquals(null, connection.getZipName());
Assert.assertEquals(true, holder.value);
}
use of io.vertx.core.net.NetSocket in project vertx-tcp-eventbus-bridge by vert-x3.
the class TcpEventBusBridgeTest method testNoHandlers.
@Test
public void testNoHandlers(TestContext context) {
// Send a request and get a response
NetClient client = vertx.createNetClient();
final Async async = context.async();
client.connect(7000, "localhost", conn -> {
context.assertFalse(conn.failed());
NetSocket socket = conn.result();
final FrameParser parser = new FrameParser(parse -> {
context.assertTrue(parse.succeeded());
JsonObject frame = parse.result();
context.assertEquals("err", frame.getString("type"));
context.assertEquals("#backtrack", frame.getString("address"));
client.close();
async.complete();
});
socket.handler(parser);
FrameHelper.sendFrame("send", "test", "#backtrack", new JsonObject().put("value", "vert.x"), socket);
});
}
use of io.vertx.core.net.NetSocket in project vertx-tcp-eventbus-bridge by vert-x3.
the class TcpEventBusBridgeTest method testSendMessageWithDuplicateReplyID.
@Test
public void testSendMessageWithDuplicateReplyID(TestContext context) {
// replies must always return to the same origin
NetClient client = vertx.createNetClient();
final Async async = context.async();
client.connect(7000, "localhost", conn -> {
context.assertFalse(conn.failed());
NetSocket socket = conn.result();
vertx.eventBus().consumer("third-party-receiver", msg -> context.fail());
final FrameParser parser = new FrameParser(parse -> {
context.assertTrue(parse.succeeded());
client.close();
async.complete();
});
socket.handler(parser);
FrameHelper.sendFrame("send", "hello", "third-party-receiver", new JsonObject().put("value", "vert.x"), socket);
});
}
Aggregations