Skip to content

Commit d373913

Browse files
committed
Refactoring: Use thing ID and registration key to register.
1 parent 9604dfc commit d373913

File tree

53 files changed

+579
-424
lines changed

Some content is hidden

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

53 files changed

+579
-424
lines changed

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

Lines changed: 56 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.slf4j.Logger;
2525
import org.slf4j.LoggerFactory;
2626

27+
import com.thefirstlineofcode.basalt.oxm.binary.BinaryUtils;
2728
import com.thefirstlineofcode.basalt.xmpp.Constants;
2829
import com.thefirstlineofcode.basalt.xmpp.core.ProtocolException;
2930
import com.thefirstlineofcode.basalt.xmpp.core.stanza.error.FeatureNotImplemented;
@@ -40,18 +41,18 @@
4041
import com.thefirstlineofcode.sand.client.ibtr.RegistrationException;
4142
import com.thefirstlineofcode.sand.client.thing.AbstractThing;
4243
import com.thefirstlineofcode.sand.protocols.actuator.ExecutionException;
43-
import com.thefirstlineofcode.sand.protocols.thing.ThingIdentity;
44+
import com.thefirstlineofcode.sand.protocols.thing.RegisteredThing;
4445

4546
public abstract class AbstractEdgeThing extends AbstractThing implements IEdgeThing, IConnectionListener {
4647
private static final String ATTRIBUTE_NAME_STREAM_CONFIG = "stream_config";
47-
private static final String ATTRIBUTE_NAME_THING_IDENTITY = "thing_identity";
48+
private static final String ATTRIBUTE_NAME_REGISTERED_THING = "registered_thing";
4849
private static final String INTERNET_CONNECTIVITY_TEST_ADDRESS = "http://www.baidu.com";
4950
private static final String SAND_EDGE_CONFIG_DIR = ".com.thefirstlineofcode.sand.client.edge";
5051

5152
private static final Logger logger = LoggerFactory.getLogger(AbstractEdgeThing.class);
5253

5354
protected StandardStreamConfig streamConfig;
54-
protected ThingIdentity identity;
55+
protected RegisteredThing registeredThing;
5556

5657
protected StandardChatClient chatClient;
5758
protected Thread autoReconnectThread;
@@ -81,6 +82,7 @@ public AbstractEdgeThing(String model, StandardStreamConfig streamConfig, boolea
8182
super(model);
8283

8384
this.streamConfig = streamConfig;
85+
this.startConsole = startConsole;
8486

8587
powered = true;
8688
batteryPower = 100;
@@ -91,18 +93,17 @@ public AbstractEdgeThing(String model, StandardStreamConfig streamConfig, boolea
9193
started = false;
9294
stopToReconnect = true;
9395

94-
this.startConsole = startConsole;
96+
processAttributes(attributes);
9597
}
9698

9799
private String getStreamConfigString() {
98100
return String.format("%s,%s,%s", streamConfig.getHost(), streamConfig.getPort(), streamConfig.isTlsPreferred() ? "true" : "false");
99101
}
100-
102+
101103
protected StandardStreamConfig getStreamConfig(Map<String, String> attributes) {
102104
String sStreamConfig = attributes.get(ATTRIBUTE_NAME_STREAM_CONFIG);
103105
if (sStreamConfig == null) {
104-
logger.error("Can't read stream config. Null stream config string.");
105-
throw new IllegalArgumentException("Can't read stream config. Null stream config string.");
106+
return null;
106107
}
107108

108109
StringTokenizer st = new StringTokenizer(sStreamConfig, ",");
@@ -112,7 +113,7 @@ protected StandardStreamConfig getStreamConfig(Map<String, String> attributes) {
112113
}
113114

114115
StandardStreamConfig streamConfig = createStreamConfig(st);
115-
streamConfig.setResource(ThingIdentity.DEFAULT_RESOURCE_NAME);
116+
streamConfig.setResource(RegisteredThing.DEFAULT_RESOURCE_NAME);
116117

117118
return streamConfig;
118119
}
@@ -129,12 +130,15 @@ protected StandardStreamConfig createStreamConfig(StringTokenizer st) {
129130
public StandardStreamConfig getStreamConfig() {
130131
return streamConfig;
131132
}
133+
134+
@Override
135+
public void setStreamConfig(StandardStreamConfig streamConfig) {
136+
this.streamConfig = streamConfig;
137+
}
132138

133139
@Override
134140
public void start() {
135141
try {
136-
processAttributes(attributes);
137-
138142
doStart();
139143
} catch (Exception e) {
140144
logger.error("Some thing is wrong. The program can't run correctly.", e);
@@ -143,24 +147,21 @@ public void start() {
143147
}
144148
}
145149

146-
private void processAttributes(Map<String, String> attributes) {
147-
if (this.streamConfig == null) {
148-
this.streamConfig = getStreamConfig(attributes);
150+
protected void processAttributes(Map<String, String> attributes) {
151+
if (streamConfig == null) {
152+
streamConfig = getStreamConfig(attributes);
149153
} else {
150154
attributes.put(ATTRIBUTE_NAME_STREAM_CONFIG, getStreamConfigString());
151155
attributesChanged = true;
152156
}
153157

154-
identity = getThingIdentity(attributes);
158+
registeredThing = getRegisteredThing(attributes);
155159

156160
if (doProcessAttributes(attributes))
157161
attributesChanged = true;
158162

159163
if (attributesChanged)
160164
saveAttributes(attributes);
161-
162-
logger.info("I'm an edge thing[thing_id='{}', host='{}', port='{}', tls_preferred='{}'].",
163-
thingId, this.streamConfig.getHost(), this.streamConfig.getPort(), this.streamConfig.isTlsPreferred());
164165
}
165166

166167
protected void doStart() {
@@ -170,6 +171,12 @@ protected void doStart() {
170171
if (started)
171172
stop();
172173

174+
if (streamConfig == null)
175+
throw new IllegalStateException("Null stream config.");
176+
177+
logger.info("I'm an edge thing[thing_id='{}', host='{}', port='{}', tls_preferred='{}'].",
178+
thingId, this.streamConfig.getHost(), this.streamConfig.getPort(), this.streamConfig.isTlsPreferred());
179+
173180
if (!isHostLocalLanAddress()) {
174181
checkInternetConnectivity(10);
175182
}
@@ -307,8 +314,8 @@ public void connect() {
307314
logger.info("The thing tries to connect to server.");
308315

309316
try {
310-
chatClient.connect(new UsernamePasswordToken(identity.getThingName().toString(),
311-
identity.getCredentials()));
317+
chatClient.connect(new UsernamePasswordToken(registeredThing.getThingName().toString(),
318+
registeredThing.getCredentials()));
312319

313320
if (isConnected()) {
314321
for (IEdgeThingListener edgeThingListener : edgeThingListeners) {
@@ -352,7 +359,7 @@ protected StandardChatClient createChatClient() {
352359
protected StandardStreamConfig createStreamConfigWithResource() {
353360
StandardStreamConfig cloned = new StandardStreamConfig(streamConfig.getHost(), streamConfig.getPort());
354361
cloned.setTlsPreferred(streamConfig.isTlsPreferred());
355-
cloned.setResource(ThingIdentity.DEFAULT_RESOURCE_NAME);
362+
cloned.setResource(RegisteredThing.DEFAULT_RESOURCE_NAME);
356363

357364
return cloned;
358365
}
@@ -368,13 +375,13 @@ protected void startAutoReconnectThread() {
368375
autoReconnectThread.start();
369376
}
370377

371-
protected void registered(ThingIdentity identity) {
372-
attributes.put(ATTRIBUTE_NAME_THING_IDENTITY, getThingIdentityString(identity));
378+
protected void registered(RegisteredThing registeredThing) {
379+
attributes.put(ATTRIBUTE_NAME_REGISTERED_THING, getRegisteredThingString(registeredThing));
373380
saveAttributes(attributes);
374381

375-
this.identity = identity;
382+
this.registeredThing = registeredThing;
376383

377-
logger.info("The thing has registered. Thing name is '{}'.", identity.getThingName());
384+
logger.info("The thing has registered. Thing name is '{}'.", registeredThing.getThingName());
378385
}
379386

380387
@Override
@@ -431,7 +438,7 @@ protected void stopAutoReconnectThread() {
431438

432439
@Override
433440
public boolean isRegistered() {
434-
return identity != null;
441+
return registeredThing != null;
435442
}
436443

437444
@Override
@@ -449,13 +456,13 @@ public void register() {
449456
}
450457
registration.addConnectionListener(this);
451458

452-
identity = registration.register(thingId);
453-
if (identity == null)
459+
registeredThing = registration.register(thingId, loadRegistrationKey());
460+
if (registeredThing == null)
454461
return;
455462

456-
registered(identity);
463+
registered(registeredThing);
457464
for (IEdgeThingListener edgeThingListener : edgeThingListeners) {
458-
edgeThingListener.registered(identity);
465+
edgeThingListener.registered(registeredThing);
459466
}
460467
} catch (RegistrationException e) {
461468
for (IEdgeThingListener edgeThingListener : edgeThingListeners) {
@@ -474,8 +481,11 @@ public void register() {
474481
}
475482
}
476483

477-
private String getThingIdentityString(ThingIdentity identity) {
478-
return String.format("%s,%s", identity.getThingName(), identity.getCredentials());
484+
protected abstract String loadRegistrationKey();
485+
486+
private String getRegisteredThingString(RegisteredThing registeredThing) {
487+
return String.format("%s,%s,%s", registeredThing.getThingName(),
488+
registeredThing.getCredentials(), BinaryUtils.encodeToBase64(registeredThing.getSecurityKey()));
479489
}
480490

481491
@Override
@@ -591,21 +601,21 @@ protected void registrationExceptionOccurred(RegistrationException e) {
591601
logger.error("Registration exception occurred.", e);
592602
}
593603

594-
protected ThingIdentity getThingIdentity(Map<String, String> attributes) {
595-
String sThingIdentity = attributes.get(ATTRIBUTE_NAME_THING_IDENTITY);
596-
if (sThingIdentity == null)
604+
protected RegisteredThing getRegisteredThing(Map<String, String> attributes) {
605+
String sRegisteredThing = attributes.get(ATTRIBUTE_NAME_REGISTERED_THING);
606+
if (sRegisteredThing == null)
597607
return null;
598608

599-
int commaIndex = sThingIdentity.indexOf(',');
600-
if (commaIndex == -1) {
601-
throw new IllegalArgumentException("Cant read thing identity. Not a valid thing identity string.");
602-
}
609+
StringTokenizer st = new StringTokenizer(sRegisteredThing, ",");
610+
if (st.countTokens() != 3)
611+
throw new RuntimeException("Invalid registered thing string!");
603612

604-
ThingIdentity identity = new ThingIdentity();
605-
identity.setThingName(sThingIdentity.substring(0, commaIndex).trim());
606-
identity.setCredentials(sThingIdentity.substring(commaIndex + 1, sThingIdentity.length()).trim());
613+
RegisteredThing registeredThing = new RegisteredThing();
614+
registeredThing.setThingName(st.nextToken().trim());
615+
registeredThing.setCredentials(st.nextToken().trim());
616+
registeredThing.setCredentials(st.nextToken().trim());
607617

608-
return identity;
618+
return registeredThing;
609619
}
610620

611621
@Override
@@ -765,7 +775,10 @@ protected Path getAttributesFilePath() {
765775
return attributesFilePath;
766776
}
767777

768-
protected abstract boolean doProcessAttributes(Map<String, String> attributes);
778+
protected boolean doProcessAttributes(Map<String, String> attributes) {
779+
return false;
780+
}
781+
769782
protected abstract void registerIotPlugins();
770783
protected abstract void startIotComponents();
771784
protected abstract void stopIotComponents();

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package com.thefirstlineofcode.sand.client.edge;
22

33
import com.thefirstlineofcode.chalk.core.IChatClient;
4-
import com.thefirstlineofcode.chalk.core.stream.StreamConfig;
4+
import com.thefirstlineofcode.chalk.core.stream.StandardStreamConfig;
55
import com.thefirstlineofcode.chalk.network.IConnectionListener;
66
import com.thefirstlineofcode.sand.client.thing.IThing;
77

88
public interface IEdgeThing extends IThing {
9-
StreamConfig getStreamConfig();
9+
StandardStreamConfig getStreamConfig();
10+
void setStreamConfig(StandardStreamConfig streamConfig);
1011
boolean isRegistered();
1112
void register();
1213
boolean isConnected();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import com.thefirstlineofcode.chalk.core.IChatClient;
55
import com.thefirstlineofcode.chalk.network.ConnectionException;
66
import com.thefirstlineofcode.sand.client.ibtr.RegistrationException;
7-
import com.thefirstlineofcode.sand.protocols.thing.ThingIdentity;
7+
import com.thefirstlineofcode.sand.protocols.thing.RegisteredThing;
88

99
public interface IEdgeThingListener {
10-
void registered(ThingIdentity identity);
10+
void registered(RegisteredThing registeredThing);
1111
void registerExceptionOccurred(RegistrationException e);
1212
void failedToAuth(AuthFailureException e);
1313
void FailedToConnect(ConnectionException e);

client/friends/src/main/java/com/thefirstlineofcode/sand/client/friends/FollowService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import com.thefirstlineofcode.basalt.xmpp.core.stanza.Iq;
88
import com.thefirstlineofcode.chalk.core.IChatServices;
99
import com.thefirstlineofcode.chalk.core.stanza.IIqListener;
10-
import com.thefirstlineofcode.sand.protocols.thing.ThingIdentity;
10+
import com.thefirstlineofcode.sand.protocols.thing.RegisteredThing;
1111
import com.thefirstlineofcode.sand.protocols.thing.tacp.Notification;
1212

1313
public class FollowService implements IFollowService, IIqListener {
@@ -54,7 +54,7 @@ public void stop() {
5454
public void received(Iq iq) {
5555
JabberId to = iq.getTo();
5656
if (to != null && to.getResource() != null &&
57-
!to.getResource().equals(ThingIdentity.DEFAULT_RESOURCE_NAME))
57+
!to.getResource().equals(RegisteredThing.DEFAULT_RESOURCE_NAME))
5858
return;
5959

6060
Notification notification = iq.getObject();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import com.thefirstlineofcode.chalk.core.stream.INegotiationListener;
44
import com.thefirstlineofcode.chalk.network.IConnectionListener;
5-
import com.thefirstlineofcode.sand.protocols.thing.ThingIdentity;
5+
import com.thefirstlineofcode.sand.protocols.thing.RegisteredThing;
66

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

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
import com.thefirstlineofcode.chalk.network.ConnectionListenerAdapter;
2525
import com.thefirstlineofcode.chalk.network.IConnectionListener;
2626
import com.thefirstlineofcode.sand.protocols.ibtr.ThingRegister;
27-
import com.thefirstlineofcode.sand.protocols.thing.ThingIdentity;
27+
import com.thefirstlineofcode.sand.protocols.thing.RegisteredThing;
28+
import com.thefirstlineofcode.sand.protocols.thing.UnregisteredThing;
2829

2930
public class Registration extends ConnectionListenerAdapter implements IRegistration, INegotiationListener {
3031
private StandardStreamConfig streamConfig;
@@ -34,7 +35,7 @@ public class Registration extends ConnectionListenerAdapter implements IRegistra
3435
private boolean dontThrowConnectionException = false;
3536

3637
@Override
37-
public ThingIdentity register(String thingId) throws RegistrationException {
38+
public RegisteredThing register(String thingId, String registrationKey) throws RegistrationException {
3839
IChatClient chatClient = new IbtrChatClient(streamConfig);
3940
chatClient.register(InternalIbtrPlugin.class);
4041

@@ -53,7 +54,7 @@ public ThingIdentity register(String thingId) throws RegistrationException {
5354
}
5455

5556
try {
56-
return chatClient.getChatServices().getTaskService().execute(new RegisterTask(thingId));
57+
return chatClient.getChatServices().getTaskService().execute(new RegisterTask(thingId, registrationKey));
5758
} catch (ErrorException e) {
5859
IError error = e.getError();
5960
if (error.getDefinedCondition().equals(RemoteServerTimeout.DEFINED_CONDITION)) {
@@ -76,12 +77,12 @@ public ThingIdentity register(String thingId) throws RegistrationException {
7677
}
7778
}
7879

79-
private class RegisterTask implements ISyncTask<Iq, ThingIdentity> {
80+
private class RegisterTask implements ISyncTask<Iq, RegisteredThing> {
8081
private ThingRegister thingRegister;
8182

82-
public RegisterTask(String thingId) {
83+
public RegisterTask(String thingId, String registrationKey) {
8384
thingRegister = new ThingRegister();
84-
thingRegister.setRegister(thingId);
85+
thingRegister.setRegister(new UnregisteredThing(thingId, registrationKey));
8586
}
8687

8788
@Override
@@ -93,9 +94,9 @@ public void trigger(IUnidirectionalStream<Iq> stream) {
9394
}
9495

9596
@Override
96-
public ThingIdentity processResult(Iq iq) {
97+
public RegisteredThing processResult(Iq iq) {
9798
ThingRegister register = iq.getObject();
98-
return (ThingIdentity)register.getRegister();
99+
return (RegisteredThing)register.getRegister();
99100
}
100101
}
101102

client/remoting/src/main/java/com/thefirstlineofcode/sand/client/remoting/Remoting.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import com.thefirstlineofcode.chalk.core.ITask;
1616
import com.thefirstlineofcode.chalk.core.IUnidirectionalStream;
1717
import com.thefirstlineofcode.sand.protocols.actuator.Execution;
18-
import com.thefirstlineofcode.sand.protocols.thing.ThingIdentity;
18+
import com.thefirstlineofcode.sand.protocols.thing.RegisteredThing;
1919

2020
public class Remoting implements IRemoting {
2121
private static final int DEFAULT_TIMEOUT = 10 * 1000;
@@ -42,7 +42,7 @@ public void execute(JabberId target, Object action, int timeout, Callback callba
4242
Execution execution = new Execution(action);
4343

4444
if (target.getResource() != null &&
45-
!ThingIdentity.DEFAULT_RESOURCE_NAME.equals(target.getResource())) {
45+
!RegisteredThing.DEFAULT_RESOURCE_NAME.equals(target.getResource())) {
4646
execution.setLanTraceable(true);
4747
}
4848

0 commit comments

Comments
 (0)