java.nio.channels.AsynchronousServerSocketChannel Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jtransc-rt Show documentation
Show all versions of jtransc-rt Show documentation
JVM AOT compiler currently generating JavaScript, C++, Haxe, with initial focus on Kotlin and games.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package java.nio.channels;
import com.jtransc.annotation.JTranscMethodBody;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.SocketOption;
import java.nio.channels.spi.AsynchronousChannelProvider;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Future;
public class AsynchronousServerSocketChannel implements AsynchronousChannel, NetworkChannel {
protected AsynchronousServerSocketChannel(AsynchronousChannelProvider provider) {
}
public final AsynchronousChannelProvider provider() {
return null;
}
@JTranscMethodBody(target = "js", value = {
"this.server = null;",
"this.clients = [];",
"this.handlers = [];",
"this._opened = false;",
})
AsynchronousServerSocketChannel() {
_init();
}
native private void _init();
public static AsynchronousServerSocketChannel open(AsynchronousChannelGroup group) throws IOException {
return new AsynchronousServerSocketChannel();
}
public static AsynchronousServerSocketChannel open() throws IOException {
return open(null);
}
public final AsynchronousServerSocketChannel bind(SocketAddress local) throws IOException {
return bind(local, 0);
}
@JTranscMethodBody(target = "js", value = {
"var host = N.istr(p0), port = p1, backlog = p2;",
"var net = require('net');",
"var _this = this;",
"this.server = net.createServer(function(socket) {",
" var client = {% CONSTRUCTOR java.nio.channels.AsynchronousSocketChannel:()V %}();",
" client.client = socket;",
" _this.clients.push(client);",
" if (_this.handlers.length != 0) { _this.handlers.shift()(_this.clients.shift()); }",
"});",
"this.server.on('error', function(err) { console.error(err); });",
"this.server.listen(port, host, backlog, function() {",
//" console.log('listening!', _this.server.address());",
" _this.opened = true;",
"});",
})
native private void _bind(String host, int port, int backlog);
AsynchronousServerSocketChannel bind(SocketAddress local, int backlog) throws IOException {
InetSocketAddress address = (InetSocketAddress) local;
_bind(address.getHostName(), address.getPort(), backlog);
return this;
}
public AsynchronousServerSocketChannel setOption(SocketOption name, T value) throws IOException {
// @TODO
return this;
}
@Override
native public T getOption(SocketOption name) throws IOException;
@Override
public Set> supportedOptions() {
return new HashSet<>();
}
@JTranscMethodBody(target = "js", value = {
"var attachment = p0, handler = p1;",
"var handlers = this.handlers;",
"function fhandle(client) { handler{% IMETHOD java.nio.channels.CompletionHandler:completed %}(client, attachment); }",
"if (this.clients.length != 0) { fhandle(this.clients.shift()); } else { this.handlers.push(fhandle); }"
})
public native void accept(A attachment, CompletionHandler handler);
public native Future accept();
@JTranscMethodBody(target = "js", value = "return N.str(this.server.address().address);")
native private String _getLocalHost();
@JTranscMethodBody(target = "js", value = "return this.server.address().port;")
native private int _getLocalPort();
@Override
public SocketAddress getLocalAddress() throws IOException {
return new InetSocketAddress(_getLocalHost(), _getLocalPort());
}
@Override
@JTranscMethodBody(target = "js", value = "return this.opened;")
native public boolean isOpen();
@Override
@JTranscMethodBody(target = "js", value = {
"this.opened = false;",
"if (this.server) this.server.close();",
})
native public void close() throws IOException;
}