Search in sources :

Example 1 with StructInfo

use of org.ballerinalang.util.codegen.StructInfo 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 StructInfo

use of org.ballerinalang.util.codegen.StructInfo in project ballerina by ballerina-lang.

the class MessageUtils method getConnectorError.

public static BStruct getConnectorError(Context context, Throwable throwable) {
    PackageInfo grpcPackageInfo = context.getProgramFile().getPackageInfo(MessageConstants.PROTOCOL_STRUCT_PACKAGE_GRPC);
    StructInfo errorStructInfo = grpcPackageInfo.getStructInfo(MessageConstants.CONNECTOR_ERROR);
    return getConnectorError(errorStructInfo.getType(), throwable);
}
Also used : StructInfo(org.ballerinalang.util.codegen.StructInfo) PackageInfo(org.ballerinalang.util.codegen.PackageInfo)

Example 3 with StructInfo

use of org.ballerinalang.util.codegen.StructInfo in project ballerina by ballerina-lang.

the class GetLocale method createLocale.

private BStruct createLocale(Context context) {
    String language = System.getProperty("user.language");
    if (language == null) {
        language = BTypes.typeString.getZeroValue().stringValue();
    }
    String country = System.getProperty("user.country");
    if (country == null) {
        country = BTypes.typeString.getZeroValue().stringValue();
    }
    PackageInfo utilsPackageInfo = context.getProgramFile().getPackageInfo("ballerina.util");
    StructInfo localeStructInfo = utilsPackageInfo.getStructInfo("Locale");
    return BLangVMStructs.createBStruct(localeStructInfo, language, country);
}
Also used : StructInfo(org.ballerinalang.util.codegen.StructInfo) PackageInfo(org.ballerinalang.util.codegen.PackageInfo)

Example 4 with StructInfo

use of org.ballerinalang.util.codegen.StructInfo in project ballerina by ballerina-lang.

the class SecureClientSocketTest method testOpenSecureClientSocket.

@Test(description = "Test connection open with properties")
public void testOpenSecureClientSocket() throws URISyntaxException {
    PackageInfo ioPackageInfo = socketClient.getProgFile().getPackageInfo("ballerina.io");
    StructInfo socketProperties = ioPackageInfo.getStructInfo("SocketProperties");
    BStruct propertyStruct = BLangVMStructs.createBStruct(socketProperties);
    URL resource = getClass().getClassLoader().getResource("datafiles/security/keyStore/ballerinaTruststore.p12");
    Assert.assertNotNull(resource, "Unable to find TrustStore.");
    propertyStruct.setStringField(2, Paths.get(resource.toURI()).toFile().getAbsolutePath());
    propertyStruct.setStringField(3, "ballerina");
    BValue[] args = { new BString("localhost"), new BInteger(port), propertyStruct };
    BRunUtil.invoke(socketClient, "openSocketConnection", args);
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) StructInfo(org.ballerinalang.util.codegen.StructInfo) PackageInfo(org.ballerinalang.util.codegen.PackageInfo) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BInteger(org.ballerinalang.model.values.BInteger) URL(java.net.URL) Test(org.testng.annotations.Test)

Example 5 with StructInfo

use of org.ballerinalang.util.codegen.StructInfo in project ballerina by ballerina-lang.

the class IOUtils method createError.

/**
 * Returns the error struct for the corresponding message.
 *
 * @param context context of the native function.
 * @param message error message.
 * @return error message struct.
 */
public static BStruct createError(Context context, String message) {
    PackageInfo ioPkg = context.getProgramFile().getPackageInfo(IO_PACKAGE);
    StructInfo error = ioPkg.getStructInfo(IO_ERROR_STRUCT);
    return BLangVMStructs.createBStruct(error, message);
}
Also used : StructInfo(org.ballerinalang.util.codegen.StructInfo) PackageInfo(org.ballerinalang.util.codegen.PackageInfo)

Aggregations

StructInfo (org.ballerinalang.util.codegen.StructInfo)40 PackageInfo (org.ballerinalang.util.codegen.PackageInfo)35 BStruct (org.ballerinalang.model.values.BStruct)21 BStructType (org.ballerinalang.model.types.BStructType)6 BString (org.ballerinalang.model.values.BString)4 BValue (org.ballerinalang.model.values.BValue)4 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)4 ProgramFile (org.ballerinalang.util.codegen.ProgramFile)3 BType (org.ballerinalang.model.types.BType)2 BInteger (org.ballerinalang.model.values.BInteger)2 BMap (org.ballerinalang.model.values.BMap)2 SocketIOChannel (org.ballerinalang.nativeimpl.io.channels.SocketIOChannel)2 Channel (org.ballerinalang.nativeimpl.io.channels.base.Channel)2 StructFieldInfo (org.ballerinalang.util.codegen.StructFieldInfo)2 Test (org.testng.annotations.Test)2 InetSocketAddress (java.net.InetSocketAddress)1 Socket (java.net.Socket)1 URL (java.net.URL)1 ByteChannel (java.nio.channels.ByteChannel)1 SocketChannel (java.nio.channels.SocketChannel)1