3030package leshan .server .lwm2m .client ;
3131
3232import java .util .Collection ;
33+ import java .util .Date ;
3334import java .util .List ;
3435import java .util .Map ;
3536import java .util .concurrent .ConcurrentHashMap ;
@@ -49,7 +50,7 @@ public class ClientRegistryImpl implements ClientRegistry {
4950
5051 private static final Logger LOG = LoggerFactory .getLogger (ClientRegistryImpl .class );
5152
52- private ConcurrentHashMap <String /* end-point */ , Client > clientsByEp = new ConcurrentHashMap <>();
53+ private Map <String /* end-point */ , Client > clientsByEp = new ConcurrentHashMap <>();
5354
5455 private List <RegistryListener > listeners = new CopyOnWriteArrayList <>();
5556
@@ -93,67 +94,73 @@ public Client registerClient(Client client) {
9394 @ Override
9495 public Client updateClient (ClientUpdate clientUpdated ) {
9596 LOG .debug ("Updating registration for client: {}" , clientUpdated );
96- Validate .notNull (clientUpdated .getRegistrationId ());
97- for (Client client : clientsByEp .values ()) {
98- if (clientUpdated .getRegistrationId ().equals (client .getRegistrationId ())) {
99- // update client
100- if (clientUpdated .getAddress () != null ) {
101- client .setAddress (clientUpdated .getAddress ());
102- }
103-
104- if (clientUpdated .getPort () > 0 ) {
105- client .setPort (clientUpdated .getPort ());
106- }
97+ Client client = findByRegistrationId (clientUpdated .getRegistrationId ());
98+ if (client == null ) {
99+ return null ;
100+ } else {
101+ // update client
102+ if (clientUpdated .getAddress () != null ) {
103+ client .setAddress (clientUpdated .getAddress ());
104+ }
107105
108- if (clientUpdated .getLwM2mVersion () != null ) {
109- client .setLwM2mVersion (clientUpdated .getLwM2mVersion ());
110- }
106+ if (clientUpdated .getPort () > 0 ) {
107+ client .setPort (clientUpdated .getPort ());
108+ }
111109
112- if (clientUpdated .getBindingMode () != null ) {
113- client .setBindingMode (clientUpdated .getBindingMode ());
114- }
110+ if (clientUpdated .getObjectLinks () != null ) {
111+ client .setObjectLinks (clientUpdated .getObjectLinks ());
112+ }
113+
114+ client .setLifeTimeInSec (clientUpdated .getLifeTimeInSec ());
115+ client .setLwM2mVersion (clientUpdated .getLwM2mVersion ());
116+ client .setBindingMode (clientUpdated .getBindingMode ());
115117
116- if (clientUpdated .getSmsNumber () != null ) {
117- client .setSmsNumber (clientUpdated .getSmsNumber ());
118- }
119- return client ;
118+ if (clientUpdated .getSmsNumber () != null ) {
119+ client .setSmsNumber (clientUpdated .getSmsNumber ());
120120 }
121+
122+ client .setLastUpdate (new Date ());
123+ return client ;
121124 }
122- return null ;
123125 }
124126
125127 @ Override
126128 public Client deregisterClient (String registrationId ) {
127- LOG .debug ("Deregistering client with registrationId: {}" , registrationId );
128129 Validate .notNull (registrationId );
130+ LOG .debug ("Deregistering client with registrationId: {}" , registrationId );
129131
130- String endpoint = null ;
131-
132- for (Client client : clientsByEp .values ()) {
133- if (registrationId .equals (client .getRegistrationId ())) {
134- endpoint = client .getEndpoint ();
135- break ;
136- }
137- }
138-
139- Client unregistered = null ;
140-
141- if (endpoint != null ) {
142- unregistered = clientsByEp .remove (endpoint );
132+ Client toBeUnregistered = findByRegistrationId (registrationId );
133+ if (toBeUnregistered == null ) {
134+ return null ;
135+ } else {
136+ Client unregistered = clientsByEp .remove (toBeUnregistered .getEndpoint ());
143137 for (RegistryListener l : listeners ) {
144138 l .unregistered (unregistered );
145139 }
146- LOG .debug ("Unregistered client: {}" , unregistered );
147- }
148-
149- return unregistered ;
140+ LOG .debug ("Deregistered client: {}" , unregistered );
141+ return unregistered ;
142+ }
150143 }
151144
145+ private Client findByRegistrationId (String id ) {
146+ Client result = null ;
147+ if (id != null ) {
148+ for (Client client : clientsByEp .values ()) {
149+ if (id .equals (client .getRegistrationId ())) {
150+ result = client ;
151+ break ;
152+ }
153+ }
154+ }
155+ return result ;
156+ }
157+
152158 /**
153159 * start the registration manager, will start regular cleanup of dead registrations.
154160 */
155161 public void start () {
156162 // every 2 seconds clean the registration list
163+ // TODO re-consider clean-up interval: wouldn't 5 minutes do as well?
157164 schedExecutor .scheduleAtFixedRate (new Cleaner (), 2 , 2 , TimeUnit .SECONDS );
158165 }
159166
@@ -171,10 +178,10 @@ private class Cleaner implements Runnable {
171178
172179 @ Override
173180 public void run () {
174- for (Map . Entry < String , Client > e : clientsByEp .entrySet ()) {
175- if (!e . getValue () .isAlive ()) {
181+ for (Client client : clientsByEp .values ()) {
182+ if (!client .isAlive ()) {
176183 // force de-registration
177- deregisterClient (e . getValue () .getRegistrationId ());
184+ deregisterClient (client .getRegistrationId ());
178185 }
179186 }
180187 }
0 commit comments