use of org.ballerinalang.nativeimpl.io.channels.SocketIOChannel in project ballerina by ballerina-lang.
the class OpenSocket method execute.
@Override
public void execute(Context context) {
final String host = context.getStringArgument(0);
final int port = (int) context.getIntArgument(0);
final BStruct options = (BStruct) context.getRefArgument(0);
if (log.isDebugEnabled()) {
log.debug("Remote host: " + host);
log.debug("Remote port: " + port);
}
Socket socket;
SocketChannel channel;
try {
// Open a client connection
SocketChannel socketChannel = SocketChannel.open();
if (options.getIntField(LOCAL_PORT_OPTION_FIELD_INDEX) > 0) {
if (log.isDebugEnabled()) {
log.debug("Bind client socket to local port: " + options.getIntField(0));
}
socketChannel.bind(new InetSocketAddress((int) options.getIntField(0)));
}
socketChannel.connect(new InetSocketAddress(host, port));
log.debug("Successfully connect to remote server.");
socket = socketChannel.socket();
if (log.isDebugEnabled()) {
log.debug("Bound local port: " + socket.getLocalPort());
log.debug("Timeout on blocking Socket operations: " + socket.getSoTimeout());
log.debug("ReceiveBufferSize: " + socket.getReceiveBufferSize());
log.debug("KeepAlive: " + socket.getKeepAlive());
}
channel = socket.getChannel();
PackageInfo ioPackageInfo = context.getProgramFile().getPackageInfo(SOCKET_PACKAGE);
// Create ByteChannel Struct
StructInfo channelStructInfo = ioPackageInfo.getStructInfo(BYTE_CHANNEL_STRUCT_TYPE);
Channel ballerinaSocketChannel = new SocketIOChannel(channel, 0);
BStruct channelStruct = BLangVMStructs.createBStruct(channelStructInfo, ballerinaSocketChannel);
channelStruct.addNativeData(IOConstants.BYTE_CHANNEL_NAME, ballerinaSocketChannel);
// Create Socket Struct
StructInfo socketStructInfo = ioPackageInfo.getStructInfo(SOCKET_STRUCT_TYPE);
BStruct socketStruct = BLangVMStructs.createBStruct(socketStructInfo);
socketStruct.setRefField(0, channelStruct);
socketStruct.setIntField(0, socket.getPort());
socketStruct.setIntField(1, socket.getLocalPort());
socketStruct.setStringField(0, socket.getInetAddress().getHostAddress());
socketStruct.setStringField(1, socket.getLocalAddress().getHostAddress());
socketStruct.addNativeData(IOConstants.CLIENT_SOCKET_NAME, channel);
context.setReturnValues(socketStruct);
} catch (Throwable e) {
String msg = "Failed to open a connection to [" + host + ":" + port + "] : " + e.getMessage();
log.error(msg, e);
context.setReturnValues(IOUtils.createError(context, msg));
}
}
use of org.ballerinalang.nativeimpl.io.channels.SocketIOChannel in project ballerina by ballerina-lang.
the class OpenSecureSocket method createReturnStruct.
private BStruct createReturnStruct(Context context, SSLSocket sslSocket, ByteChannel channel) throws IOException {
PackageInfo ioPackageInfo = context.getProgramFile().getPackageInfo(SOCKET_PACKAGE);
// Create ByteChannel Struct
StructInfo channelStructInfo = ioPackageInfo.getStructInfo(BYTE_CHANNEL_STRUCT_TYPE);
Channel ballerinaSocketChannel = new SocketIOChannel(channel, 0);
BStruct channelStruct = BLangVMStructs.createBStruct(channelStructInfo, ballerinaSocketChannel);
channelStruct.addNativeData(IOConstants.BYTE_CHANNEL_NAME, ballerinaSocketChannel);
// Create Socket Struct
StructInfo socketStructInfo = ioPackageInfo.getStructInfo(SOCKET_STRUCT_TYPE);
BStruct socketStruct = BLangVMStructs.createBStruct(socketStructInfo);
socketStruct.setRefField(0, channelStruct);
socketStruct.setIntField(0, sslSocket.getPort());
socketStruct.setIntField(1, sslSocket.getLocalPort());
socketStruct.setStringField(0, sslSocket.getInetAddress().getHostAddress());
socketStruct.setStringField(1, sslSocket.getLocalAddress().getHostAddress());
socketStruct.addNativeData(IOConstants.CLIENT_SOCKET_NAME, channel);
return socketStruct;
}
Aggregations