@Override public void notifyObserverRelations() { super.notifyObserverRelations(); }
/** * Adds the specified resource as child. This method is syntactic sugar to * have a fluent-interface when adding resources to a tree. For instance, * consider the following example: * * <pre> * server.add( * new CoapResource("foo") * .add(new CoapResource("a") * .add(new CoapResource("a1")) * .add(new CoapResource("a2")) * .add(new CoapResource("a3")) * .add(new CoapResource("a4")) * ) * .add(new CoapResource("b") * .add(new CoapResource("b1") * ) * ) * ); * </pre> * * @param child the child to add * @return this */ public synchronized CoapResource add(CoapResource child) { add( (Resource) child); return this; }
/** * Notifies all CoAP clients that have established an observe relation with * this resource that the state has changed by reprocessing their original * request that has established the relation. The notification is done by * the executor of this resource or on the executor of its parent or * transitively ancestor. If no ancestor defines its own executor, the * thread that has called this method performs the notification. * * @see #changed(ObserveRelationFilter) */ public void changed() { changed(null); }
/** * Delete this resource from its parents and notify all observing CoAP * clients that this resource is no longer accessible. */ public synchronized void delete() { Resource parent = getParent(); if (parent != null) { parent.delete(this); } if (isObservable()) { clearAndNotifyObserveRelations(ResponseCode.NOT_FOUND); } }
/** * Notifies a filtered set of CoAP clients that have established an observe * relation with this resource that the state has changed by reprocessing * their original request that has established the relation. The notification * is done by the executor of this resource or on the executor of its parent or * transitively ancestor. If no ancestor defines its own executor, the * thread that has called this method performs the notification. * * @param filter filter to select set of relations. * <code>null</code>, if all clients should be notified. * * @see #changed() */ public void changed(final ObserveRelationFilter filter) { Executor executor = getExecutor(); // use thread from the protocol stage if (executor == null) notifyObserverRelations(filter); // use thread from the resource pool else executor.execute(new Runnable() { public void run() { notifyObserverRelations(filter); }}); }
this.deliverer = new ServerMessageDeliverer(root); CoapResource well_known = new CoapResource(".well-known"); well_known.setVisible(false); well_known.add(new DiscoveryResource(root)); root.add(well_known);
private void createServer() { CoapEndpoint endpoint = new CoapEndpoint(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); resource = new TestResource(NAME_1, PAYLOAD); server = new CoapServer(); server .add(new CoapResource(RES_A) .add(new CoapResource(RES_AA) .add(resource .add(new TestResource(CHILD, CHILD_PAYLOAD))))); server.addEndpoint(endpoint); server.start(); serverPort = endpoint.getAddress().getPort(); }
public static Resource parseTree(String linkFormat) { Pattern DELIMITER = Pattern.compile("\\s*,+\\s*"); Resource root = new CoapResource(""); Resource resource = new CoapResource(path);
public synchronized void setName(String name) { if (name == null) throw new NullPointerException(); String old = this.name; // adjust parent if in tree Resource parent = getParent(); if (parent!=null) { synchronized (parent) { parent.delete(this); this.name = name; parent.add(this); } } else { this.name = name; } adjustChildrenPath(); for (ResourceObserver obs:observers) obs.changedName(old); }
@Override public void handleRequest(Exchange exchange) { try { super.handleRequest(exchange); } catch (Exception e) { LOG.error("Exception while handling a request on the /bs resource", e); exchange.sendResponse(new Response(ResponseCode.INTERNAL_SERVER_ERROR)); } }
@Override protected void doStop() throws Exception { for (CoapResource r : resources) { r.getParent().delete(r); } resources.clear(); super.doStop(); } }
public synchronized void setPath(String path) { String old = this.path; this.path = path; for (ResourceObserver obs:observers) obs.changedPath(old); adjustChildrenPath(); }
/** * Respond with the specified response. * @param response the response */ public void respond(Response response) { if (response == null) throw new NullPointerException(); // set the response options configured through the CoapExchange API if (locationPath != null) response.getOptions().setLocationPath(locationPath); if (locationQuery != null) response.getOptions().setLocationQuery(locationQuery); if (maxAge != 60) response.getOptions().setMaxAge(maxAge); if (eTag != null) { response.getOptions().clearETags(); response.getOptions().addETag(eTag); } resource.checkObserveRelation(exchange, response); exchange.sendResponse(response); }
addObserveRelation(relation); } else if (observeType != null) {
this.deliverer = new ServerMessageDeliverer(root); CoapResource wellKnown = new CoapResource(".well-known"); wellKnown.setVisible(false); wellKnown.add(new DiscoveryResource(root)); root.add(wellKnown);
/** * Delete this resource from its parents and notify all observing CoAP * clients that this resource is no longer accessible. */ public synchronized void delete() { Resource parent = getParent(); if (parent != null) { parent.delete(this); } if (isObservable()) { clearAndNotifyObserveRelations(ResponseCode.NOT_FOUND); } }
/** * Notifies a filtered set of CoAP clients that have established an observe * relation with this resource that the state has changed by reprocessing * their original request that has established the relation. The notification * is done by the executor of this resource or on the executor of its parent or * transitively ancestor. If no ancestor defines its own executor, the * thread that has called this method performs the notification. * * @param filter filter to select set of relations. * <code>null</code>, if all clients should be notified. * * @see #changed() */ public void changed(final ObserveRelationFilter filter) { Executor executor = getExecutor(); // use thread from the protocol stage if (executor == null) notifyObserverRelations(filter); // use thread from the resource pool else executor.execute(new Runnable() { public void run() { notifyObserverRelations(filter); }}); }
@Before public void setup() { try { System.out.println(System.lineSeparator() + "Start " + getClass().getSimpleName()); EndpointManager.clear(); root = new CoapResource(""); Resource sensors = new CoapResource("sensors"); Resource temp = new CoapResource("temp"); Resource light = new CoapResource("light"); root.add(sensors); sensors.add(temp); sensors.add(light); sensors.getAttributes().setTitle("Sensor Index"); temp.getAttributes().addResourceType("temperature-c"); temp.getAttributes().addInterfaceDescription("sensor"); temp.getAttributes().addAttribute("foo"); temp.getAttributes().addAttribute("bar", "one"); temp.getAttributes().addAttribute("bar", "two"); light.getAttributes().addResourceType("light-lux"); light.getAttributes().addInterfaceDescription("sensor"); } catch (Throwable t) { t.printStackTrace(); } }
public synchronized void setName(String name) { if (name == null) throw new NullPointerException(); String old = this.name; // adjust parent if in tree Resource parent = getParent(); if (parent!=null) { synchronized (parent) { parent.delete(this); this.name = name; parent.add(this); } } else { this.name = name; } adjustChildrenPath(); for (ResourceObserver obs:observers) obs.changedName(old); }
@Override public void handleRequest(Exchange exchange) { try { super.handleRequest(exchange); } catch (Exception e) { LOG.error("Exception while handling a request on the /rd resource", e); // unexpected error, we should sent something like a INTERNAL_SERVER_ERROR. // but it would not be LWM2M compliant. so BAD_REQUEST for now... exchange.sendResponse(new Response(ResponseCode.BAD_REQUEST)); } }