Skip to content

Commit e81d0c1

Browse files
committed
1. Is confirmation required? 2. Rename registrationKey to registrationCode.
1 parent b4d758a commit e81d0c1

File tree

46 files changed

+290
-180
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+290
-180
lines changed

client/actuator/src/main/java/com/thefirstlineofcode/sand/client/actuator/Actuator.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ private <T> IExecutor<T> createExecutor(T action) throws ProtocolException {
123123
}
124124

125125
@SuppressWarnings("unchecked")
126-
private <T> void injectWorker(IWorkerAware<T> executor, Object worker) {
127-
executor.setWorker((T)worker);
126+
private <T> void injectThingController(IThingControllerAware<T> executor, Object thingController) {
127+
executor.setThingController((T)thingController);
128128
}
129129

130130
private IExecutor<?> createExecutor(Class<? extends IExecutor<?>> executorType) {
@@ -210,21 +210,21 @@ public <T> void registerExecutor(Class<T> actionType, Class<? extends IExecutor<
210210
}
211211

212212
@Override
213-
public <T> void registerExecutor(Class<T> actionType, Class<? extends IExecutor<T>> executorType, Object worker) {
214-
registerExecutorFactory(new CreateByTypeExecutorFactory<T>(actionType, executorType, worker));
213+
public <T> void registerExecutor(Class<T> actionType, Class<? extends IExecutor<T>> executorType, Object thingController) {
214+
registerExecutorFactory(new CreateByTypeExecutorFactory<T>(actionType, executorType, thingController));
215215
}
216216

217217
private class CreateByTypeExecutorFactory<T> implements IExecutorFactory<T> {
218218
private Protocol protocol;
219219
private Class<T> actionType;
220220
private Class<? extends IExecutor<T>> executorType;
221-
private Object worker;
221+
private Object thingController;
222222

223223
public CreateByTypeExecutorFactory(Class<T> actionType,
224-
Class<? extends IExecutor<T>> executorType, Object worker) {
224+
Class<? extends IExecutor<T>> executorType, Object thingController) {
225225
this.actionType = actionType;
226226
this.executorType = executorType;
227-
this.worker = worker;
227+
this.thingController = thingController;
228228

229229
ProtocolObject protocolObject = actionType.getAnnotation(ProtocolObject.class);
230230
if (protocolObject == null)
@@ -238,8 +238,8 @@ public CreateByTypeExecutorFactory(Class<T> actionType,
238238
@Override
239239
public IExecutor<T> create() {
240240
IExecutor<T> executor = (IExecutor<T>)createExecutor(executorType);
241-
if (executor instanceof IWorkerAware) {
242-
injectWorker((IWorkerAware<?>)executor, worker);
241+
if (executor instanceof IThingControllerAware) {
242+
injectThingController((IThingControllerAware<?>)executor, thingController);
243243
}
244244

245245
return executor;

client/actuator/src/main/java/com/thefirstlineofcode/sand/client/actuator/IActuator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public interface IActuator {
44
<T> void registerExecutor(Class<T> actionType, Class<? extends IExecutor<T>> executorType);
5-
<T> void registerExecutor(Class<T> actionType, Class<? extends IExecutor<T>> executorType, Object worker);
5+
<T> void registerExecutor(Class<T> actionType, Class<? extends IExecutor<T>> executorType, Object thingController);
66
<T> void registerExecutorFactory(IExecutorFactory<T> executorFactory);
77
boolean isExecutorRegistered(Class<?> actionType);
88
boolean unregisterExecutor(Class<?> actionType);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.thefirstlineofcode.sand.client.actuator;
2+
3+
public interface IThingControllerAware<T> {
4+
void setThingController(T thingController);
5+
}

client/actuator/src/main/java/com/thefirstlineofcode/sand/client/actuator/IWorkerAware.java

Lines changed: 0 additions & 5 deletions
This file was deleted.

client/concentrator/src/main/java/com/thefirstlineofcode/sand/client/concentrator/Concentrator.java

Lines changed: 36 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.thefirstlineofcode.basalt.xmpp.core.stanza.Iq;
3434
import com.thefirstlineofcode.basalt.xmpp.core.stanza.Stanza;
3535
import com.thefirstlineofcode.basalt.xmpp.core.stanza.error.BadRequest;
36+
import com.thefirstlineofcode.basalt.xmpp.core.stanza.error.Conflict;
3637
import com.thefirstlineofcode.basalt.xmpp.core.stanza.error.InternalServerError;
3738
import com.thefirstlineofcode.basalt.xmpp.core.stanza.error.ItemNotFound;
3839
import com.thefirstlineofcode.basalt.xmpp.core.stanza.error.NotAcceptable;
@@ -741,10 +742,11 @@ public void addNode(LanNode node) {
741742
}
742743

743744
@Override
744-
public void requestServerToAddNode(String thingId, int lanId, IAddress address) {
745+
public void requestServerToAddNode(String thingId, String registrationCode, int lanId, IAddress address) {
745746
synchronized (nodesLock) {
746747
LanNode node = new LanNode();
747748
node.setThingId(thingId);
749+
node.setRegistrationCode(registrationCode);
748750
node.setLanId(lanId);
749751
node.setCommunicationNet(address.getCommunicationNet());
750752
node.setAddress(address.toAddressString());
@@ -755,9 +757,7 @@ public void requestServerToAddNode(String thingId, int lanId, IAddress address)
755757
logger.error("Node size overflow.");
756758
}
757759

758-
for (Listener listener : listeners) {
759-
listener.occurred(AddNodeError.SIZE_OVERFLOW, node);
760-
}
760+
processNodeAdditionError(AddNodeError.SIZE_OVERFLOW, node);
761761

762762
return;
763763
}
@@ -768,10 +768,7 @@ public void requestServerToAddNode(String thingId, int lanId, IAddress address)
768768
logger.error("Reduplicate thing ID: {}.", node.getThingId());
769769
}
770770

771-
for (Listener listener : listeners) {
772-
listener.occurred(AddNodeError.REDUPLICATE_THING_ID, node);
773-
}
774-
771+
processNodeAdditionError(AddNodeError.REDUPLICATE_THING_ID, node);
775772
return;
776773
}
777774

@@ -780,10 +777,7 @@ public void requestServerToAddNode(String thingId, int lanId, IAddress address)
780777
logger.error("Reduplicate thing address: {}.", node.getAddress());
781778
}
782779

783-
for (Listener listener : listeners) {
784-
listener.occurred(AddNodeError.REDUPLICATE_THING_ADDRESS, node);
785-
}
786-
780+
processNodeAdditionError(AddNodeError.REDUPLICATE_THING_ADDRESS, node);
787781
return;
788782
}
789783

@@ -792,10 +786,7 @@ public void requestServerToAddNode(String thingId, int lanId, IAddress address)
792786
logger.error("Reduplicate thing LAN ID: {}.", node.getLanId());
793787
}
794788

795-
for (Listener listener : listeners) {
796-
listener.occurred(AddNodeError.REDUPLICATE_THING_ADDRESS, node);
797-
}
798-
789+
processNodeAdditionError(AddNodeError.REDUPLICATE_LAN_ID, node);
799790
return;
800791
}
801792
}
@@ -820,6 +811,7 @@ public NodeAdditionTask(LanNode node) {
820811
public void trigger(IUnidirectionalStream<Iq> stream) {
821812
AddNode addNode = new AddNode();
822813
addNode.setThingId(node.getThingId());
814+
addNode.setRegistrationCode(node.getRegistrationCode());
823815
addNode.setLanId(node.getLanId());
824816
addNode.setCommunicationNet(node.getCommunicationNet().toString());
825817
addNode.setAddress(node.getAddress());
@@ -834,18 +826,6 @@ public void trigger(IUnidirectionalStream<Iq> stream) {
834826

835827
@Override
836828
public void processResponse(IUnidirectionalStream<Iq> stream, Iq iq) {
837-
if (iq.getType() != Iq.Type.RESULT || iq.getObject() == null) {
838-
if (logger.isErrorEnabled()) {
839-
logger.error("Server returned a bad response. Response is {}.", iq);
840-
}
841-
842-
for (IConcentrator.Listener listener : listeners) {
843-
listener.occurred(IConcentrator.AddNodeError.BAD_NODE_ADDITION_RESPONSE, node);
844-
}
845-
846-
return;
847-
}
848-
849829
NodeAdded nodeAdded = iq.getObject();
850830

851831
LanNode confirmingNode = confirmingNodes.get(iq.getId());
@@ -854,10 +834,7 @@ public void processResponse(IUnidirectionalStream<Iq> stream, Iq iq) {
854834
logger.error("Confirming node which's thing ID is '{}' not found.", nodeAdded.getNodeThingId());
855835
}
856836

857-
for (IConcentrator.Listener listener : listeners) {
858-
listener.occurred(IConcentrator.AddNodeError.ADDED_NODE_NOT_FOUND, node);
859-
}
860-
837+
processNodeAdditionError(IConcentrator.AddNodeError.ADDED_NODE_NOT_FOUND, node);
861838
return;
862839
}
863840

@@ -870,10 +847,7 @@ public void processResponse(IUnidirectionalStream<Iq> stream, Iq iq) {
870847
getThingName(), confirmingNode.getThingId(), nodeAdded.getNodeThingId(), nodeAdded.getConcentratorThingName());
871848
}
872849

873-
for (IConcentrator.Listener listener : listeners) {
874-
listener.occurred(IConcentrator.AddNodeError.BAD_NODE_ADDITION_RESPONSE, node);
875-
}
876-
850+
processNodeAdditionError(IConcentrator.AddNodeError.BAD_NODE_ADDITION_RESPONSE, node);
877851
return;
878852
}
879853

@@ -882,18 +856,15 @@ public void processResponse(IUnidirectionalStream<Iq> stream, Iq iq) {
882856
logger.error("Bad node addition response. The server changed LAN ID of node to {}.", nodeAdded.getLanId());
883857
}
884858

885-
for (IConcentrator.Listener listener : listeners) {
886-
listener.occurred(IConcentrator.AddNodeError.SERVER_CHANGED_LAN_ID, node);
887-
}
888-
859+
processNodeAdditionError(IConcentrator.AddNodeError.SERVER_CHANGED_LAN_ID, node);
889860
return;
890861
}
891862

892-
confirmingNode.setModel(nodeAdded.getModel());
893-
894863
synchronized (nodesLock) {
895864
confirmingNodes.remove(iq.getId());
896865

866+
confirmingNode.setModel(nodeAdded.getModel());
867+
confirmingNode.setRegistrationCode(null);
897868
confirmingNode.setConfirmed(true);
898869
nodes.put(confirmingNode.getLanId(), confirmingNode);
899870
}
@@ -910,18 +881,22 @@ public void processResponse(IUnidirectionalStream<Iq> stream, Iq iq) {
910881

911882
@Override
912883
public boolean processError(IUnidirectionalStream<Iq> stream, StanzaError error) {
913-
if (logger.isErrorEnabled()) {
914-
logger.error("Some errors occurred while adding node. Error defined condition: '{}'. Error text: '{}'.",
915-
error.getDefinedCondition(), error.getText());
916-
}
917-
918884
try {
919-
for (IConcentrator.Listener listener : listeners) {
920-
listener.occurred(error, node);
921-
}
922-
} catch (Exception e) {
923885
if (logger.isErrorEnabled()) {
924-
logger.error("Exception was thrown while processing error.", e);
886+
logger.error("Stanza error receiving when requesting to add node to concentrator. Stanz error: '{}'.",
887+
error.getDefinedCondition(), error.getText());
888+
}
889+
890+
if (ItemNotFound.DEFINED_CONDITION.equals(error.getDefinedCondition())) {
891+
processNodeAdditionError(IConcentrator.AddNodeError.NO_SUCH_CONCENTRATOR, node);
892+
} else if (ServiceUnavailable.DEFINED_CONDITION.equals(error.getDefinedCondition())) {
893+
processNodeAdditionError(IConcentrator.AddNodeError.NOT_CONCENTRATOR, node);
894+
} else if (Conflict.DEFINED_CONDITION.equals(error.getDefinedCondition())) {
895+
processNodeAdditionError(IConcentrator.AddNodeError.REDUPLICATE_NODE_OR_LAN_ID, node);
896+
} else if (NotAcceptable.DEFINED_CONDITION.equals(error.getDefinedCondition())) {
897+
processNodeAdditionError(IConcentrator.AddNodeError.NOT_UNREGISTERED_THING, node);
898+
} else {
899+
processNodeAdditionError(IConcentrator.AddNodeError.UNKNOWN_ERROR, node);
925900
}
926901
} finally {
927902
synchronized (nodesLock) {
@@ -940,9 +915,7 @@ public boolean processTimeout(IUnidirectionalStream<Iq> stream, Iq iq) {
940915

941916
nodes.remove(node.getLanId());
942917

943-
for (IConcentrator.Listener listener : listeners) {
944-
listener.occurred(new RemoteServerTimeout(), node);
945-
}
918+
processNodeAdditionError(IConcentrator.AddNodeError.REMOTE_SERVER_TIMEOUT, node);
946919

947920
return true;
948921
}
@@ -961,6 +934,12 @@ public void removeNode(int lanId) {
961934
}
962935
}
963936

937+
private void processNodeAdditionError(IConcentrator.AddNodeError error, LanNode node) {
938+
for (IConcentrator.Listener listener : listeners) {
939+
listener.occurred(error, node);
940+
}
941+
}
942+
964943
@Override
965944
public void setNodes(Map<Integer, LanNode> nodes) {
966945
synchronized (nodesLock) {
@@ -1031,8 +1010,8 @@ public void processResponse(IUnidirectionalStream<Iq> stream, Iq iq) {
10311010
for (LanNode lanNode : oldNodes) {
10321011
if (!lanNode.isConfirmed() && !isConfirmed(pulledLanNodes.values(), lanNode)) {
10331012
try {
1034-
requestServerToAddNode(lanNode.getThingId(), lanNode.getLanId(),
1035-
lanNode.getCommunicationNet().parse(lanNode.getAddress()));
1013+
requestServerToAddNode(lanNode.getThingId(), lanNode.getRegistrationCode(),
1014+
lanNode.getLanId(), lanNode.getCommunicationNet().parse(lanNode.getAddress()));
10361015
} catch (BadAddressException e) {
10371016
throw new RuntimeException("Why???", e);
10381017
}

client/concentrator/src/main/java/com/thefirstlineofcode/sand/client/concentrator/IConcentrator.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,21 @@ public enum AddNodeError {
2222
REDUPLICATE_THING_ID,
2323
REDUPLICATE_THING_ADDRESS,
2424
REDUPLICATE_LAN_ID,
25-
BAD_NODE_ADDITION_RESPONSE,
2625
ADDED_NODE_NOT_FOUND,
27-
SERVER_CHANGED_LAN_ID
26+
SERVER_CHANGED_LAN_ID,
27+
BAD_NODE_ADDITION_RESPONSE,
28+
NO_SUCH_CONCENTRATOR,
29+
NOT_CONCENTRATOR,
30+
REDUPLICATE_NODE_OR_LAN_ID,
31+
NOT_UNREGISTERED_THING,
32+
REMOTE_SERVER_TIMEOUT,
33+
UNKNOWN_ERROR
2834
}
2935

3036
void addCommunicator(CommunicationNet net, ICommunicator<?, ?, byte[]> communicator);
3137
ICommunicator<?, ?, byte[]> getCommunicator(CommunicationNet communicationNet);
3238
int getBestSuitedNewLanId();
33-
void requestServerToAddNode(String thingId, int lanId, IAddress address);
39+
void requestServerToAddNode(String thingId, String registrationCode, int lanId, IAddress address);
3440
void removeNode(int lanId) throws NodeNotFoundException;
3541
void cleanNodes();
3642
void setNodes(Map<Integer, LanNode> nodes);
@@ -82,7 +88,6 @@ public interface Listener {
8288
void nodeAdded(int lanId, LanNode node);
8389
void nodeReset(int lanId, LanNode node);
8490
void nodeRemoved(int lanId, LanNode node);
85-
void occurred(StanzaError error, LanNode source);
8691
void occurred(AddNodeError error, LanNode source);
8792
}
8893

client/concentrator/src/main/java/com/thefirstlineofcode/sand/client/concentrator/LanNode.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
public class LanNode implements Externalizable {
1111
private String thingId;
12+
private String registrationCode;
1213
private Integer lanId;
1314
private String model;
1415
private CommunicationNet communicationNet;
@@ -22,6 +23,14 @@ public String getThingId() {
2223
public void setThingId(String thingId) {
2324
this.thingId = thingId;
2425
}
26+
27+
public String getRegistrationCode() {
28+
return registrationCode;
29+
}
30+
31+
public void setRegistrationCode(String registrationCode) {
32+
this.registrationCode = registrationCode;
33+
}
2534

2635
public Integer getLanId() {
2736
return lanId;
@@ -66,6 +75,7 @@ public void setConfirmed(boolean confirmed) {
6675
@Override
6776
public void writeExternal(ObjectOutput out) throws IOException {
6877
out.writeObject(thingId);
78+
out.writeObject(registrationCode);
6979
out.writeObject(lanId);
7080
out.writeObject(model);
7181
out.writeObject(communicationNet);
@@ -76,6 +86,7 @@ public void writeExternal(ObjectOutput out) throws IOException {
7686
@Override
7787
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
7888
thingId = (String)in.readObject();
89+
registrationCode = (String)in.readObject();
7990
lanId = (Integer)in.readObject();
8091
model = (String)in.readObject();
8192
communicationNet = (CommunicationNet)in.readObject();

client/edge/src/main/java/com/thefirstlineofcode/sand/client/edge/AbstractEdgeThing.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ public void register() {
460460
}
461461
registration.addConnectionListener(this);
462462

463-
registeredThing = registration.register(thingId, loadRegistrationKey());
463+
registeredThing = registration.register(thingId, loadRegistrationCode());
464464
if (registeredThing == null)
465465
return;
466466

@@ -485,8 +485,6 @@ public void register() {
485485
}
486486
}
487487

488-
protected abstract String loadRegistrationKey();
489-
490488
private String getRegisteredThingString(RegisteredThing registeredThing) {
491489
return String.format("%s,%s,%s", registeredThing.getThingName(),
492490
registeredThing.getCredentials(), BinaryUtils.encodeToBase64(registeredThing.getSecurityKey()));

client/ibtr/src/main/java/com/thefirstlineofcode/sand/client/ibtr/IRegistration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import com.thefirstlineofcode.sand.protocols.thing.RegisteredThing;
66

77
public interface IRegistration {
8-
RegisteredThing register(String thingId, String registrationKey) throws RegistrationException;
8+
RegisteredThing register(String thingId, String registrationCode) throws RegistrationException;
99
void remove();
1010
void addConnectionListener(IConnectionListener listener);
1111
void removeConnectionListener(IConnectionListener listener);

0 commit comments

Comments
 (0)