@Override public void registered(Registration reg, Registration previousReg, Collection<Observation> previousObsersations) { if (reg.usesQueueMode()) { presenceService.setAwake(reg); } }
@Override public void updated(RegistrationUpdate update, Registration updatedRegistration, Registration previousRegistration) { if (updatedRegistration.usesQueueMode()) { presenceService.setAwake(updatedRegistration); } }
/** * Notify the listeners that the client state changed to {@link Presence#SLEEPING}. The state changes is produced * inside {@link PresenceStatus} when the timer expires or when the client doesn't respond to a request. * * @param reg the client's registration object */ public void setSleeping(Registration reg) { if (reg.usesQueueMode()) { PresenceStatus status = clientStatusList.get(reg.getEndpoint()); if (status != null) { boolean stateChanged = false; synchronized (status) { stateChanged = status.setSleeping(); stopClientAwakeTimer(reg); } if (stateChanged) { for (PresenceListener listener : listeners) { listener.onSleeping(reg); } } } } }
@Override public JsonElement serialize(Registration src, Type typeOfSrc, JsonSerializationContext context) { JsonObject element = new JsonObject(); element.addProperty("endpoint", src.getEndpoint()); element.addProperty("registrationId", src.getId()); element.add("registrationDate", context.serialize(src.getRegistrationDate())); element.add("lastUpdate", context.serialize(src.getLastUpdate())); element.addProperty("address", src.getAddress().getHostAddress() + ":" + src.getPort()); element.addProperty("smsNumber", src.getSmsNumber()); element.addProperty("lwM2mVersion", src.getLwM2mVersion()); element.addProperty("lifetime", src.getLifeTimeInSec()); element.addProperty("bindingMode", src.getBindingMode().toString()); element.add("rootPath", context.serialize(src.getRootPath())); element.add("objectLinks", context.serialize(src.getSortedObjectLinks())); element.add("secure", context.serialize(src.getIdentity().isSecure())); element.add("additionalRegistrationAttributes", context.serialize(src.getAdditionalRegistrationAttributes())); if (src.usesQueueMode()) { element.add("sleeping", context.serialize(!presenceService.isClientAwake(src))); } return element; } }
if (!destination.usesQueueMode()) { sender.sendCoapRequest(destination, coapRequest, timeout, responseCallback, errorCallback); return;
if (!destination.usesQueueMode()) { delegatedSender.send(destination, request, timeout, responseCallback, errorCallback); return;
/** * Set the state of the client identified by registration as {@link Presence#AWAKE} * * @param reg the client's registration object */ public void setAwake(Registration reg) { if (reg.usesQueueMode()) { PresenceStatus status = new PresenceStatus(); PresenceStatus previous = clientStatusList.putIfAbsent(reg.getEndpoint(), status); if (previous != null) { // We already have a status for this reg. status = previous; } boolean stateChanged = false; synchronized (status) { // Every time we set the clientAwakeTime, in case it changes dynamically stateChanged = status.setAwake(); if (stateChanged) { startClientAwakeTimer(reg, status, awakeTimeProvider.getClientAwakeTime(reg)); } } if (stateChanged) { for (PresenceListener listener : listeners) { listener.onAwake(reg); } } } }
@Override public Response sendCoapRequest(Registration destination, Request coapRequest, long timeout) throws InterruptedException { // Ensure that delegated sender is able to send CoAP request if (!(delegatedSender instanceof CoapRequestSender)) { throw new UnsupportedOperationException("This sender does not support to send CoAP request"); } CoapRequestSender sender = (CoapRequestSender) delegatedSender; // If the client does not use Q-Mode, just send if (!destination.usesQueueMode()) { return sender.sendCoapRequest(destination, coapRequest, timeout); } // If the client uses Q-Mode... // If the client is sleeping, warn the user and return if (!presenceService.isClientAwake(destination)) { throw new ClientSleepingException("The destination client is sleeping, request cannot be sent."); } // Use delegation to send the request Response response = sender.sendCoapRequest(destination, coapRequest, timeout); if (response != null) { // Set the client awake. This will restart the timer. presenceService.setAwake(destination); } else { // If the timeout expires, this means the client does not respond. presenceService.clientNotResponding(destination); } // Wait for response, then return it return response; }
@Override public <T extends LwM2mResponse> T send(final Registration destination, DownlinkRequest<T> request, long timeout) throws InterruptedException { // If the client does not use Q-Mode, just send if (!destination.usesQueueMode()) { return delegatedSender.send(destination, request, timeout); } // If the client uses Q-Mode... // If the client is sleeping, warn the user and return if (!presenceService.isClientAwake(destination)) { throw new ClientSleepingException("The destination client is sleeping, request cannot be sent."); } // Use delegation to send the request T response = delegatedSender.send(destination, request, timeout); if (response != null) { // Set the client awake. This will restart the timer. presenceService.setAwake(destination); } else { // If the timeout expires, this means the client does not respond. presenceService.clientNotResponding(destination); } // Wait for response, then return it return response; }