private ThingState getStateFor(NodeInfo nodeInfo) { ThingState state = things.get(nodeInfo); if (state == null) { state = new ThingState(nodeInfo); things.put(nodeInfo, state); } return state; }
@SuppressWarnings("ObjectToString") @Override public IQ handleIQRequest(IQ iqRequest) { if (!isRegistry(iqRequest.getFrom())) { LOGGER.log(Level.SEVERE, "Received control stanza from non-registry entity: " + iqRequest); return null; } IoTDisowned iotDisowned = (IoTDisowned) iqRequest; Jid from = iqRequest.getFrom(); NodeInfo nodeInfo = iotDisowned.getNodeInfo(); ThingState state = getStateFor(nodeInfo); if (!from.equals(state.getRegistry())) { LOGGER.severe("Received <disowned/> for " + nodeInfo + " from " + from + " but this is not the registry " + state.getRegistry() + " of the thing."); return null; } if (state.isOwned()) { state.setUnowned(); } else { LOGGER.fine("Received <disowned/> for " + nodeInfo + " but thing was not owned."); } return IQ.createResultIQ(iqRequest); } });
@Override public IQ handleIQRequest(IQ iqRequest) { if (!isRegistry(iqRequest.getFrom())) { LOGGER.log(Level.SEVERE, "Received control stanza from non-registry entity: " + iqRequest); return null; } IoTClaimed iotClaimed = (IoTClaimed) iqRequest; Jid owner = iotClaimed.getJid(); NodeInfo nodeInfo = iotClaimed.getNodeInfo(); // Update the state. ThingState state = getStateFor(nodeInfo); state.setOwner(owner.asBareJid()); LOGGER.info("Our thing got claimed by " + owner + ". " + iotClaimed); IoTProvisioningManager iotProvisioningManager = IoTProvisioningManager.getInstanceFor( connection()); try { iotProvisioningManager.sendFriendshipRequest(owner.asBareJid()); } catch (NotConnectedException | InterruptedException e) { LOGGER.log(Level.WARNING, "Could not friendship owner", e); } return IQ.createResultIQ(iqRequest); } });
@Override public void iotScenario(XMPPTCPConnection dataThingConnection, XMPPTCPConnection readingThingConnection) throws TimeoutException, Exception { ThingState dataThingState = actAsDataThing(dataThingConnection); final SimpleResultSyncPoint syncPoint = new SimpleResultSyncPoint(); dataThingState.setThingStateChangeListener(new AbstractThingStateChangeListener() { @Override public void owned(BareJid jid) { syncPoint.signal(); } }); // Wait until the thing is owned. syncPoint.waitForResult(TIMEOUT); printStatus("OWNED - Thing now owned by " + dataThingState.getOwner()); // Make sure things are befriended. IoTProvisioningManager readingThingProvisioningManager = IoTProvisioningManager.getInstanceFor(readingThingConnection); readingThingProvisioningManager.sendFriendshipRequestIfRequired(dataThingConnection.getUser().asBareJid()); Roster dataThingRoster = Roster.getInstanceFor(dataThingConnection); RosterUtil.waitUntilOtherEntityIsSubscribed(dataThingRoster, readingThingConnection.getUser().asBareJid(), TIMEOUT); printStatus("FRIENDSHIP ACCEPTED - Trying to read out data"); IoTDataManager readingThingDataManager = IoTDataManager.getInstanceFor(readingThingConnection); List<IoTFieldsExtension> values = readingThingDataManager.requestMomentaryValuesReadOut(dataThingConnection.getUser()); if (values.size() != 1) { throw new IllegalStateException("Unexpected number of values returned: " + values.size()); } IoTFieldsExtension field = values.get(0); printStatus("DATA READ-OUT SUCCESS: " + field.toXML(null)); printStatus("IoT SCENARIO FINISHED SUCCESSFULLY"); } };
public void unregister(Jid registry, NodeInfo nodeInfo) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { interactWithRegistry(registry); IoTUnregister iotUnregister = new IoTUnregister(nodeInfo); iotUnregister.setTo(registry); connection().createStanzaCollectorAndSend(iotUnregister).nextResultOrThrow(); ThingState state = getStateFor(nodeInfo); state.setUnregistered(); final XMPPConnection connection = connection(); IoTDataManager.getInstanceFor(connection).uninstallThing(nodeInfo); IoTControlManager.getInstanceFor(connection).uninstallThing(nodeInfo); }
public ThingState registerThing(Jid registry, Thing thing) throws NotConnectedException, InterruptedException, NoResponseException, XMPPErrorException, IoTClaimedException { final XMPPConnection connection = connection(); IoTRegister iotRegister = new IoTRegister(thing.getMetaTags(), thing.getNodeInfo(), thing.isSelfOwened()); iotRegister.setTo(registry); IQ result = connection.createStanzaCollectorAndSend(iotRegister).nextResultOrThrow(); if (result instanceof IoTClaimed) { IoTClaimed iotClaimedResult = (IoTClaimed) result; throw new IoTClaimedException(iotClaimedResult); } ThingState state = getStateFor(thing.getNodeInfo()); state.setRegistry(registry.asBareJid()); interactWithRegistry(registry); IoTDataManager.getInstanceFor(connection).installThing(thing); IoTControlManager.getInstanceFor(connection).installThing(thing); return state; }
@Override public IQ handleIQRequest(IQ iqRequest) { if (!isRegistry(iqRequest.getFrom())) { LOGGER.log(Level.SEVERE, "Received control stanza from non-registry entity: " + iqRequest); return null; } IoTRemoved iotRemoved = (IoTRemoved) iqRequest; ThingState state = getStateFor(iotRemoved.getNodeInfo()); state.setRemoved(); // Unfriend registry. "It does this, so the Thing can remove the friendship and stop any // meta data updates to the Registry." try { IoTProvisioningManager.getInstanceFor(connection()).unfriend(iotRemoved.getFrom()); } catch (NotConnectedException | InterruptedException e) { LOGGER.log(Level.SEVERE, "Could not unfriend registry after <removed/>", e); } return IQ.createResultIQ(iqRequest); } });
public void unregister(Jid registry, NodeInfo nodeInfo) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { interactWithRegistry(registry); IoTUnregister iotUnregister = new IoTUnregister(nodeInfo); iotUnregister.setTo(registry); connection().createStanzaCollectorAndSend(iotUnregister).nextResultOrThrow(); ThingState state = getStateFor(nodeInfo); state.setUnregistered(); final XMPPConnection connection = connection(); IoTDataManager.getInstanceFor(connection).uninstallThing(nodeInfo); IoTControlManager.getInstanceFor(connection).uninstallThing(nodeInfo); }
public ThingState registerThing(Jid registry, Thing thing) throws NotConnectedException, InterruptedException, NoResponseException, XMPPErrorException, IoTClaimedException { final XMPPConnection connection = connection(); IoTRegister iotRegister = new IoTRegister(thing.getMetaTags(), thing.getNodeInfo(), thing.isSelfOwened()); iotRegister.setTo(registry); IQ result = connection.createStanzaCollectorAndSend(iotRegister).nextResultOrThrow(); if (result instanceof IoTClaimed) { IoTClaimed iotClaimedResult = (IoTClaimed) result; throw new IoTClaimedException(iotClaimedResult); } ThingState state = getStateFor(thing.getNodeInfo()); state.setRegistry(registry.asBareJid()); interactWithRegistry(registry); IoTDataManager.getInstanceFor(connection).installThing(thing); IoTControlManager.getInstanceFor(connection).installThing(thing); return state; }
@Override public IQ handleIQRequest(IQ iqRequest) { if (!isRegistry(iqRequest.getFrom())) { LOGGER.log(Level.SEVERE, "Received control stanza from non-registry entity: " + iqRequest); return null; } IoTRemoved iotRemoved = (IoTRemoved) iqRequest; ThingState state = getStateFor(iotRemoved.getNodeInfo()); state.setRemoved(); // Unfriend registry. "It does this, so the Thing can remove the friendship and stop any // meta data updates to the Registry." try { IoTProvisioningManager.getInstanceFor(connection()).unfriend(iotRemoved.getFrom()); } catch (NotConnectedException | InterruptedException e) { LOGGER.log(Level.SEVERE, "Could not unfriend registry after <removed/>", e); } return IQ.createResultIQ(iqRequest); } });
dataThingState.setThingStateChangeListener(new AbstractThingStateChangeListener() { @Override public void owned(BareJid jid) { printStatus("OWNED - Thing now owned by " + dataThingState.getOwner());
@Override public IQ handleIQRequest(IQ iqRequest) { if (!isRegistry(iqRequest.getFrom())) { LOGGER.log(Level.SEVERE, "Received control stanza from non-registry entity: " + iqRequest); return null; } IoTDisowned iotDisowned = (IoTDisowned) iqRequest; Jid from = iqRequest.getFrom(); NodeInfo nodeInfo = iotDisowned.getNodeInfo(); ThingState state = getStateFor(nodeInfo); if (!from.equals(state.getRegistry())) { LOGGER.severe("Received <disowned/> for " + nodeInfo + " from " + from + " but this is not the registry " + state.getRegistry() + " of the thing."); return null; } if (state.isOwned()) { state.setUnowned(); } else { LOGGER.fine("Received <disowned/> for " + nodeInfo + " but thing was not owned."); } return IQ.createResultIQ(iqRequest); } });
private ThingState getStateFor(NodeInfo nodeInfo) { ThingState state = things.get(nodeInfo); if (state == null) { state = new ThingState(nodeInfo); things.put(nodeInfo, state); } return state; }
@Override public IQ handleIQRequest(IQ iqRequest) { if (!isRegistry(iqRequest.getFrom())) { LOGGER.log(Level.SEVERE, "Received control stanza from non-registry entity: " + iqRequest); return null; } IoTClaimed iotClaimed = (IoTClaimed) iqRequest; Jid owner = iotClaimed.getJid(); NodeInfo nodeInfo = iotClaimed.getNodeInfo(); // Update the state. ThingState state = getStateFor(nodeInfo); state.setOwner(owner.asBareJid()); LOGGER.info("Our thing got claimed by " + owner + ". " + iotClaimed); IoTProvisioningManager iotProvisioningManager = IoTProvisioningManager.getInstanceFor( connection()); try { iotProvisioningManager.sendFriendshipRequest(owner.asBareJid()); } catch (NotConnectedException | InterruptedException e) { LOGGER.log(Level.WARNING, "Could not friendship owner", e); } return IQ.createResultIQ(iqRequest); } });