2 * Copyright 2015 Samsung Electronics All Rights Reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package oic.simulator.clientcontroller.manager;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.Comparator;
22 import java.util.Date;
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.LinkedList;
26 import java.util.List;
29 import java.util.Vector;
31 import oic.simulator.clientcontroller.Activator;
32 import oic.simulator.clientcontroller.listener.IConfigurationUpload;
33 import oic.simulator.clientcontroller.listener.IDevicePlatformInfoUIListener;
34 import oic.simulator.clientcontroller.listener.IFindResourceUIListener;
35 import oic.simulator.clientcontroller.listener.IGetUIListener;
36 import oic.simulator.clientcontroller.listener.IObserveUIListener;
37 import oic.simulator.clientcontroller.listener.IPostUIListener;
38 import oic.simulator.clientcontroller.listener.IPutUIListener;
39 import oic.simulator.clientcontroller.listener.IResourceSelectionChangedUIListener;
40 import oic.simulator.clientcontroller.listener.IVerificationUIListener;
41 import oic.simulator.clientcontroller.remoteresource.DeviceAndPlatformInfo;
42 import oic.simulator.clientcontroller.remoteresource.MetaProperty;
43 import oic.simulator.clientcontroller.remoteresource.RemoteResource;
44 import oic.simulator.clientcontroller.utils.Constants;
45 import oic.simulator.clientcontroller.utils.Utility;
47 import org.oic.simulator.AttributeProperty;
48 import org.oic.simulator.AttributeProperty.Type;
49 import org.oic.simulator.AttributeValue;
50 import org.oic.simulator.AttributeValue.TypeInfo;
51 import org.oic.simulator.AttributeValue.ValueType;
52 import org.oic.simulator.DeviceInfo;
53 import org.oic.simulator.DeviceListener;
54 import org.oic.simulator.ILogger.Level;
55 import org.oic.simulator.PlatformInfo;
56 import org.oic.simulator.PlatformListener;
57 import org.oic.simulator.SimulatorException;
58 import org.oic.simulator.SimulatorManager;
59 import org.oic.simulator.SimulatorResourceAttribute;
60 import org.oic.simulator.SimulatorResourceModel;
61 import org.oic.simulator.SimulatorResult;
62 import org.oic.simulator.client.FindResourceListener;
63 import org.oic.simulator.client.SimulatorRemoteResource;
64 import org.oic.simulator.client.SimulatorRemoteResource.GetResponseListener;
65 import org.oic.simulator.client.SimulatorRemoteResource.ObserveNotificationListener;
66 import org.oic.simulator.client.SimulatorRemoteResource.PostResponseListener;
67 import org.oic.simulator.client.SimulatorRemoteResource.PutResponseListener;
68 import org.oic.simulator.client.SimulatorRemoteResource.VerificationListener;
69 import org.oic.simulator.client.SimulatorRemoteResource.VerificationType;
72 * This class acts as an interface between the simulator java SDK and the
73 * various UI modules. It maintains all the details of resources and provides
74 * other UI modules with the information required. It also handles responses for
75 * find, GET, PUT, POST, Observe and automatic verification operations from
76 * native layer and propagates those events to the registered UI listeners.
78 public class ResourceManager {
80 private Set<String> lastKnownSearchTypes;
82 private RemoteResource currentResourceInSelection;
84 private FindResourceListener findResourceListener;
85 private GetResponseListener getListener;
86 private PutResponseListener putListener;
87 private PostResponseListener postListener;
88 private ObserveNotificationListener observeListener;
89 private VerificationListener verifyListener;
90 private DeviceListener deviceListener;
91 private PlatformListener platformListener;
93 private ResponseSynchronizerThread synchronizerThread;
95 private Thread threadHandle;
97 private List<IFindResourceUIListener> findResourceUIListeners;
98 private List<IResourceSelectionChangedUIListener> resourceSelectionChangedUIListeners;
99 private List<IGetUIListener> getUIListeners;
100 private List<IPutUIListener> putUIListeners;
101 private List<IPostUIListener> postUIListeners;
102 private List<IObserveUIListener> observeUIListeners;
103 private List<IVerificationUIListener> verificationUIListeners;
104 private List<IConfigurationUpload> configUploadUIListeners;
105 private List<IDevicePlatformInfoUIListener> devicePlatformInfoUIListeners;
107 // Map with Server ID as key and the complete object as the value
108 private Map<String, RemoteResource> resourceMap;
109 private List<RemoteResource> favoriteResources;
110 // Maintaining a list of resource URIs for favorite resources feature.
111 private List<String> favoriteURIList;
113 // Maintaining a list of observed resource URIs.
114 private List<String> observedResourceURIList;
116 private Map<String, DeviceAndPlatformInfo> hostDeviceAndPlatformMap;
118 public ResourceManager() {
119 resourceMap = new HashMap<String, RemoteResource>();
120 favoriteResources = new ArrayList<RemoteResource>();
121 favoriteURIList = new ArrayList<String>();
122 observedResourceURIList = new ArrayList<String>();
123 hostDeviceAndPlatformMap = new HashMap<String, DeviceAndPlatformInfo>();
125 findResourceUIListeners = new ArrayList<IFindResourceUIListener>();
126 resourceSelectionChangedUIListeners = new ArrayList<IResourceSelectionChangedUIListener>();
127 getUIListeners = new ArrayList<IGetUIListener>();
128 putUIListeners = new ArrayList<IPutUIListener>();
129 postUIListeners = new ArrayList<IPostUIListener>();
130 observeUIListeners = new ArrayList<IObserveUIListener>();
131 verificationUIListeners = new ArrayList<IVerificationUIListener>();
132 configUploadUIListeners = new ArrayList<IConfigurationUpload>();
133 devicePlatformInfoUIListeners = new ArrayList<IDevicePlatformInfoUIListener>();
135 findResourceListener = new FindResourceListener() {
138 public void onResourceFound(final SimulatorRemoteResource resourceN) {
139 synchronizerThread.addToQueue(new Runnable() {
142 if (null == resourceN) {
146 // If id is not available, then it cannot be added to
147 // the local map as null value should not be allowed as
149 String uid = resourceN.getId();
154 // If resource already exist, then ignore it.
155 boolean exist = isUidExist(uid);
160 // Fetch the resource data
161 // RemoteResource resource =
162 // fetchResourceDetails(resourceN);
163 RemoteResource resource = new RemoteResource();
164 resource.setRemoteResourceRef(resourceN);
166 String uri = resourceN.getURI();
167 if (null != uri && uri.trim().length() > 0) {
168 // Add resource to favorite list if it was in
169 // favorites list during find/refresh operation.
170 if (favoriteURIList.contains(uri)) {
171 addResourcetoFavorites(resource);
173 // Add resource to observed resources list if it was
174 // in observe list during find/refresh operation.
175 if (observedResourceURIList.contains(uri)) {
176 sendObserveRequest(resource);
182 .log(Level.INFO.ordinal(), new Date(),
183 "Found a resource without URI. Ignoring it.");
187 // Add the resource in local data structure
188 addResourceDetails(resource);
190 // Notify the UI listener
191 newResourceFoundNotification(resource);
196 .log(Level.INFO.ordinal(),
198 "Resource Found [" + resourceN.getURI()
201 // Send an initial GET request to get the resource
204 resourceN.get(null, getListener);
205 } catch (SimulatorException e) {
209 .log(Level.ERROR.ordinal(),
211 Utility.getSimulatorErrorString(e,
215 // Get the device information
216 if (!isDeviceInfoExist(resourceN.getHost())) {
218 SimulatorManager.findDevices(resource
219 .getRemoteResourceRef().getHost(),
221 } catch (SimulatorException e) {
225 .log(Level.ERROR.ordinal(),
227 Utility.getSimulatorErrorString(
232 // Get the platform information
233 if (!isPlatformInfoExist(resourceN.getHost())) {
235 SimulatorManager.getPlatformInformation(
236 resource.getRemoteResourceRef()
237 .getHost(), platformListener);
238 } catch (SimulatorException e) {
242 .log(Level.ERROR.ordinal(),
244 Utility.getSimulatorErrorString(
253 // TODO: Listeners for device and platform information.
254 deviceListener = new DeviceListener() {
257 public void onDeviceFound(final String host,
258 final DeviceInfo deviceInfo) {
259 if (null == deviceInfo || null == host) {
262 synchronizerThread.addToQueue(new Runnable() {
265 synchronized (hostDeviceAndPlatformMap) {
266 DeviceAndPlatformInfo info = hostDeviceAndPlatformMap
269 info = new DeviceAndPlatformInfo();
271 hostDeviceAndPlatformMap.put(host, info);
273 info.setDeviceInfo(deviceInfo);
276 // Notify UI listeners
277 deviceInfoReceivedNotification();
283 platformListener = new PlatformListener() {
286 public void onPlatformFound(final String host,
287 final PlatformInfo platformInfo) {
288 if (null == platformInfo || null == host) {
291 synchronizerThread.addToQueue(new Runnable() {
294 synchronized (hostDeviceAndPlatformMap) {
295 DeviceAndPlatformInfo info = hostDeviceAndPlatformMap
298 info = new DeviceAndPlatformInfo();
300 hostDeviceAndPlatformMap.put(host, info);
302 info.setPlatformInfo(platformInfo);
305 // Notify UI listeners
306 platformInfoReceivedNotification();
312 getListener = new GetResponseListener() {
314 public void onGetResponse(final String uid,
315 final SimulatorResult result,
316 final SimulatorResourceModel resourceModelN) {
317 System.out.println(result);
318 if (result != SimulatorResult.SIMULATOR_OK) {
322 .log(Level.ERROR.ordinal(),
326 + "] Received error response for GET request.");
329 synchronizerThread.addToQueue(new Runnable() {
332 // Handling the response which includes retrieving the
333 // attributes and updating the local model.
334 RemoteResource resource = handleResponse(uid,
336 if (null != resource) {
337 // Notify the UI listeners
338 getCompleteNotification(resource);
345 putListener = new PutResponseListener() {
348 public void onPutResponse(final String uid,
349 final SimulatorResult result,
350 final SimulatorResourceModel resourceModelN) {
351 if (result != SimulatorResult.SIMULATOR_OK) {
355 .log(Level.ERROR.ordinal(),
359 + "] Received error response for PUT request.");
362 synchronizerThread.addToQueue(new Thread() {
365 // Handling the response which includes retrieving the
366 // attributes and updating the local model.
367 RemoteResource resource = handleResponse(uid,
369 if (null != resource) {
370 // Notify the UI listeners
371 putCompleteNotification(resource);
378 postListener = new PostResponseListener() {
380 public void onPostResponse(final String uid,
381 final SimulatorResult result,
382 final SimulatorResourceModel resourceModelN) {
383 if (result != SimulatorResult.SIMULATOR_OK) {
387 .log(Level.ERROR.ordinal(),
391 + "] Received error response for POST request.");
394 synchronizerThread.addToQueue(new Runnable() {
397 // Handling the response which includes retrieving the
398 // attributes and updating the local model.
399 RemoteResource resource = handleResponse(uid,
401 if (null != resource) {
402 // Notify the UI listeners
403 postCompleteNotification(resource);
410 observeListener = new ObserveNotificationListener() {
413 public void onObserveNotification(final String uid,
414 final SimulatorResourceModel resourceModelN, final int seq) {
415 synchronizerThread.addToQueue(new Runnable() {
418 // Handling the response which includes retrieving the
419 // attributes and updating the local model.
420 RemoteResource resource = handleResponse(uid,
422 if (null != resource) {
423 // Notify the UI listeners
424 observeCompleteNotification(resource);
431 verifyListener = new VerificationListener() {
434 public void onVerificationStarted(final String uid, final int autoId) {
435 synchronizerThread.addToQueue(new Runnable() {
438 RemoteResource resource = getResource(uid);
439 if (null == resource) {
442 // Update the automation status.
443 resource.updateAutomationStatus(autoId, true);
445 int autoType = resource.getAutomationtype(autoId);
447 // Notify the listeners.
448 verificationStartedNotification(resource, autoType);
454 public void onVerificationCompleted(final String uid,
456 synchronizerThread.addToQueue(new Runnable() {
459 RemoteResource resource = getResource(uid);
460 if (null == resource) {
463 // Update the automation status.
464 resource.updateAutomationStatus(autoId, false);
466 int autoType = resource.getAutomationtype(autoId);
468 // Notify the listeners.
469 verificationCompletedNotification(resource, autoType);
475 public void onVerificationAborted(final String uid, final int autoId) {
476 synchronizerThread.addToQueue(new Runnable() {
479 RemoteResource resource = getResource(uid);
480 if (null == resource) {
483 // Update the automation status.
484 resource.updateAutomationStatus(autoId, false);
486 int autoType = resource.getAutomationtype(autoId);
488 // Notify the listeners.
489 verificationAbortedNotification(resource, autoType);
495 synchronizerThread = new ResponseSynchronizerThread();
496 threadHandle = new Thread(synchronizerThread);
497 threadHandle.setName("Simulator Client Controller Event Queue");
498 threadHandle.start();
501 private RemoteResource handleResponse(String uid,
502 SimulatorResourceModel resourceModelN) {
503 if (null == uid || null == resourceModelN) {
507 // Update the local model
508 RemoteResource resource;
509 resource = getResource(uid);
510 if (null == resource) {
514 // if(!resource.isConfigUploaded() || null ==
515 // resource.getResourceModelRef())
516 SimulatorResourceModel resourceModel = resource.getResourceModelRef();
517 if (null == resourceModel) {
518 resource.setResourceModelRef(resourceModelN);
520 resourceModel.update(resourceModelN);
523 resource.setResourceRepresentation(resourceModelN, false);
528 public synchronized boolean isDeviceInfoExist(String host) {
529 DeviceAndPlatformInfo info = hostDeviceAndPlatformMap.get(host);
533 if (null == info.getDeviceInfo()) {
539 public synchronized boolean isPlatformInfoExist(String host) {
540 DeviceAndPlatformInfo info = hostDeviceAndPlatformMap.get(host);
544 if (null == info.getPlatformInfo()) {
550 private static class ResponseSynchronizerThread implements Runnable {
552 LinkedList<Runnable> responseQueue = new LinkedList<Runnable>();
556 while (!Thread.interrupted()) {
557 synchronized (this) {
559 while (responseQueue.isEmpty()) {
563 } catch (InterruptedException e) {
569 synchronized (this) {
570 thread = responseQueue.pop();
574 } catch (Exception e) {
575 if (e instanceof InterruptedException) {
583 public void addToQueue(Runnable event) {
584 synchronized (this) {
585 responseQueue.add(event);
591 public void addResourceSelectionChangedUIListener(
592 IResourceSelectionChangedUIListener resourceSelectionChangedUIListener) {
593 synchronized (resourceSelectionChangedUIListeners) {
594 resourceSelectionChangedUIListeners
595 .add(resourceSelectionChangedUIListener);
599 public void addGetUIListener(IGetUIListener getUIListener) {
600 synchronized (getUIListeners) {
601 getUIListeners.add(getUIListener);
605 public void addPutUIListener(IPutUIListener putUIListener) {
606 synchronized (putUIListeners) {
607 putUIListeners.add(putUIListener);
611 public void addPostUIListener(IPostUIListener postUIListener) {
612 synchronized (postUIListeners) {
613 postUIListeners.add(postUIListener);
617 public void addObserveUIListener(IObserveUIListener observeUIListener) {
618 synchronized (observeUIListeners) {
619 observeUIListeners.add(observeUIListener);
623 public void addVerificationUIListener(
624 IVerificationUIListener verificationUIListener) {
625 synchronized (verificationUIListeners) {
626 verificationUIListeners.add(verificationUIListener);
630 public void addConfigUploadUIListener(IConfigurationUpload configListener) {
631 synchronized (configUploadUIListeners) {
632 configUploadUIListeners.add(configListener);
636 public void addDevicePlatformInfoUIListener(
637 IDevicePlatformInfoUIListener deviceUIListener) {
638 synchronized (devicePlatformInfoUIListeners) {
639 devicePlatformInfoUIListeners.add(deviceUIListener);
643 public void removeDevicePlatformInfoUIListener(
644 IDevicePlatformInfoUIListener platformUIListener) {
645 synchronized (devicePlatformInfoUIListeners) {
646 devicePlatformInfoUIListeners.remove(platformUIListener);
650 public void removeResourceSelectionChangedUIListener(
651 IResourceSelectionChangedUIListener listener) {
652 synchronized (resourceSelectionChangedUIListeners) {
654 && resourceSelectionChangedUIListeners.size() > 0) {
655 resourceSelectionChangedUIListeners.remove(listener);
660 public void removeGetUIListener(IGetUIListener getUIListener) {
661 synchronized (getUIListeners) {
662 getUIListeners.remove(getUIListener);
666 public void removePutUIListener(IPutUIListener putUIListener) {
667 synchronized (putUIListeners) {
668 putUIListeners.remove(putUIListener);
672 public void removePostUIListener(IPostUIListener postUIListener) {
673 synchronized (postUIListeners) {
674 postUIListeners.remove(postUIListener);
678 public void removeObserveUIListener(IObserveUIListener observeUIListener) {
679 synchronized (observeUIListeners) {
680 observeUIListeners.remove(observeUIListener);
684 public void removeVerificationUIListener(
685 IVerificationUIListener verificationUIListener) {
686 synchronized (verificationUIListeners) {
687 verificationUIListeners.remove(verificationUIListener);
691 public void removeConfigUploadUIListener(IConfigurationUpload configListener) {
692 synchronized (configUploadUIListeners) {
693 configUploadUIListeners.remove(configListener);
697 public void addResourcetoFavorites(RemoteResource resource) {
698 if (null == resource) {
701 resource.setFavorite(true);
702 synchronized (favoriteResources) {
703 favoriteResources.add(resource);
707 public void removeResourceFromFavorites(RemoteResource resource) {
708 if (null == resource) {
711 resource.setFavorite(false);
712 synchronized (favoriteResources) {
713 favoriteResources.remove(resource);
717 public void addResourceURItoFavorites(RemoteResource resource) {
718 if (null == resource) {
721 synchronized (favoriteURIList) {
722 favoriteURIList.add(resource.getRemoteResourceRef().getURI());
726 public void removeResourceURIFromFavorites(RemoteResource resource) {
727 if (null == resource) {
730 synchronized (favoriteURIList) {
731 favoriteURIList.remove(resource.getRemoteResourceRef().getURI());
735 public void addObservedResourceURI(String resourceURI) {
736 synchronized (observedResourceURIList) {
737 if (!observedResourceURIList.contains(resourceURI))
738 observedResourceURIList.add(resourceURI);
742 public void removeObservedResourceURI(String resourceURI) {
743 synchronized (observedResourceURIList) {
744 observedResourceURIList.remove(resourceURI);
748 public boolean isResourceObserved(String resourceURI) {
749 boolean observed = false;
750 synchronized (observedResourceURIList) {
751 observed = observedResourceURIList.contains(resourceURI);
756 public synchronized RemoteResource getCurrentResourceInSelection() {
757 return currentResourceInSelection;
760 public synchronized void setCurrentResourceInSelection(
761 RemoteResource resource) {
762 this.currentResourceInSelection = resource;
765 private void addResourceDetails(RemoteResource remoteResource) {
766 if (null != remoteResource) {
767 synchronized (resourceMap) {
768 resourceMap.put(remoteResource.getRemoteResourceRef().getId(),
774 public void addFindresourceUIListener(IFindResourceUIListener listener) {
775 if (null == listener) {
778 synchronized (findResourceUIListeners) {
779 findResourceUIListeners.add(listener);
783 public void removeFindresourceUIListener(IFindResourceUIListener listener) {
784 if (null == listener) {
787 synchronized (findResourceUIListeners) {
788 findResourceUIListeners.remove(listener);
792 private boolean isUidExist(String uid) {
794 synchronized (resourceMap) {
795 exist = resourceMap.containsKey(uid);
800 private RemoteResource getResource(String uid) {
804 RemoteResource resource;
805 synchronized (resourceMap) {
806 resource = resourceMap.get(uid);
811 private void newResourceFoundNotification(RemoteResource resource) {
812 synchronized (findResourceUIListeners) {
813 if (findResourceUIListeners.size() > 0) {
814 IFindResourceUIListener listener;
815 Iterator<IFindResourceUIListener> listenerItr = findResourceUIListeners
817 while (listenerItr.hasNext()) {
818 listener = listenerItr.next();
819 if (null != listener) {
820 listener.onNewResourceFound(resource);
827 private void resourceSelectionChangedUINotification(RemoteResource resource) {
828 synchronized (resourceSelectionChangedUIListeners) {
829 if (resourceSelectionChangedUIListeners.size() > 0) {
830 IResourceSelectionChangedUIListener listener;
831 Iterator<IResourceSelectionChangedUIListener> listenerItr = resourceSelectionChangedUIListeners
833 while (listenerItr.hasNext()) {
834 listener = listenerItr.next();
835 if (null != listener) {
836 listener.onResourceSelectionChange(resource);
843 private void getCompleteNotification(RemoteResource resource) {
844 synchronized (getUIListeners) {
845 if (getUIListeners.size() > 0) {
846 IGetUIListener listener;
847 Iterator<IGetUIListener> listenerItr = getUIListeners
849 while (listenerItr.hasNext()) {
850 listener = listenerItr.next();
851 if (null != listener) {
852 listener.onGetCompleted(resource);
859 private void putCompleteNotification(RemoteResource resource) {
860 synchronized (putUIListeners) {
861 if (putUIListeners.size() > 0) {
862 IPutUIListener listener;
863 Iterator<IPutUIListener> listenerItr = putUIListeners
865 while (listenerItr.hasNext()) {
866 listener = listenerItr.next();
867 if (null != listener) {
868 listener.onPutCompleted(resource);
875 private void postCompleteNotification(RemoteResource resource) {
876 synchronized (postUIListeners) {
877 if (postUIListeners.size() > 0) {
878 IPostUIListener listener;
879 Iterator<IPostUIListener> listenerItr = postUIListeners
881 while (listenerItr.hasNext()) {
882 listener = listenerItr.next();
883 if (null != listener) {
884 listener.onPostCompleted(resource);
891 private void observeCompleteNotification(RemoteResource resource) {
892 synchronized (observeUIListeners) {
893 if (observeUIListeners.size() > 0) {
894 IObserveUIListener listener;
895 Iterator<IObserveUIListener> listenerItr = observeUIListeners
897 while (listenerItr.hasNext()) {
898 listener = listenerItr.next();
899 if (null != listener) {
900 listener.onObserveCompleted(resource);
907 private void verificationStartedNotification(RemoteResource resource,
909 synchronized (verificationUIListeners) {
910 if (verificationUIListeners.size() > 0) {
911 IVerificationUIListener listener;
912 Iterator<IVerificationUIListener> listenerItr = verificationUIListeners
914 while (listenerItr.hasNext()) {
915 listener = listenerItr.next();
916 if (null != listener) {
917 listener.onVerificationStarted(resource, autoType);
924 private void verificationAbortedNotification(RemoteResource resource,
926 synchronized (verificationUIListeners) {
927 if (verificationUIListeners.size() > 0) {
928 IVerificationUIListener listener;
929 Iterator<IVerificationUIListener> listenerItr = verificationUIListeners
931 while (listenerItr.hasNext()) {
932 listener = listenerItr.next();
933 if (null != listener) {
934 listener.onVerificationAborted(resource, autoType);
941 private void verificationCompletedNotification(RemoteResource resource,
943 synchronized (verificationUIListeners) {
944 if (verificationUIListeners.size() > 0) {
945 IVerificationUIListener listener;
946 Iterator<IVerificationUIListener> listenerItr = verificationUIListeners
948 while (listenerItr.hasNext()) {
949 listener = listenerItr.next();
950 if (null != listener) {
951 listener.onVerificationCompleted(resource, autoType);
958 private void configUploadedNotification(RemoteResource resource) {
959 synchronized (configUploadUIListeners) {
960 if (configUploadUIListeners.size() > 0) {
961 IConfigurationUpload listener;
962 Iterator<IConfigurationUpload> listenerItr = configUploadUIListeners
964 while (listenerItr.hasNext()) {
965 listener = listenerItr.next();
966 if (null != listener) {
967 listener.onConfigurationUploaded(resource);
974 private void deviceInfoReceivedNotification() {
975 synchronized (devicePlatformInfoUIListeners) {
976 if (devicePlatformInfoUIListeners.size() > 0) {
977 IDevicePlatformInfoUIListener listener;
978 Iterator<IDevicePlatformInfoUIListener> listenerItr = devicePlatformInfoUIListeners
980 while (listenerItr.hasNext()) {
981 listener = listenerItr.next();
982 if (null != listener) {
983 listener.onDeviceInfoFound();
990 private void platformInfoReceivedNotification() {
991 synchronized (devicePlatformInfoUIListeners) {
992 if (devicePlatformInfoUIListeners.size() > 0) {
993 IDevicePlatformInfoUIListener listener;
994 Iterator<IDevicePlatformInfoUIListener> listenerItr = devicePlatformInfoUIListeners
996 while (listenerItr.hasNext()) {
997 listener = listenerItr.next();
998 if (null != listener) {
999 listener.onPlatformInfoFound();
1006 public synchronized Set<String> getLastKnownSearchTypes() {
1007 return lastKnownSearchTypes;
1010 public synchronized void setLastKnownSearchTypes(
1011 Set<String> lastKnownSearchTypes) {
1012 this.lastKnownSearchTypes = lastKnownSearchTypes;
1015 public boolean findResourceRequest(Set<String> searchTypes) {
1016 boolean result = false;
1017 if (null == searchTypes || searchTypes.size() < 1) {
1019 SimulatorManager.findResource(findResourceListener);
1021 } catch (SimulatorException e) {
1025 .log(Level.ERROR.ordinal(), new Date(),
1026 Utility.getSimulatorErrorString(e, null));
1029 Iterator<String> searchItr = searchTypes.iterator();
1031 while (searchItr.hasNext()) {
1032 rType = searchItr.next();
1034 SimulatorManager.findResource(rType, findResourceListener);
1036 } catch (SimulatorException e) {
1040 .log(Level.ERROR.ordinal(), new Date(),
1041 Utility.getSimulatorErrorString(e, null));
1048 public void deleteResources(final Set<String> searchTypes) {
1049 synchronized (resourceMap) {
1050 if (null == resourceMap && resourceMap.isEmpty()) {
1056 if (null == searchTypes || searchTypes.size() < 1) {
1057 synchronized (resourceMap) {
1058 // Stop observing all the resources
1059 Iterator<String> itr = resourceMap.keySet().iterator();
1060 while (itr.hasNext()) {
1061 sendCancelObserveRequest(
1062 resourceMap.get(itr.next()), false);
1064 // Delete all cached details of resources
1065 resourceMap.clear();
1066 favoriteResources.clear();
1068 // Clearing the device and platform information
1069 hostDeviceAndPlatformMap.clear();
1071 // Change the current resource in selection
1072 setCurrentResourceInSelection(null);
1073 resourceSelectionChangedUINotification(null);
1075 Iterator<String> typeItr = searchTypes.iterator();
1077 while (typeItr.hasNext()) {
1078 resType = typeItr.next();
1079 deleteResourcesByType(resType);
1081 // Change the current resource in selection
1082 updateCurrentResourceInSelection(searchTypes);
1089 private void updateCurrentResourceInSelection(Set<String> searchTypes) {
1090 if (null == searchTypes || searchTypes.size() < 1) {
1093 RemoteResource resourceInSelection = getCurrentResourceInSelection();
1094 if (null == resourceInSelection) {
1097 List<String> typesOfSelection = resourceInSelection
1098 .getRemoteResourceRef().getResourceTypes();
1099 if (null == typesOfSelection || typesOfSelection.size() < 1) {
1102 Iterator<String> itr = typesOfSelection.iterator();
1104 while (itr.hasNext()) {
1106 if (searchTypes.contains(type)) {
1107 setCurrentResourceInSelection(null);
1108 resourceSelectionChangedUINotification(null);
1114 private void deleteResourcesByType(String resourceType) {
1115 if (null == resourceType) {
1118 synchronized (resourceMap) {
1119 Set<String> keySet = resourceMap.keySet();
1120 if (null == keySet) {
1123 Iterator<String> keyItr = keySet.iterator();
1125 RemoteResource resource;
1128 while (keyItr.hasNext()) {
1129 uId = keyItr.next();
1130 resource = resourceMap.get(uId);
1131 if (null == resource) {
1134 types = resource.getRemoteResourceRef().getResourceTypes();
1135 if (null != types) {
1136 exist = types.contains(resourceType);
1138 // Cancel observing the resource.
1139 sendCancelObserveRequest(resource, false);
1140 // Remove the resource from favorites list.
1141 removeResourceFromFavorites(resource);
1142 // Remove the resource
1144 // Remove the device and platform information
1145 synchronized (hostDeviceAndPlatformMap) {
1146 hostDeviceAndPlatformMap.remove(resource
1147 .getRemoteResourceRef().getHost());
1155 public void resourceSelectionChanged(final RemoteResource resource) {
1159 setCurrentResourceInSelection(resource);
1160 // Notify all observers for resource selection change event
1161 resourceSelectionChangedUINotification(resource);
1166 public List<MetaProperty> getDefaultProperties(RemoteResource resource) {
1167 if (null != resource) {
1171 List<MetaProperty> metaPropertyList = new ArrayList<MetaProperty>();
1173 for (int index = 0; index < Constants.META_PROPERTY_COUNT; index++) {
1174 propName = Constants.META_PROPERTIES[index];
1175 if (propName.equals(Constants.RESOURCE_URI)) {
1176 propValue = resource.getRemoteResourceRef().getURI();
1177 } else if (propName.equals(Constants.CONNECTIVITY_TYPE)) {
1178 propValue = resource.getRemoteResourceRef()
1179 .getConnectivityType().toString();
1180 } else if (propName.equals(Constants.ADDRESS)) {
1181 propValue = resource.getRemoteResourceRef().getHost();
1182 } else if (propName.equals(Constants.OBSERVABLE)) {
1183 propValue = Utility.getObservableInString(resource
1184 .getRemoteResourceRef().isObservable());
1185 } else if (propName.equals(Constants.RESOURCE_TYPES)) {
1186 Vector<String> types = resource.getRemoteResourceRef()
1187 .getResourceTypes();
1188 if (null != types) {
1189 propValue = types.toString();
1191 propValue = Constants.NOT_AVAILABLE;
1193 } else if (propName.equals(Constants.RESOURCE_INTERFACES)) {
1194 Vector<String> interfaces = resource.getRemoteResourceRef()
1195 .getResourceInterfaces();
1196 if (null != interfaces) {
1197 propValue = interfaces.toString();
1199 propValue = Constants.NOT_AVAILABLE;
1204 if (null != propValue) {
1205 metaPropertyList.add(new MetaProperty(propName, propValue));
1209 return metaPropertyList;
1214 public List<MetaProperty> getDeviceProperties() {
1215 if (null == currentResourceInSelection) {
1219 SimulatorRemoteResource remoteResource = currentResourceInSelection
1220 .getRemoteResourceRef();
1221 if (null == remoteResource) {
1225 String host = remoteResource.getHost();
1230 if (!isDeviceInfoExist(host)) {
1231 // Device Information
1233 SimulatorManager.findDevices(host, deviceListener);
1234 } catch (SimulatorException e) {
1238 .log(Level.ERROR.ordinal(), new Date(),
1239 Utility.getSimulatorErrorString(e, null));
1244 List<MetaProperty> metaProperties = new ArrayList<MetaProperty>();
1245 synchronized (hostDeviceAndPlatformMap) {
1246 DeviceInfo devInfo = hostDeviceAndPlatformMap.get(host)
1248 metaProperties.add(new MetaProperty(Constants.DEVICE_ID, devInfo
1250 metaProperties.add(new MetaProperty(Constants.DEVICE_NAME, devInfo
1252 metaProperties.add(new MetaProperty(Constants.DEVICE_SPEC_VERSION,
1253 devInfo.getSpecVersion()));
1254 metaProperties.add(new MetaProperty(Constants.DEVICE_DMV_VERSION,
1255 devInfo.getDataModelVersion()));
1259 * metaProperties.add(new MetaProperty(Constants.DEVICE_ID, ""));
1260 * metaProperties.add(new MetaProperty(Constants.DEVICE_NAME, ""));
1261 * metaProperties.add(new MetaProperty(Constants.DEVICE_SPEC_VERSION,
1262 * "")); metaProperties.add(new
1263 * MetaProperty(Constants.DEVICE_DMV_VERSION, ""));
1265 return metaProperties;
1268 public List<MetaProperty> getPlatformProperties() {
1269 if (null == currentResourceInSelection) {
1273 SimulatorRemoteResource remoteResource = currentResourceInSelection
1274 .getRemoteResourceRef();
1275 if (null == remoteResource) {
1279 String host = remoteResource.getHost();
1284 if (!isPlatformInfoExist(host)) {
1285 // Platform Information
1287 SimulatorManager.getPlatformInformation(host, platformListener);
1288 } catch (SimulatorException e) {
1292 .log(Level.ERROR.ordinal(), new Date(),
1293 Utility.getSimulatorErrorString(e, null));
1298 List<MetaProperty> metaProperties = new ArrayList<MetaProperty>();
1299 synchronized (hostDeviceAndPlatformMap) {
1300 PlatformInfo platInfo = hostDeviceAndPlatformMap.get(host)
1302 metaProperties.add(new MetaProperty(Constants.PLATFORM_ID, platInfo
1304 metaProperties.add(new MetaProperty(
1305 Constants.PLATFORM_MANUFAC_NAME, platInfo
1306 .getManufacturerName()));
1307 metaProperties.add(new MetaProperty(Constants.PLATFORM_MANUFAC_URL,
1308 platInfo.getManufacturerUrl()));
1309 metaProperties.add(new MetaProperty(Constants.PLATFORM_MODEL_NO,
1310 platInfo.getModelNumber()));
1311 metaProperties.add(new MetaProperty(
1312 Constants.PLATFORM_DATE_OF_MANUFAC, platInfo
1313 .getDateOfManufacture()));
1314 metaProperties.add(new MetaProperty(Constants.PLATFORM_VERSION,
1315 platInfo.getPlatformVersion()));
1316 metaProperties.add(new MetaProperty(Constants.PLATFORM_OS_VERSION,
1317 platInfo.getOperationSystemVersion()));
1318 metaProperties.add(new MetaProperty(
1319 Constants.PLATFORM_HARDWARE_VERSION, platInfo
1320 .getHardwareVersion()));
1321 metaProperties.add(new MetaProperty(
1322 Constants.PLATFORM_FIRMWARE_VERSION, platInfo
1323 .getFirmwareVersion()));
1324 metaProperties.add(new MetaProperty(Constants.PLATFORM_SUPPORT_URL,
1325 platInfo.getSupportUrl()));
1326 metaProperties.add(new MetaProperty(Constants.PLATFORM_SYSTEM_TIME,
1327 platInfo.getSystemTime()));
1330 * metaProperties.add(new MetaProperty(Constants.PLATFORM_ID, ""));
1331 * metaProperties .add(new MetaProperty(Constants.PLATFORM_MANUFAC_NAME,
1332 * "")); metaProperties .add(new
1333 * MetaProperty(Constants.PLATFORM_MANUFAC_URL, ""));
1334 * metaProperties.add(new MetaProperty(Constants.PLATFORM_MODEL_NO,
1335 * "")); metaProperties.add(new
1336 * MetaProperty(Constants.PLATFORM_DATE_OF_MANUFAC, ""));
1337 * metaProperties.add(new MetaProperty(Constants.PLATFORM_VERSION, ""));
1338 * metaProperties.add(new MetaProperty(Constants.PLATFORM_OS_VERSION,
1339 * "")); metaProperties.add(new MetaProperty(
1340 * Constants.PLATFORM_HARDWARE_VERSION, "")); metaProperties.add(new
1341 * MetaProperty( Constants.PLATFORM_FIRMWARE_VERSION, ""));
1342 * metaProperties .add(new MetaProperty(Constants.PLATFORM_SUPPORT_URL,
1343 * "")); metaProperties .add(new
1344 * MetaProperty(Constants.PLATFORM_SYSTEM_TIME, ""));
1346 return metaProperties;
1349 public Map<String, Boolean> getAutomationStatus(RemoteResource resource) {
1350 if (null == resource) {
1353 Map<String, Boolean> autoStatus = new HashMap<String, Boolean>();
1354 autoStatus.put(Constants.GET, resource.isGetAutomtnInProgress());
1355 autoStatus.put(Constants.PUT, resource.isPutAutomtnInProgress());
1356 autoStatus.put(Constants.POST, resource.isPostAutomtnInProgress());
1360 public List<RemoteResource> getResourceList() {
1361 List<RemoteResource> resourceList = new ArrayList<RemoteResource>();
1362 synchronized (resourceMap) {
1363 Set<String> idSet = resourceMap.keySet();
1364 Iterator<String> idItr = idSet.iterator();
1365 RemoteResource resource;
1366 while (idItr.hasNext()) {
1367 resource = resourceMap.get(idItr.next());
1368 if (null != resource) {
1369 resourceList.add(resource);
1374 Collections.sort(resourceList, new Comparator<RemoteResource>() {
1375 public int compare(RemoteResource res1, RemoteResource res2) {
1376 String s1 = res1.getRemoteResourceRef().getURI();
1377 String s2 = res2.getRemoteResourceRef().getURI();
1379 String s1Part = s1.replaceAll("\\d", "");
1380 String s2Part = s2.replaceAll("\\d", "");
1382 if (s1Part.equalsIgnoreCase(s2Part)) {
1383 return extractInt(s1) - extractInt(s2);
1385 return s1.compareTo(s2);
1388 int extractInt(String s) {
1389 String num = s.replaceAll("\\D", "");
1390 // return 0 if no digits found
1391 return num.isEmpty() ? 0 : Integer.parseInt(num);
1395 return resourceList;
1398 public List<RemoteResource> getFavResourceList() {
1399 List<RemoteResource> resourceList;
1400 synchronized (favoriteResources) {
1401 resourceList = new ArrayList<RemoteResource>(favoriteResources);
1403 return resourceList;
1406 public List<String> getAllValuesOfAttribute(SimulatorResourceAttribute att) {
1411 AttributeValue val = att.value();
1416 List<String> values = new ArrayList<String>();
1418 TypeInfo type = val.typeInfo();
1420 AttributeProperty prop = att.property();
1421 if (null == prop || prop.type().ordinal() == Type.UNKNOWN.ordinal()) {
1422 values.add(Utility.getAttributeValueAsString(val));
1426 Type valuesType = prop.type();
1428 if (type.mType != ValueType.RESOURCEMODEL) {
1429 if (type.mType == ValueType.ARRAY) {
1430 if (type.mDepth == 1) {
1431 AttributeProperty childProp = prop.getChildProperty();
1432 if (null != childProp) {
1433 valuesType = childProp.type();
1434 if (valuesType.ordinal() == Type.RANGE.ordinal()) {
1435 List<String> list = getRangeForPrimitiveNonArrayAttributes(
1436 childProp, type.mBaseType);
1438 values.addAll(list);
1440 } else if (valuesType.ordinal() == Type.VALUESET
1442 List<String> list = getAllowedValuesForPrimitiveNonArrayAttributes(
1443 childProp.valueSet(), type.mBaseType);
1445 values.addAll(list);
1451 if (valuesType.ordinal() == Type.RANGE.ordinal()) {
1452 List<String> list = getRangeForPrimitiveNonArrayAttributes(
1455 values.addAll(list);
1457 } else if (valuesType.ordinal() == Type.VALUESET.ordinal()) {
1458 List<String> list = getAllowedValuesForPrimitiveNonArrayAttributes(
1459 prop.valueSet(), type.mType);
1461 values.addAll(list);
1467 if (values.isEmpty()) {
1468 values.add(Utility.getAttributeValueAsString(val));
1474 public List<String> getRangeForPrimitiveNonArrayAttributes(
1475 AttributeProperty prop, ValueType type) {
1480 if (type == ValueType.ARRAY || type == ValueType.RESOURCEMODEL) {
1484 List<String> values = new ArrayList<String>();
1487 int min = (int) prop.min();
1488 int max = (int) prop.max();
1489 for (int iVal = min; iVal <= max; iVal++) {
1490 values.add(String.valueOf(iVal));
1494 double minD = (double) prop.min();
1495 double maxD = (double) prop.max();
1496 for (double iVal = minD; iVal <= maxD; iVal = iVal + 1.0) {
1497 values.add(String.valueOf(iVal));
1505 public List<String> getAllowedValuesForPrimitiveNonArrayAttributes(
1506 AttributeValue[] attValues, ValueType type) {
1507 if (null == attValues || attValues.length < 1) {
1511 if (type == ValueType.ARRAY || type == ValueType.RESOURCEMODEL) {
1516 List<String> values = new ArrayList<String>();
1517 for (AttributeValue val : attValues) {
1525 values.add(String.valueOf(obj));
1531 * public String getAttributeValue(RemoteResource res, String attName) { if
1532 * (null == res || null == attName) { return null; } return
1533 * res.getAttributeValue(attName); }
1535 public void sendGetRequest(RemoteResource resource) {
1536 if (null == resource) {
1539 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1540 if (null == resourceN) {
1544 resourceN.get(null, getListener);
1545 } catch (SimulatorException e) {
1549 .log(Level.ERROR.ordinal(), new Date(),
1550 Utility.getSimulatorErrorString(e, null));
1554 public void sendPutRequest(RemoteResource resource,
1555 SimulatorResourceModel model) {
1556 if (null == resource || null == model) {
1559 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1560 if (null == resourceN) {
1564 resourceN.put(null, model, putListener);
1565 } catch (Exception e) {
1567 addlInfo = "Invalid Attribute Value. Cannot send PUT request.";
1571 .log(Level.ERROR.ordinal(), new Date(),
1572 Utility.getSimulatorErrorString(e, addlInfo));
1576 public void sendPostRequest(RemoteResource resource,
1577 SimulatorResourceModel model) {
1578 if (null == resource || null == model) {
1581 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1582 if (null == resourceN) {
1586 resourceN.post(null, model, postListener);
1587 } catch (Exception e) {
1589 addlInfo = "Invalid Attribute Value. Cannot send POST request.";
1593 .log(Level.ERROR.ordinal(), new Date(),
1594 Utility.getSimulatorErrorString(e, addlInfo));
1598 public boolean sendObserveRequest(RemoteResource resource) {
1599 if (null == resource) {
1602 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1603 if (null == resourceN) {
1607 resourceN.startObserve(null, observeListener);
1608 resource.setObserved(true);
1609 // Add observed resource URI to show the proper status after every
1610 // find/refresh operations.
1611 addObservedResourceURI(resource.getRemoteResourceRef().getURI());
1612 } catch (SimulatorException e) {
1616 .log(Level.ERROR.ordinal(), new Date(),
1617 Utility.getSimulatorErrorString(e, null));
1623 public boolean sendCancelObserveRequest(RemoteResource resource,
1624 boolean removeEntry) {
1625 if (null == resource || !resource.isObserved()) {
1628 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1629 if (null == resourceN) {
1633 resourceN.stopObserve();
1634 resource.setObserved(false);
1635 // Remove observed resource URI to show the proper status after
1636 // every find/refresh operations.
1638 removeObservedResourceURI(resource.getRemoteResourceRef()
1640 } catch (SimulatorException e) {
1644 .log(Level.ERROR.ordinal(), new Date(),
1645 Utility.getSimulatorErrorString(e, null));
1651 public void startAutomationRequest(VerificationType reqType,
1652 RemoteResource resource) {
1653 if (null == resource) {
1656 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1657 if (null == resourceN) {
1660 if (null == reqType) {
1665 autoId = resourceN.startVerification(reqType, verifyListener);
1667 if (reqType == VerificationType.GET) {
1668 // resource.setGetAutomtnInProgress(true);
1669 resource.setGetAutomtnId(autoId);
1670 } else if (reqType == VerificationType.PUT) {
1671 // resource.setPutAutomtnInProgress(true);
1672 resource.setPutAutomtnId(autoId);
1673 } else {// if(reqType == Constants.POST_AUTOMATION_INDEX) {
1674 // resource.setPostAutomtnInProgress(true);
1675 resource.setPostAutomtnId(autoId);
1678 } catch (SimulatorException e) {
1682 .log(Level.ERROR.ordinal(), new Date(),
1683 Utility.getSimulatorErrorString(e, null));
1687 public void stopAutomationRequest(VerificationType reqType,
1688 RemoteResource resource) {
1689 if (null == resource) {
1692 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1693 if (null == resourceN) {
1697 if (reqType == VerificationType.GET) {
1698 resource.setGetAutomtnInProgress(false);
1699 autoId = resource.getGetAutomtnId();
1700 } else if (reqType == VerificationType.PUT) {
1701 resource.setPutAutomtnInProgress(false);
1702 autoId = resource.getPutAutomtnId();
1703 } else {// if(reqType == Constants.POST_AUTOMATION_INDEX) {
1704 resource.setPostAutomtnInProgress(false);
1705 autoId = resource.getPostAutomtnId();
1708 resourceN.stopVerification(autoId);
1709 } catch (SimulatorException e) {
1713 .log(Level.ERROR.ordinal(), new Date(),
1714 Utility.getSimulatorErrorString(e, null));
1718 public boolean setConfigFilePath(RemoteResource resource,
1719 String configFilePath) throws SimulatorException {
1720 if (null == resource) {
1723 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1724 if (null == resourceN) {
1728 SimulatorResourceModel configuredResourceModel;
1729 configuredResourceModel = resourceN.setConfigInfo(configFilePath);
1730 if (null == configuredResourceModel) {
1734 // Store the resource model in the local cache
1735 SimulatorResourceModel resourceModel = resource
1736 .getResourceModelRef();
1737 if (null != resourceModel) {
1738 configuredResourceModel.update(resourceModel);
1740 resource.setResourceModelRef(configuredResourceModel);
1741 } catch (SimulatorException e) {
1745 .log(Level.ERROR.ordinal(), new Date(),
1746 Utility.getSimulatorErrorString(e, null));
1749 // Update the status
1750 resource.setConfigUploaded(true);
1752 // Notify the UI listeners
1753 configUploadedNotification(resource);
1758 public void shutdown() {