use of java.nio.channels.ServerSocketChannel in project xtext-core by eclipse.
the class SocketServerLauncher method main.
public static void main(final String[] args) {
try {
ServerModule _serverModule = new ServerModule();
final Injector injector = Guice.createInjector(_serverModule);
final LanguageServer languageServer = injector.<LanguageServer>getInstance(LanguageServer.class);
final ServerSocketChannel serverSocket = ServerSocketChannel.open();
InetSocketAddress _inetSocketAddress = new InetSocketAddress("localhost", 5007);
serverSocket.bind(_inetSocketAddress);
final SocketChannel socketChannel = serverSocket.accept();
InputStream _newInputStream = Channels.newInputStream(socketChannel);
OutputStream _newOutputStream = Channels.newOutputStream(socketChannel);
PrintWriter _printWriter = new PrintWriter(System.out);
final Launcher<LanguageClient> launcher = LSPLauncher.createServerLauncher(languageServer, _newInputStream, _newOutputStream, true, _printWriter);
launcher.startListening().get();
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
use of java.nio.channels.ServerSocketChannel in project ignite by apache.
the class GridNioServer method createSelector.
/**
* Creates selector and binds server socket to a given address and port. If address is null
* then will not bind any address and just creates a selector.
*
* @param addr Local address to listen on.
* @return Created selector.
* @throws IgniteCheckedException If selector could not be created or port is already in use.
*/
private Selector createSelector(@Nullable SocketAddress addr) throws IgniteCheckedException {
Selector selector = null;
ServerSocketChannel srvrCh = null;
try {
// Create a new selector
selector = SelectorProvider.provider().openSelector();
if (addr != null) {
// Create a new non-blocking server socket channel
srvrCh = ServerSocketChannel.open();
srvrCh.configureBlocking(false);
if (sockRcvBuf > 0)
srvrCh.socket().setReceiveBufferSize(sockRcvBuf);
// Bind the server socket to the specified address and port
srvrCh.socket().bind(addr);
// Register the server socket channel, indicating an interest in
// accepting new connections
srvrCh.register(selector, SelectionKey.OP_ACCEPT);
}
return selector;
} catch (Throwable e) {
U.close(srvrCh, log);
U.close(selector, log);
if (e instanceof Error)
throw (Error) e;
throw new IgniteCheckedException("Failed to initialize NIO selector.", e);
}
}
use of java.nio.channels.ServerSocketChannel in project ignite by apache.
the class FileDownloader method start.
/**
*/
public InetSocketAddress start() throws IgniteCheckedException {
try {
ServerSocketChannel ch = ServerSocketChannel.open();
ch.bind(null);
serverChannel = ch;
return (InetSocketAddress) ch.getLocalAddress();
} catch (Exception ex) {
throw new IgniteCheckedException(ex);
}
}
use of java.nio.channels.ServerSocketChannel in project ignite by apache.
the class FileDownloader method download.
/**
*/
public void download(GridFutureAdapter<?> fut) {
this.finishFut = fut;
final ServerSocketChannel ch = serverChannel;
fut.listen(new IgniteInClosureX<IgniteInternalFuture<?>>() {
@Override
public void applyx(IgniteInternalFuture<?> future) throws IgniteCheckedException {
try {
if (log != null && log.isInfoEnabled())
log.info("Server socket closed " + ch.getLocalAddress());
ch.close();
} catch (Exception ex) {
U.error(log, "Fail close socket.", ex);
throw new IgniteCheckedException(ex);
}
}
});
FileChannel writeChannel = null;
SocketChannel readChannel = null;
try {
File f = new File(path.toUri().getPath());
if (f.exists())
f.delete();
File cacheWorkDir = f.getParentFile();
if (!cacheWorkDir.exists())
cacheWorkDir.mkdir();
writeChannel = FileChannel.open(path, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
initFut.onDone();
readChannel = serverChannel.accept();
long pos = 0;
long size = this.size.get();
while (size == -1 || pos < size) {
pos += writeChannel.transferFrom(readChannel, pos, CHUNK_SIZE);
if (size == -1)
size = this.size.get();
}
} catch (IOException ex) {
initFut.onDone(ex);
fut.onDone(ex);
} finally {
try {
if (writeChannel != null)
writeChannel.close();
} catch (IOException ex) {
throw new IgniteException("Could not close file: " + path);
}
try {
if (readChannel != null)
readChannel.close();
} catch (IOException ex) {
throw new IgniteException("Could not close socket");
}
}
}
use of java.nio.channels.ServerSocketChannel in project tomee by apache.
the class MultipointServer method doAccept.
private void doAccept(final SelectionKey key) throws IOException {
// we are a server
// when you are a server, we must first listen for the
// address of the client before sending data.
// once they send us their address, we will send our
// full list of known addresses, followed by the "end"
// address to signal that we are done.
// Afterward we will only pulls our heartbeat
final ServerSocketChannel server = (ServerSocketChannel) key.channel();
final SocketChannel client = server.accept();
final InetSocketAddress address = (InetSocketAddress) client.socket().getRemoteSocketAddress();
client.configureBlocking(false);
final Session session = new Session(client, address, null);
session.trace("accept");
session.state(SelectionKey.OP_READ, State.GREETING);
}
Aggregations