Search in sources :

Example 1 with RpcEncapsulation

use of com.neuronrobotics.sdk.common.RpcEncapsulation in project BowlerStudio by CommonWealthRobotics.

the class TestServer method main.

public static void main(String[] args) throws Exception {
    class SampleBowlerServer extends BowlerAbstractServer {

        BowlerAbstractDeviceServerNamespace ns = new BowlerAbstractDeviceServerNamespace(getMacAddress(), "test.thingy.*;0.3;;") {
        };

        public SampleBowlerServer() {
            super(new MACAddress());
            ns.addRpc(new RpcEncapsulation(ns.getNamespaceIndex(), ns.getNamespace(), "test", BowlerMethod.GET, new BowlerDataType[] { BowlerDataType.I32, BowlerDataType.I32, // send 3
            BowlerDataType.I32 }, // integers
            BowlerMethod.POST, new BowlerDataType[] { BowlerDataType.I32, BowlerDataType.I32, // get 3 integers back
            BowlerDataType.I32 }, new IBowlerCommandProcessor() {

                public Object[] process(Object[] data) {
                    for (int i = 0; i < data.length; i++) {
                        System.out.println("Server Got # " + data[i]);
                    }
                    return new Object[] { 37, 42, 999999 };
                }
            }));
            addBowlerDeviceServerNamespace(ns);
            Log.info("Starting UDP");
            try {
                startNetworkServer(1865);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        // starts the UDP server
        // this also starts tcp server on port+1, in this case 1866
        }
    }
    class SampleBowlerClient extends BowlerAbstractDevice {

        public void runCommand() {
            Object[] args = send("test.thingy.*;0.3;;", BowlerMethod.GET, "test", // send some numbers
            new Object[] { 36, 83, 13 });
            for (int i = 0; i < args.length; i++) {
                System.out.println("Client Received  # " + args[i]);
            }
        }

        @Override
        public void onAsyncResponse(BowlerDatagram data) {
        }
        // no async in this demo
    }
    SampleBowlerClient client = new SampleBowlerClient();
    // client.setConnection(new UDPBowlerConnection(InetAddress.getByName("127.0.0.1"), 1865));
    // Alternately you can use the tcp connection
    client.setConnection(new BowlerTCPClient("127.0.0.1", 1866));
    DeviceManager.addConnection(client, "sampleClient");
    // runs our test command from client to server and
    client.runCommand();
// back
}
Also used : IBowlerCommandProcessor(com.neuronrobotics.sdk.common.device.server.IBowlerCommandProcessor) BowlerAbstractDeviceServerNamespace(com.neuronrobotics.sdk.common.device.server.BowlerAbstractDeviceServerNamespace) IOException(java.io.IOException) BowlerDataType(com.neuronrobotics.sdk.common.BowlerDataType) BowlerTCPClient(com.neuronrobotics.sdk.network.BowlerTCPClient) BowlerAbstractDevice(com.neuronrobotics.sdk.common.BowlerAbstractDevice) MACAddress(com.neuronrobotics.sdk.common.MACAddress) BowlerDatagram(com.neuronrobotics.sdk.common.BowlerDatagram) BowlerAbstractServer(com.neuronrobotics.sdk.common.device.server.BowlerAbstractServer) RpcEncapsulation(com.neuronrobotics.sdk.common.RpcEncapsulation)

Example 2 with RpcEncapsulation

use of com.neuronrobotics.sdk.common.RpcEncapsulation in project java-bowler by NeuronRobotics.

the class BowlerAbstractDeviceServerNamespace method process.

public BowlerDatagram process(BowlerDatagram data) {
    Object[] dataParsed = null;
    RpcEncapsulation parser = null;
    for (RpcEncapsulation enc : getRpcList()) {
        if (enc.getRpc().contains(data.getRPC()) && enc.getDownstreamMethod() == data.getMethod()) {
            parser = enc;
        }
    }
    if (parser == null)
        return null;
    dataParsed = parser.parseResponseDownstream(data);
    Object[] backData = parser.getProcessor().process(dataParsed);
    BowlerAbstractCommand back = parser.getCommandUpstream(backData);
    return BowlerDatagramFactory.build(getAddress(), back);
}
Also used : BowlerAbstractCommand(com.neuronrobotics.sdk.common.BowlerAbstractCommand) RpcEncapsulation(com.neuronrobotics.sdk.common.RpcEncapsulation)

Example 3 with RpcEncapsulation

use of com.neuronrobotics.sdk.common.RpcEncapsulation in project java-bowler by NeuronRobotics.

the class BowlerAbstractServer method pushAsyncPacket.

/**
 * THis is the scripting interface to Bowler devices. THis allows a user to
 * describe a namespace, rpc, and array or arguments to be paced into the
 * packet based on the data types of the argument. The response in likewise
 * unpacked into an array of objects.
 *
 * @param namespace
 *            The string of the desired namespace
 * @param rpcString
 *            The string of the desired RPC
 * @param arguments
 *            An array of objects corresponding to the data to be stuffed
 *            into the packet.
 * @throws DeviceConnectionException
 *             If the desired RPC's are not available then this will be
 *             thrown
 */
public void pushAsyncPacket(int namespaceIndex, String namespace, String rpcString, Object[] arguments, BowlerDataType[] asyncArguments) {
    if (arguments.length != asyncArguments.length) {
        throw new RuntimeException("Arguments must match argument types exactly, your two arrays are different lengths");
    }
    RpcEncapsulation rpcl = new RpcEncapsulation(namespaceIndex, namespace, rpcString, BowlerMethod.ASYNCHRONOUS, asyncArguments, null, null);
    BowlerAbstractCommand command = BowlerAbstractConnection.getCommand(namespace, BowlerMethod.ASYNCHRONOUS, rpcString, arguments, rpcl);
    BowlerDatagram cmd = BowlerDatagramFactory.build(new MACAddress(), command);
    Log.debug("Async>>" + cmd);
    pushAsyncPacket(cmd);
}
Also used : MACAddress(com.neuronrobotics.sdk.common.MACAddress) BowlerDatagram(com.neuronrobotics.sdk.common.BowlerDatagram) BowlerAbstractCommand(com.neuronrobotics.sdk.common.BowlerAbstractCommand) RpcEncapsulation(com.neuronrobotics.sdk.common.RpcEncapsulation)

Aggregations

RpcEncapsulation (com.neuronrobotics.sdk.common.RpcEncapsulation)3 BowlerAbstractCommand (com.neuronrobotics.sdk.common.BowlerAbstractCommand)2 BowlerDatagram (com.neuronrobotics.sdk.common.BowlerDatagram)2 MACAddress (com.neuronrobotics.sdk.common.MACAddress)2 BowlerAbstractDevice (com.neuronrobotics.sdk.common.BowlerAbstractDevice)1 BowlerDataType (com.neuronrobotics.sdk.common.BowlerDataType)1 BowlerAbstractDeviceServerNamespace (com.neuronrobotics.sdk.common.device.server.BowlerAbstractDeviceServerNamespace)1 BowlerAbstractServer (com.neuronrobotics.sdk.common.device.server.BowlerAbstractServer)1 IBowlerCommandProcessor (com.neuronrobotics.sdk.common.device.server.IBowlerCommandProcessor)1 BowlerTCPClient (com.neuronrobotics.sdk.network.BowlerTCPClient)1 IOException (java.io.IOException)1