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));
}
}
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);
}
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);
}
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);
}
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);
}
Aggregations