Search in sources :

Example 1 with BowlerTCPClient

use of com.neuronrobotics.sdk.network.BowlerTCPClient 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 BowlerTCPClient

use of com.neuronrobotics.sdk.network.BowlerTCPClient in project java-bowler by NeuronRobotics.

the class ConfigManager method loadDefaultConnection.

public static BowlerAbstractConnection loadDefaultConnection(String filename) {
    try {
        File file = new File(filename);
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(file);
        doc.getDocumentElement().normalize();
        NodeList nodeLst = doc.getElementsByTagName("connection");
        for (int s = 0; s < nodeLst.getLength(); s++) {
            Node fstNode = nodeLst.item(s);
            if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
                Element e = (Element) fstNode;
                String type = getElementValue(e, "type", "");
                String port = getElementValue(e, "port", "");
                String baud = getElementValue(e, "baud", "0");
                String host = getElementValue(e, "host", "127.0.0.1");
                if (type.equalsIgnoreCase("serial")) {
                // return new SerialConnection(port, Integer.parseInt(baud));
                }
                if (type.equalsIgnoreCase("udp")) {
                    return new UDPBowlerConnection(Integer.parseInt(port));
                }
                if (type.equalsIgnoreCase("tcp")) {
                    return new BowlerTCPClient(host, Integer.parseInt(port));
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) UDPBowlerConnection(com.neuronrobotics.sdk.network.UDPBowlerConnection) Document(org.w3c.dom.Document) File(java.io.File) BowlerTCPClient(com.neuronrobotics.sdk.network.BowlerTCPClient)

Example 3 with BowlerTCPClient

use of com.neuronrobotics.sdk.network.BowlerTCPClient in project java-bowler by NeuronRobotics.

the class GenericPIDTest method main.

/**
 * @param args
 */
public static void main(String[] args) {
    Log.enableDebugPrint();
    GenericPIDDevice pid = new GenericPIDDevice();
    // }
    try {
        try {
            pid.setConnection(new BowlerTCPClient("cortex.wpi.edu", 1965));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // pid.setConnection(new BowlerTCPClient("192.168.0.134", 1965));
        pid.GetAllPIDPosition();
        pid.GetPIDPosition(2);
        pid.disconnect();
        System.out.println("All OK!");
        System.exit(0);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        pid.disconnect();
        System.exit(1);
    }
}
Also used : GenericPIDDevice(com.neuronrobotics.sdk.pid.GenericPIDDevice) BowlerTCPClient(com.neuronrobotics.sdk.network.BowlerTCPClient)

Example 4 with BowlerTCPClient

use of com.neuronrobotics.sdk.network.BowlerTCPClient in project java-bowler by NeuronRobotics.

the class TCPConnectionPanel method getConnection.

@Override
public BowlerAbstractConnection getConnection() {
    if (clnt == null) {
        try {
            int thePort = Integer.parseInt(port.getText());
            if (thePort < 0) {
                throw new NumberFormatException();
            }
            String address = connectionCbo.getSelectedItem().toString();
            Log.info("Connecting on: " + address + ":" + thePort);
            clnt = new BowlerTCPClient(address, thePort);
            setVisible(false);
            return clnt;
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Invalid port given.", "Invalid port", JOptionPane.ERROR_MESSAGE);
        } catch (RuntimeException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "Invalid address given.", "Invalid address", JOptionPane.ERROR_MESSAGE);
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "Invalid address given.", "Invalid address", JOptionPane.ERROR_MESSAGE);
        } finally {
            setVisible(false);
        }
        return null;
    }
    return clnt;
}
Also used : BowlerTCPClient(com.neuronrobotics.sdk.network.BowlerTCPClient)

Aggregations

BowlerTCPClient (com.neuronrobotics.sdk.network.BowlerTCPClient)4 BowlerAbstractDevice (com.neuronrobotics.sdk.common.BowlerAbstractDevice)1 BowlerDataType (com.neuronrobotics.sdk.common.BowlerDataType)1 BowlerDatagram (com.neuronrobotics.sdk.common.BowlerDatagram)1 MACAddress (com.neuronrobotics.sdk.common.MACAddress)1 RpcEncapsulation (com.neuronrobotics.sdk.common.RpcEncapsulation)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 UDPBowlerConnection (com.neuronrobotics.sdk.network.UDPBowlerConnection)1 GenericPIDDevice (com.neuronrobotics.sdk.pid.GenericPIDDevice)1 File (java.io.File)1 IOException (java.io.IOException)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1