Search in sources :

Example 16 with PackageInfo

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

the class BRunUtil method invokePackageInit.

/**
 * Invoke package init function.
 *
 * @param compileResult CompileResult instance
 * @param packageName   Name of the package to invoke
 */
protected static void invokePackageInit(CompileResult compileResult, String packageName) {
    if (compileResult.getErrorCount() > 0) {
        throw new IllegalStateException(compileResult.toString());
    }
    ProgramFile programFile = compileResult.getProgFile();
    PackageInfo packageInfo = programFile.getPackageInfo(packageName);
    WorkerExecutionContext context = new WorkerExecutionContext(programFile);
    Debugger debugger = new Debugger(programFile);
    programFile.setDebugger(debugger);
    compileResult.setContext(context);
    BLangFunctions.invokePackageInitFunction(packageInfo.getInitFunctionInfo(), context);
}
Also used : WorkerExecutionContext(org.ballerinalang.bre.bvm.WorkerExecutionContext) Debugger(org.ballerinalang.util.debugger.Debugger) PackageInfo(org.ballerinalang.util.codegen.PackageInfo) ProgramFile(org.ballerinalang.util.codegen.ProgramFile)

Example 17 with PackageInfo

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

the class TransactionUtils method invokeCoordinatorFunction.

private static BValue[] invokeCoordinatorFunction(WorkerExecutionContext ctx, String functionName, BValue[] args) {
    PackageInfo packageInfo = ctx.programFile.getPackageInfo(TransactionConstants.COORDINATOR_PACKAGE);
    FunctionInfo functionInfo = packageInfo.getFunctionInfo(functionName);
    return BLangFunctions.invokeCallable(functionInfo, args);
}
Also used : PackageInfo(org.ballerinalang.util.codegen.PackageInfo) FunctionInfo(org.ballerinalang.util.codegen.FunctionInfo)

Example 18 with PackageInfo

use of org.ballerinalang.util.codegen.PackageInfo 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 19 with PackageInfo

use of org.ballerinalang.util.codegen.PackageInfo 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 20 with PackageInfo

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

Aggregations

PackageInfo (org.ballerinalang.util.codegen.PackageInfo)48 StructInfo (org.ballerinalang.util.codegen.StructInfo)35 BStruct (org.ballerinalang.model.values.BStruct)17 ProgramFile (org.ballerinalang.util.codegen.ProgramFile)10 BValue (org.ballerinalang.model.values.BValue)6 BStructType (org.ballerinalang.model.types.BStructType)5 FunctionInfo (org.ballerinalang.util.codegen.FunctionInfo)5 Debugger (org.ballerinalang.util.debugger.Debugger)5 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)5 BInteger (org.ballerinalang.model.values.BInteger)2 BMap (org.ballerinalang.model.values.BMap)2 BString (org.ballerinalang.model.values.BString)2 SocketIOChannel (org.ballerinalang.nativeimpl.io.channels.SocketIOChannel)2 Channel (org.ballerinalang.nativeimpl.io.channels.base.Channel)2 TesterinaFunction (org.ballerinalang.testerina.core.entity.TesterinaFunction)2 Instruction (org.ballerinalang.util.codegen.Instruction)2 PackageVarInfo (org.ballerinalang.util.codegen.PackageVarInfo)2 BLangRuntimeException (org.ballerinalang.util.exceptions.BLangRuntimeException)2 Test (org.testng.annotations.Test)2 PrintStream (java.io.PrintStream)1