/** * Unregisters as service advertiser. */ public void unadvertiseService() { this.ros.deregisterCallServiceCallback(this.name); // build and send the rosbridge call JsonObject call = Json.createObjectBuilder() .add(JRosbridge.FIELD_OP, JRosbridge.OP_CODE_UNADVERTISE_SERVICE) .add(JRosbridge.FIELD_SERVICE, this.name).build(); this.ros.send(call); // set the flag indicating we are registered this.isAdvertised = false; }
@Override public void disconnectFromExternal() { ros.disconnect(); }
/** * Unregister as a publisher for the topic. */ public void unadvertise() { // build and send the rosbridge call String unadvertiseId = "unadvertise:" + this.name + ":" + this.ros.nextId(); JsonObject call = Json.createObjectBuilder() .add(JRosbridge.FIELD_OP, JRosbridge.OP_CODE_UNADVERTISE) .add(JRosbridge.FIELD_ID, unadvertiseId) .add(JRosbridge.FIELD_TOPIC, this.name).build(); this.ros.send(call); // set the flag indicating we are no longer registered this.isAdvertised = false; }
/** * Call this service. The callback function will be called with the * associated service response. * * @param request * The service request to send. * @param cb * The callback used when the associated response comes back. */ public void callService(ServiceRequest request, ServiceCallback cb) { // construct the unique ID String callServceId = "call_service:" + this.name + ":" + this.ros.nextId(); // register the callback function this.ros.registerServiceCallback(callServceId, cb); // build and send the rosbridge call JsonObject call = Json.createObjectBuilder() .add(JRosbridge.FIELD_OP, JRosbridge.OP_CODE_CALL_SERVICE) .add(JRosbridge.FIELD_ID, callServceId) .add(JRosbridge.FIELD_TYPE, this.type) .add(JRosbridge.FIELD_SERVICE, this.name) .add(JRosbridge.FIELD_ARGS, request.toJsonObject()).build(); this.ros.send(call); }
/** * Subscribe to this topic. A callback function is required and will be * called with any incoming message for this topic. * * @param cb * The callback that will be called when incoming messages are * received. */ public void subscribe(TopicCallback cb) { // register the callback function this.ros.registerTopicCallback(this.name, cb); // internal reference used during unsubscribe this.callbacks.add(cb); String subscribeId = "subscribe:" + this.name + ":" + this.ros.nextId(); this.ids.add(subscribeId); // build and send the rosbridge call JsonObject call = Json.createObjectBuilder() .add(JRosbridge.FIELD_OP, JRosbridge.OP_CODE_SUBSCRIBE) .add(JRosbridge.FIELD_ID, subscribeId) .add(JRosbridge.FIELD_TYPE, this.type) .add(JRosbridge.FIELD_TOPIC, this.name) .add(JRosbridge.FIELD_COMPRESSION, this.compression.toString()) .add(JRosbridge.FIELD_THROTTLE_RATE, this.throttleRate).build(); this.ros.send(call); // set the flag indicating we have subscribed this.isSubscribed = true; }
Ros ros = new Ros(host); ros.connect(); ros.disconnect();
/** * Registers as service advertiser. */ public void advertiseService(CallServiceCallback cb) { // register the callback this.ros.registerCallServiceCallback(this.name, cb); // build and send the rosbridge call JsonObject call = Json.createObjectBuilder() .add(JRosbridge.FIELD_OP, JRosbridge.OP_CODE_ADVERTISE_SERVICE) .add(JRosbridge.FIELD_TYPE, this.type) .add(JRosbridge.FIELD_SERVICE, this.name).build(); this.ros.send(call); // set the flag indicating we are registered this.isAdvertised = true; }
/** * Unregisters as a subscriber for the topic. Unsubscribing will remove all * the associated subscribe callbacks. */ public void unsubscribe() { // remove this object's associated callbacks. for (TopicCallback cb : this.callbacks) { this.ros.deregisterTopicCallback(this.name, cb); } this.callbacks.clear(); // build and send the rosbridge calls for (String id : this.ids) { JsonObject call = Json.createObjectBuilder() .add(JRosbridge.FIELD_OP, JRosbridge.OP_CODE_UNSUBSCRIBE) .add(JRosbridge.FIELD_ID, id) .add(JRosbridge.FIELD_TOPIC, this.name).build(); this.ros.send(call); } // set the flag indicating we are not longer subscribed this.isSubscribed = false; }
/** * Disconnect the connection to rosbridge. Errors are printed to the error * output stream. * * @return Returns true if the disconnection was successful and false * otherwise. */ public boolean disconnect() { if (this.isConnected()) { try { this.session.close(); return true; } catch (IOException e) { System.err.println("[ERROR]: Could not disconnect: " + e.getMessage()); } } // could not disconnect cleanly return false; }
/** * Attempt to establish a connection to rosbridge. Errors are printed to the * error output stream. * * @return Returns true if the connection was established successfully and * false otherwise. */ public boolean connect() { try { // create a WebSocket connection here URI uri = new URI(this.getURL()); ContainerProvider.getWebSocketContainer() .connectToServer(this, uri); return true; } catch (DeploymentException | URISyntaxException | IOException e) { // failed connection, return false System.err.println("[ERROR]: Could not create WebSocket: " + e.getMessage()); return false; } }
handleMessage(newJsonObject); } else { handleMessage(jsonObject);
/** * Unregister as a publisher for the topic. */ public void unadvertise() { // build and send the rosbridge call String unadvertiseId = "unadvertise:" + this.name + ":" + this.ros.nextId(); JsonObject call = Json.createObjectBuilder() .add(JRosbridge.FIELD_OP, JRosbridge.OP_CODE_UNADVERTISE) .add(JRosbridge.FIELD_ID, unadvertiseId) .add(JRosbridge.FIELD_TOPIC, this.name).build(); this.ros.send(call); // set the flag indicating we are no longer registered this.isAdvertised = false; }
/** * Call this service. The callback function will be called with the * associated service response. * * @param request * The service request to send. * @param cb * The callback used when the associated response comes back. */ public void callService(ServiceRequest request, ServiceCallback cb) { // construct the unique ID String callServceId = "call_service:" + this.name + ":" + this.ros.nextId(); // register the callback function this.ros.registerServiceCallback(callServceId, cb); // build and send the rosbridge call JsonObject call = Json.createObjectBuilder() .add(JRosbridge.FIELD_OP, JRosbridge.OP_CODE_CALL_SERVICE) .add(JRosbridge.FIELD_ID, callServceId) .add(JRosbridge.FIELD_TYPE, this.type) .add(JRosbridge.FIELD_SERVICE, this.name) .add(JRosbridge.FIELD_ARGS, request.toJsonObject()).build(); this.ros.send(call); }
/** * Subscribe to this topic. A callback function is required and will be * called with any incoming message for this topic. * * @param cb * The callback that will be called when incoming messages are * received. */ public void subscribe(TopicCallback cb) { // register the callback function this.ros.registerTopicCallback(this.name, cb); // internal reference used during unsubscribe this.callbacks.add(cb); String subscribeId = "subscribe:" + this.name + ":" + this.ros.nextId(); this.ids.add(subscribeId); // build and send the rosbridge call JsonObject call = Json.createObjectBuilder() .add(JRosbridge.FIELD_OP, JRosbridge.OP_CODE_SUBSCRIBE) .add(JRosbridge.FIELD_ID, subscribeId) .add(JRosbridge.FIELD_TYPE, this.type) .add(JRosbridge.FIELD_TOPIC, this.name) .add(JRosbridge.FIELD_COMPRESSION, this.compression.toString()) .add(JRosbridge.FIELD_THROTTLE_RATE, this.throttleRate).build(); this.ros.send(call); // set the flag indicating we have subscribed this.isSubscribed = true; }
/** * Registers as service advertiser. * * @param cb the cb */ public void advertiseService(CallServiceCallback cb) { // register the callback this.ros.registerCallServiceCallback(this.name, cb); // build and send the rosbridge call JsonObject call = Json.createObjectBuilder() .add(JRosbridge.FIELD_OP, JRosbridge.OP_CODE_ADVERTISE_SERVICE) .add(JRosbridge.FIELD_TYPE, this.type) .add(JRosbridge.FIELD_SERVICE, this.name).build(); this.ros.send(call); // set the flag indicating we are registered this.isAdvertised = true; }
/** * Unregisters as a subscriber for the topic. Unsubscribing will remove all * the associated subscribe callbacks. */ public void unsubscribe() { // remove this object's associated callbacks. for (TopicCallback cb : this.callbacks) { this.ros.deregisterTopicCallback(this.name, cb); } this.callbacks.clear(); // build and send the rosbridge calls for (String id : this.ids) { JsonObject call = Json.createObjectBuilder() .add(JRosbridge.FIELD_OP, JRosbridge.OP_CODE_UNSUBSCRIBE) .add(JRosbridge.FIELD_ID, id) .add(JRosbridge.FIELD_TOPIC, this.name).build(); this.ros.send(call); } // set the flag indicating we are not longer subscribed this.isSubscribed = false; }
/** * Disconnect the connection to rosbridge. Errors are printed to the error * output stream. * * @return Returns true if the disconnection was successful and false * otherwise. */ public boolean disconnect() { if (this.isConnected()) { try { this.session.close(); return true; } catch (IOException e) { System.err.println("[ERROR]: Could not disconnect: " + e.getMessage()); } } // could not disconnect cleanly return false; }
/** * Attempt to establish a connection to rosbridge. Errors are printed to the * error output stream. * * @return Returns true if the connection was established successfully and * false otherwise. */ public boolean connect() { try { // create a WebSocket connection here URI uri = new URI(this.getURL()); ContainerProvider.getWebSocketContainer() .connectToServer(this, uri); return true; } catch (DeploymentException | URISyntaxException | IOException e) { // failed connection, return false System.err.println("[ERROR]: Could not create WebSocket: " + e.getMessage()); return false; } }
handleMessage(newJsonObject); } else { handleMessage(jsonObject);