Search in sources :

Example 1 with MACAddress

use of com.neuronrobotics.sdk.common.MACAddress 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 MACAddress

use of com.neuronrobotics.sdk.common.MACAddress 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)

Example 3 with MACAddress

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

the class PidDeviceServer method main.

public static void main(String[] args) {
    Log.enableInfoPrint();
    PidDeviceServer srv = new PidDeviceServer(new MACAddress(), new VirtualGenericPIDDevice(10000));
}
Also used : MACAddress(com.neuronrobotics.sdk.common.MACAddress) VirtualGenericPIDDevice(com.neuronrobotics.sdk.pid.VirtualGenericPIDDevice)

Example 4 with MACAddress

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

the class BowlerDatagramFactoryTests method test.

@Test
public void test() {
    // Must be a factor of default pool size
    int testBifferSize = 10 * BowlerDatagramFactory.getDefaultPoolSize();
    ArrayList<BowlerDatagram> myList = new ArrayList<BowlerDatagram>();
    // verify initial state
    if (BowlerDatagramFactory.getCurrentPoolSize() != BowlerDatagramFactory.getDefaultPoolSize()) {
        fail();
    }
    for (int i = 0; i < testBifferSize; i++) {
        myList.add(BowlerDatagramFactory.build(new MACAddress(), new PingCommand()));
    }
    for (BowlerDatagram b : myList) {
        if (b.isFree()) {
            // if any packets not marked as allocated
            fail();
        }
    // System.out.println(b);
    }
    // wait for packets to timeout
    ThreadUtil.wait((int) ((double) BowlerDatagramFactory.getPacketTimeout()) / 2);
    for (BowlerDatagram b : myList) {
        if (b.isFree())
            // if any packets not marked as free too soon
            fail();
    // System.out.println(b);
    }
    // wait for packets to timeout
    ThreadUtil.wait((int) ((double) BowlerDatagramFactory.getPacketTimeout()));
    for (BowlerDatagram b : myList) {
        if (!b.isFree())
            // any packets that failed to timeout
            fail();
    }
    // refill the array
    myList.clear();
    for (int i = 0; i < testBifferSize; i++) {
        myList.add(BowlerDatagramFactory.build(new MACAddress(), new PingCommand()));
    }
    for (BowlerDatagram b : myList) {
        if (b.isFree())
            // if any packets not marked as before freeing
            fail();
        BowlerDatagramFactory.freePacket(b);
        if (!b.isFree())
            // if any packets not marked as free after freeing it
            fail();
    }
}
Also used : MACAddress(com.neuronrobotics.sdk.common.MACAddress) BowlerDatagram(com.neuronrobotics.sdk.common.BowlerDatagram) ArrayList(java.util.ArrayList) PingCommand(com.neuronrobotics.sdk.commands.bcs.core.PingCommand) Test(org.junit.Test)

Example 5 with MACAddress

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

the class PacketValidationTest method packetTest.

@Test
public void packetTest() {
    Log.enableInfoPrint();
    BowlerDatagram bd = BowlerDatagramFactory.build(new MACAddress(), new PingCommand());
    System.out.println(bd);
    ByteList data = new ByteList(bd.getBytes());
    System.out.println(data);
    BowlerDatagram back = BowlerDatagramFactory.build(data);
    if (back == null)
        fail();
    System.out.println(back);
}
Also used : MACAddress(com.neuronrobotics.sdk.common.MACAddress) ByteList(com.neuronrobotics.sdk.common.ByteList) BowlerDatagram(com.neuronrobotics.sdk.common.BowlerDatagram) PingCommand(com.neuronrobotics.sdk.commands.bcs.core.PingCommand) Test(org.junit.Test)

Aggregations

MACAddress (com.neuronrobotics.sdk.common.MACAddress)6 BowlerDatagram (com.neuronrobotics.sdk.common.BowlerDatagram)5 PingCommand (com.neuronrobotics.sdk.commands.bcs.core.PingCommand)3 RpcEncapsulation (com.neuronrobotics.sdk.common.RpcEncapsulation)2 IOException (java.io.IOException)2 Test (org.junit.Test)2 BowlerAbstractCommand (com.neuronrobotics.sdk.common.BowlerAbstractCommand)1 BowlerAbstractDevice (com.neuronrobotics.sdk.common.BowlerAbstractDevice)1 BowlerDataType (com.neuronrobotics.sdk.common.BowlerDataType)1 ByteList (com.neuronrobotics.sdk.common.ByteList)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 VirtualGenericPIDDevice (com.neuronrobotics.sdk.pid.VirtualGenericPIDDevice)1 InetAddress (java.net.InetAddress)1 ArrayList (java.util.ArrayList)1