Skip to content

Commit 77bacf1

Browse files
committed
Refactoring to remove some mess codes.
1 parent 5d4f99c commit 77bacf1

File tree

12 files changed

+111
-146
lines changed

12 files changed

+111
-146
lines changed

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

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import com.thefirstlineofcode.basalt.xmpp.core.IError;
2828
import com.thefirstlineofcode.basalt.xmpp.core.IqProtocolChain;
2929
import com.thefirstlineofcode.basalt.xmpp.core.JabberId;
30-
import com.thefirstlineofcode.basalt.xmpp.core.LangText;
3130
import com.thefirstlineofcode.basalt.xmpp.core.Protocol;
3231
import com.thefirstlineofcode.basalt.xmpp.core.ProtocolException;
3332
import com.thefirstlineofcode.basalt.xmpp.core.stanza.Iq;
@@ -42,7 +41,6 @@
4241
import com.thefirstlineofcode.basalt.xmpp.core.stanza.error.StanzaError;
4342
import com.thefirstlineofcode.basalt.xmpp.core.stanza.error.UndefinedCondition;
4443
import com.thefirstlineofcode.basalt.xmpp.core.stanza.error.UnexpectedRequest;
45-
import com.thefirstlineofcode.basalt.xmpp.core.stream.error.StreamError;
4644
import com.thefirstlineofcode.chalk.core.IChatServices;
4745
import com.thefirstlineofcode.chalk.core.ITask;
4846
import com.thefirstlineofcode.chalk.core.IUnidirectionalStream;
@@ -92,7 +90,6 @@ public class Concentrator extends Actuator implements IConcentrator {
9290
protected Map<CommunicationNet, ICommunicator<?, ? extends IAddress, byte[]>> communicators;
9391
protected Map<CommunicationNet, LanCommunicationListener<?, ?>> netToLanCommunicationListeners;
9492
protected Map<Integer, List<LanExecutionTraceInfo>> lanNodeToLanExecutionTraceInfos;
95-
protected Map<String, ILanExecutionErrorConverter> modelToLanExecutionErrorConverters;
9693
protected long defaultLanExecutionTimeout;
9794
protected int lanExecutionTimeoutCheckInterval;
9895
protected ExpiredLanExecutionsChecker expiredLanExecutionsChecker;
@@ -130,6 +127,8 @@ public class Concentrator extends Actuator implements IConcentrator {
130127
protected QoS defaultDataQoS;
131128
protected Map<Class<?>, QoS> dataTypeToQoSs;
132129

130+
private StandardErrorCodeMapping standardErrorCodeMapping;
131+
133132
public Concentrator(IChatServices chatServices) {
134133
super(chatServices);
135134

@@ -138,7 +137,6 @@ public Concentrator(IChatServices chatServices) {
138137
communicators = new HashMap<>();
139138
netToLanCommunicationListeners = new HashMap<>();
140139
lanNodeToLanExecutionTraceInfos = new HashMap<>();
141-
modelToLanExecutionErrorConverters = new HashMap<>();
142140
defaultLanExecutionTimeout = DEFAULT_VALUE_OF_DEFAULT_LAN_EXECUTION_TIMEOUT;
143141
lanExecutionTimeoutCheckInterval = DEFAULT_LAN_EXECUTION_TIMEOUT_CHECK_INTERVAL;
144142

@@ -170,6 +168,8 @@ public Concentrator(IChatServices chatServices) {
170168

171169
IReportService reportService = chatServices.createApi(IReportService.class);
172170
reporter = reportService.getReporter();
171+
172+
standardErrorCodeMapping = new StandardErrorCodeMapping();
173173
}
174174

175175
private class AckRequiredLanNotificationsLruPool<K, V> extends LinkedHashMap<K, V> {
@@ -276,14 +276,6 @@ protected void registerLanData(Class<?> dataType) {
276276
reporter.registerSupportedData(dataType);
277277
}
278278

279-
@Override
280-
public void registerLanExecutionErrorConverter(ILanExecutionErrorConverter lanExecutionErrorConverter) {
281-
modelToLanExecutionErrorConverters.put(lanExecutionErrorConverter.getModel(), lanExecutionErrorConverter);
282-
283-
if (logger.isInfoEnabled())
284-
logger.info("Execution error converter for model '{}' has registered.", lanExecutionErrorConverter.getModel());
285-
}
286-
287279
@SuppressWarnings("unchecked")
288280
protected <PA extends IAddress> void executeOnLanNode(Iq iq, Object action, boolean lanTraceable, Integer lanTimeout) {
289281
int lanId = Integer.parseInt(iq.getTo().getResource());
@@ -609,37 +601,11 @@ protected void processLanExecutionError(LanExecutionTraceInfo traceInfo, LanAnsw
609601
if (error.getErrorNumber() == null)
610602
throw new RuntimeException("Null error number.");
611603

612-
if (Math.abs(error.getErrorNumber()) > 99) {
613-
logger.error("Error number must be in range of -99~99. Mode of answer node: {}.", traceInfo.node.getModel());
614-
throw new RuntimeException(String.format("Error number must be in range of -99~99. Mode of answer node: {}.", traceInfo.node.getModel()));
615-
}
616-
617-
Integer errorNumber = error.getErrorNumber();
618-
ILanExecutionErrorConverter lanExecutionErrorConverter = modelToLanExecutionErrorConverters.get(traceInfo.node.getModel());
619-
if (lanExecutionErrorConverter != null) {
620-
IError e = lanExecutionErrorConverter.convertErrorNumberToError(errorNumber);
621-
622-
if (e instanceof StreamError) {
623-
chatServices.getStream().send(e);
624-
if (e.closeStream())
625-
chatServices.getStream().close();
626-
} else {
627-
StanzaError se = (StanzaError)e;
628-
629-
se.setId(traceInfo.sanzaId);
630-
setFromToAddresses(traceInfo.from, traceInfo.to, se);
631-
632-
chatServices.getStream().send(e);
633-
}
634-
} else {
635-
StanzaError e = new UndefinedCondition(StanzaError.Type.CANCEL);
636-
e.setId(traceInfo.sanzaId);
637-
setFromToAddresses(traceInfo.from, traceInfo.to, e);
638-
e.setText(new LangText(ThingsUtils.getExecutionErrorDescription(traceInfo.node.getModel(), errorNumber)));
639-
640-
chatServices.getStream().send((IError)e);
641-
}
604+
StanzaError e = errorCodeToError(traceInfo.node.getModel(), error.getErrorNumber());
605+
e.setId(traceInfo.sanzaId);
606+
setFromToAddresses(traceInfo.from, traceInfo.to, e);
642607

608+
chatServices.getStream().send((IError)e);
643609
}
644610

645611
protected void processLanExecutionResponse(JabberId from, JabberId to, String stanzaId) {
@@ -1585,4 +1551,13 @@ public void setAddNodeTimeout(long addNodeTimeout) {
15851551
public long getAddNodeTimeout() {
15861552
return addNodeTimeout;
15871553
}
1554+
1555+
protected StanzaError errorCodeToError(String modelName, int errorCode) {
1556+
StanzaError error = standardErrorCodeMapping.codeToError(errorCode);
1557+
if (error != null)
1558+
return error;
1559+
1560+
String errorDescription = ThingsUtils.getExecutionErrorDescription(modelName, errorCode);
1561+
return new UndefinedCondition(StanzaError.Type.CANCEL, errorDescription);
1562+
}
15881563
}

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

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

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public enum AddNodeError {
4949
void pullLanFollows(PullLanFollowsListener pullLanFollowsListener);
5050
String getThingName();
5151
void registerLanThingModel(IThingModelDescriptor modelDescriptor);
52-
void registerLanExecutionErrorConverter(ILanExecutionErrorConverter lanActionErrorConverter);
5352
void setDefaultLanExecutionTimeout(long timeout);
5453
long getDefaultLanExecutionTimeout();
5554
void setLanExecutionTimeoutCheckInterval(int interval);

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

Lines changed: 0 additions & 8 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.thefirstlineofcode.sand.client.concentrator;
2+
3+
import java.lang.reflect.Constructor;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
import com.thefirstlineofcode.basalt.xmpp.core.stanza.error.BadRequest;
8+
import com.thefirstlineofcode.basalt.xmpp.core.stanza.error.Conflict;
9+
import com.thefirstlineofcode.basalt.xmpp.core.stanza.error.FeatureNotImplemented;
10+
import com.thefirstlineofcode.basalt.xmpp.core.stanza.error.Forbidden;
11+
import com.thefirstlineofcode.basalt.xmpp.core.stanza.error.InternalServerError;
12+
import com.thefirstlineofcode.basalt.xmpp.core.stanza.error.ItemNotFound;
13+
import com.thefirstlineofcode.basalt.xmpp.core.stanza.error.NotAcceptable;
14+
import com.thefirstlineofcode.basalt.xmpp.core.stanza.error.NotAllowed;
15+
import com.thefirstlineofcode.basalt.xmpp.core.stanza.error.NotAuthorized;
16+
import com.thefirstlineofcode.basalt.xmpp.core.stanza.error.PaymentRequired;
17+
import com.thefirstlineofcode.basalt.xmpp.core.stanza.error.Redirect;
18+
import com.thefirstlineofcode.basalt.xmpp.core.stanza.error.RegistrationRequired;
19+
import com.thefirstlineofcode.basalt.xmpp.core.stanza.error.RemoteServerTimeout;
20+
import com.thefirstlineofcode.basalt.xmpp.core.stanza.error.ServiceUnavailable;
21+
import com.thefirstlineofcode.basalt.xmpp.core.stanza.error.StanzaError;
22+
23+
public class StandardErrorCodeMapping {
24+
public static final int ERROR_CODE_REDIRECT = 302;
25+
public static final int ERROR_CODE_BAD_REQUEST = 400;
26+
public static final int ERROR_CODE_NOT_AUTHORIZED = 401;
27+
public static final int ERROR_CODE_PAYMENT_REQUIRED = 402;
28+
public static final int ERROR_CODE_FORBIDDEN = 403;
29+
public static final int ERROR_CODE_ITEM_NOT_FOUND = 404;
30+
public static final int ERROR_CODE_NOT_ALLOWED = 405;
31+
public static final int ERROR_CODE_NOT_ACCEPTABLE = 406;
32+
public static final int ERROR_CODE_REGISTRATION_REQUIRED = 407;
33+
public static final int ERROR_CODE_CONFLICT = 409;
34+
public static final int ERROR_CODE_INTERNAL_SERVER_ERROR = 500;
35+
public static final int ERROR_CODE_FEATURE_NOT_IMPLEMENTED = 501;
36+
public static final int ERROR_CODE_SERVICE_UNAVAILABLE = 503;
37+
public static final int ERROR_CODE_REMOTE_SERVER_TIMEOUT = 504;
38+
39+
private static Map<Integer, Class<? extends StanzaError>> errorCodeToErrorClass = new HashMap<>();
40+
41+
static {
42+
errorCodeToErrorClass.put(ERROR_CODE_REDIRECT, Redirect.class);
43+
errorCodeToErrorClass.put(ERROR_CODE_BAD_REQUEST, BadRequest.class);
44+
errorCodeToErrorClass.put(ERROR_CODE_NOT_AUTHORIZED, NotAuthorized.class);
45+
errorCodeToErrorClass.put(ERROR_CODE_PAYMENT_REQUIRED, PaymentRequired.class);
46+
errorCodeToErrorClass.put(ERROR_CODE_FORBIDDEN, Forbidden.class);
47+
errorCodeToErrorClass.put(ERROR_CODE_ITEM_NOT_FOUND, ItemNotFound.class);
48+
errorCodeToErrorClass.put(ERROR_CODE_NOT_ALLOWED, NotAllowed.class);
49+
errorCodeToErrorClass.put(ERROR_CODE_NOT_ACCEPTABLE, NotAcceptable.class);
50+
errorCodeToErrorClass.put(ERROR_CODE_REGISTRATION_REQUIRED, RegistrationRequired.class);
51+
errorCodeToErrorClass.put(ERROR_CODE_CONFLICT, Conflict.class);
52+
errorCodeToErrorClass.put(ERROR_CODE_INTERNAL_SERVER_ERROR, InternalServerError.class);
53+
errorCodeToErrorClass.put(ERROR_CODE_FEATURE_NOT_IMPLEMENTED, FeatureNotImplemented.class);
54+
errorCodeToErrorClass.put(ERROR_CODE_SERVICE_UNAVAILABLE, ServiceUnavailable.class);
55+
errorCodeToErrorClass.put(ERROR_CODE_REMOTE_SERVER_TIMEOUT, RemoteServerTimeout.class);
56+
}
57+
58+
public StanzaError codeToError(int errorCode) {
59+
Class<? extends StanzaError> errorClass = errorCodeToErrorClass.get(errorCode);
60+
if (errorClass != null) {
61+
try {
62+
Constructor<? extends StanzaError> constructor = errorClass.getConstructor();
63+
return constructor.newInstance();
64+
} catch (Exception e) {
65+
throw new RuntimeException("Failed to instaniate the error.", e);
66+
}
67+
}
68+
69+
return null;
70+
}
71+
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
name=mud_arduino_avr
2-
version=1.0.0-ALPHA1
3-
author=Jelly Bean
4-
maintainer=Jelly Bean <jelly-b@thefirstlineofcode.com>
2+
version=1.0.0-BETA1
3+
author=Dongger
4+
maintainer=Dongger <48190591@qq.com>
55
sentence=Arduino AVR boards mud adaptation
66
paragraph=Mud adaptation library for Arduino AVR boards
77
category=Communication
8-
url=https://github.com/jelly-b/mud
8+
url=https://github.com/TheFirstLineOfCode/mud
99
architectures=*
1010
includes=mcu_board_adaptation.h

demo/mud_adaptations/mud_arduino_avr/src/mcu_board_adaptation.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ DacState getDacStateByByte(uint8_t iDacState) {
109109
return NONE;
110110
}
111111

112-
char *generateThingIdImpl() {
112+
char *generateThingIdUsingUniqueIdLibrary() {
113113
int modelNameLength = strlen(modelName);
114-
char *thingId = malloc(sizeof(char) * (modelNameLength + 8 + 1));
114+
char *thingId = malloc(sizeof(char) * (modelNameLength + 1 + 8 + 1));
115115
sprintf(thingId, "%s-%x%x%x%x%x%x%x%x", modelName,
116116
UniqueID8[0] / 16, UniqueID8[1] / 16, UniqueID8[2] / 16, UniqueID8[3] / 16,
117117
UniqueID8[4] / 16, UniqueID8[5] / 16, UniqueID8[6] / 16, UniqueID8[7] / 16);
@@ -186,7 +186,10 @@ void loadThingInfoImpl(ThingInfo *thingInfo) {
186186
readAddressFromEepRom(thingInfo->address, position);
187187
}
188188
}
189-
189+
190+
#ifdef ENABLE_DEBUG
191+
Serial.println(F("ThingInfo loaded."));
192+
#endif
190193
debugOutThingInfo(thingInfo);
191194
}
192195

@@ -229,6 +232,10 @@ void saveThingInfoImpl(ThingInfo *thingInfo) {
229232

230233
position = writeAddressToEepRom(thingInfo->address, position);
231234
}
235+
236+
#ifdef ENABLE_DEBUG
237+
Serial.println(F("ThingInfo saved."));
238+
#endif
232239

233240
debugOutThingInfo(thingInfo);
234241
}
@@ -249,7 +256,6 @@ void configureMcuBoard(const char *_modelName) {
249256
#endif
250257

251258
registerThingInfoLoader(loadThingInfoImpl);
252-
registerThingIdGenerator(generateThingIdImpl);
253259
registerThingInfoSaver(saveThingInfoImpl);
254260
registerResetter(resetImpl);
255261
registerTimer(getTimeImpl);

demo/mud_adaptations/mud_arduino_avr/src/mcu_board_adaptation.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@
2525
void configureMcuBoard(const char *modelName);
2626
void printToSerialPort(char title[], uint8_t message[], int size);
2727
void resetAll();
28+
char *generateThingIdUsingUniqueIdLibrary();
2829

2930
#endif
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
name=mud_as32_ttl_100
2-
version=1.0.0-ALPHA1
3-
author=Jelly Bean
4-
maintainer=Jelly Bean <jelly-b@thefirstlineofcode.com>
2+
version=1.0.0-BETA1
3+
author=Dongger
4+
maintainer=Dongger <48190591@qq.com>
55
sentence=AS32-TTL-100 mud adaptation
66
paragraph=Mud adaptation for AS32-TTL-100 LoRa module
77
category=Communication
8-
url=https://github.com/jelly-b/mud
8+
url=https://github.com/TheFirstLineOfCode/mud
99
architectures=*
1010
includes=radio_module_adaptation.h
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
name=mud_configuration
2-
version=1.0.0-ALPHA1
3-
author=Jelly Bean
4-
maintainer=Jelly Bean <jelly-b@thefirstlineofcode.com>
2+
version=1.0.0-BETA1
3+
author=Dongger
4+
maintainer=Dongger <48190591@qq.com>
55
sentence=Mud configuration
66
paragraph=Mud configuration for specified application
77
category=Communication
8-
url=https://github.com/jelly-b/mud
8+
url=https://github.com/TheFirstLineOfCode/mud
99
architectures=*
1010
includes=mud_configuration.h

0 commit comments

Comments
 (0)