Search in sources :

Example 1 with SocketIOChannel

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));
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) BStruct(org.ballerinalang.model.values.BStruct) StructInfo(org.ballerinalang.util.codegen.StructInfo) SocketIOChannel(org.ballerinalang.nativeimpl.io.channels.SocketIOChannel) InetSocketAddress(java.net.InetSocketAddress) PackageInfo(org.ballerinalang.util.codegen.PackageInfo) SocketIOChannel(org.ballerinalang.nativeimpl.io.channels.SocketIOChannel) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) SocketChannel(java.nio.channels.SocketChannel) Socket(java.net.Socket)

Example 2 with SocketIOChannel

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;
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) StructInfo(org.ballerinalang.util.codegen.StructInfo) SocketIOChannel(org.ballerinalang.nativeimpl.io.channels.SocketIOChannel) PackageInfo(org.ballerinalang.util.codegen.PackageInfo) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) ByteChannel(java.nio.channels.ByteChannel) SocketIOChannel(org.ballerinalang.nativeimpl.io.channels.SocketIOChannel)

Aggregations

BStruct (org.ballerinalang.model.values.BStruct)2 SocketIOChannel (org.ballerinalang.nativeimpl.io.channels.SocketIOChannel)2 Channel (org.ballerinalang.nativeimpl.io.channels.base.Channel)2 PackageInfo (org.ballerinalang.util.codegen.PackageInfo)2 StructInfo (org.ballerinalang.util.codegen.StructInfo)2 InetSocketAddress (java.net.InetSocketAddress)1 Socket (java.net.Socket)1 ByteChannel (java.nio.channels.ByteChannel)1 SocketChannel (java.nio.channels.SocketChannel)1