From: spurthi.segu Date: Tue, 13 Oct 2015 15:20:27 +0000 (+0530) Subject: Fix for Update Frequency for both RECURRENT and NORMAL type of an resource is not... X-Git-Tag: 1.2.0+RC1~889 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=d6f3145c0bbc946908b48a08c33308ce5b47dcc5;p=platform%2Fupstream%2Fiotivity.git Fix for Update Frequency for both RECURRENT and NORMAL type of an resource is not working properly (IOT-801) Change-Id: If802f56c76e24469d5ddfa0fe588d18f2d0d5c7d Signed-off-by: spurthi.segu Reviewed-on: https://gerrit.iotivity.org/gerrit/3859 Tested-by: jenkins-iotivity Reviewed-by: Uze Choi --- diff --git a/service/simulator/examples/server/service_provider.cpp b/service/simulator/examples/server/service_provider.cpp index 28512a8..3cb3c13 100644 --- a/service/simulator/examples/server/service_provider.cpp +++ b/service/simulator/examples/server/service_provider.cpp @@ -26,7 +26,7 @@ class AppLogger : public ILogger void write(std::string time, ILogger::Level level, std::string message) { std::cout << "[APPLogger] " << time << " " << ILogger::getString(level) << " " - << message; + << message; } }; std::shared_ptr gAppLogger(new AppLogger()); @@ -34,7 +34,7 @@ std::shared_ptr gAppLogger(new AppLogger()); class SimLightResource { public: - void startTest(std::string &configPath) + void startTest() { printMenu(); bool cont = true; @@ -50,19 +50,9 @@ class SimLightResource switch (choice) { - case 1 : simulateResource(configPath); break; + case 1 : simulateResource(); break; case 2 : displayResource(); break; - case 3 : - try - { - deleteResource(); - } - catch (InvalidArgsException &e) - { - std::cout << "InvalidArgsException occured [code : " << e.code() << - " Detail: " << e.what() << "]" << std::endl; - } - break; + case 3 : deleteResource(); break; case 4 : updateAttributePower(); break; case 5 : updateAttributeIntensity(); break; case 6 : automateResourceUpdate(); break; @@ -124,17 +114,17 @@ class SimLightResource const SimulatorResourceModel &resModel) { std::cout << "[callback] Resource model is changed URI: " << uri.c_str() - << " Count : " << resModel.size() << std::endl; + << " Count : " << resModel.size() << std::endl; std::cout << "#### Modified attributes are ####" << std::endl; for (auto & attribute : resModel.getAttributes()) { std::cout << attribute.second.getName() << " : " - << attribute.second.valueToString().c_str() << std::endl; + << attribute.second.valueToString().c_str() << std::endl; } std::cout << "########################" << std::endl; } - void simulateResource(std::string &configPath) + void simulateResource() { SimulatorResourceServer::ResourceModelChangedCB callback = std::bind( &SimLightResource::onResourceModelChanged, this, std::placeholders::_1, @@ -142,21 +132,24 @@ class SimLightResource try { + std::string configPath; + std::cout << "Enter RAML path: "; + std::cin>>configPath; SimulatorResourceServerSP resource = - SimulatorManager::getInstance()->createResource(configPath, callback); + SimulatorManager::getInstance()->createResource(configPath, callback); m_resources.push_back(resource); std::cout << "Resource created successfully! URI= " << resource->getURI().c_str() - << std::endl; + << std::endl; } catch (InvalidArgsException &e) { std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: " - << e.what() << "]" << std::endl; + << e.what() << "]" << std::endl; } catch (SimulatorException &e) { std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " - << e.what() << "]" << std::endl; + << e.what() << "]" << std::endl; } } @@ -204,7 +197,7 @@ class SimLightResource { SimulatorManager::getInstance()->deleteResource(resourceType); std::cout << "Resources of type \"" << resourceType << "\"" << - " deleted successfully! " << std::endl; + " deleted successfully! " << std::endl; std::vector::iterator ite = m_resources.begin(); while (ite != m_resources.end()) { @@ -219,12 +212,12 @@ class SimLightResource catch (InvalidArgsException &e) { std::cout << "InvalidArgsException occured [code : " << e.code() - << " Detail: " << e.what() << "]" << std::endl; + << " Detail: " << e.what() << "]" << std::endl; } catch (SimulatorException &e) { std::cout << "SimulatorException occured [code : " << e.code() - << " Detail: " << e.what() << "]" << std::endl; + << " Detail: " << e.what() << "]" << std::endl; } } break; @@ -257,7 +250,7 @@ class SimLightResource } std::cout << "Setting the new values from allowed values list to power attribute" << - std::endl; + std::endl; // Update all possible values from allowed values for (int index = 0; index < allowedValuesSize; index++) { @@ -303,7 +296,7 @@ class SimLightResource } std::cout << "Setting the new values from allowed values list to intensity attribute" - << std::endl; + << std::endl; // Update all possible values from allowed values for (int index = min; index <= max; index++) { @@ -350,7 +343,7 @@ class SimLightResource // Attributes SimulatorResourceModel resModel = resource->getModel(); std::map attributes = - resModel.getAttributes(); + resModel.getAttributes(); std::cout << "##### Attributes [" << attributes.size() << "]" << std::endl; for (auto & attribute : attributes) { @@ -374,7 +367,7 @@ class SimLightResource const int id) { std::cout << "Update automation is completed [URI: " << uri.c_str() - << " AutomationID: " << id << "] ###" << std::endl; + << " AutomationID: " << id << "] ###" << std::endl; } void automateResourceUpdate() @@ -392,16 +385,16 @@ class SimLightResource try { - int id = m_resources[index - 1]->startUpdateAutomation(type, + int id = m_resources[index - 1]->startUpdateAutomation(type, 500, std::bind(&SimLightResource::onUpdateAutomationCompleted, this, - std::placeholders::_1, std::placeholders::_2)); + std::placeholders::_1, std::placeholders::_2)); std::cout << "startUpdateAutomation() returned succces : " << id << std::endl; } catch (SimulatorException &e) { std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " << - e.what() << "]" << std::endl; + e.what() << "]" << std::endl; } } @@ -414,7 +407,7 @@ class SimLightResource SimulatorResourceServerSP resource = m_resources[index - 1]; SimulatorResourceModel resModel = resource->getModel(); std::map attributes = - resModel.getAttributes(); + resModel.getAttributes(); int size = 0; for (auto & attribute : attributes) { @@ -429,7 +422,7 @@ class SimLightResource int choice = -1; std::cout << "Select the attribute which you want to automate for updation: " << - std::endl; + std::endl; std::cin >> choice; if (choice < 0 || choice > size) { @@ -457,20 +450,20 @@ class SimLightResource type = AutomationType::RECURRENT; std::cout << "Requesting attribute automation for " << attributeName.c_str() << - std::endl; + std::endl; try { - int id = resource->startUpdateAutomation(attributeName, type, + int id = resource->startUpdateAutomation(attributeName, type, 500, std::bind(&SimLightResource::onUpdateAutomationCompleted, this, - std::placeholders::_1, std::placeholders::_2)); + std::placeholders::_1, std::placeholders::_2)); std::cout << "startUpdateAutomation() returned succces : " << id << std::endl; } catch (SimulatorException &e) { std::cout << "SimulatorException occured [Error: " << e.code() << " Details: " << - e.what() << "]" << std::endl; + e.what() << "]" << std::endl; } } @@ -494,7 +487,7 @@ class SimLightResource if (!ids.size()) { std::cout << "No automation operation is going on this resource right now!" << - std::endl; + std::endl; return; } @@ -514,7 +507,7 @@ class SimLightResource std::ostringstream out; out << "ID: " << (int) observerInfo.id << std::endl; out << " [address: " << observerInfo.address << " port: " << observerInfo.port - << "]" << std::endl; + << "]" << std::endl; std::cout << out.str(); } @@ -537,7 +530,7 @@ class SimLightResource for (auto & observerInfo : observersList) { std::cout << " ID : " << (int) observerInfo.id << " [address: " << - observerInfo.address << " port: " << observerInfo.port << "]" << std::endl; + observerInfo.address << " port: " << observerInfo.port << "]" << std::endl; } std::cout << "########################" << std::endl; } @@ -553,12 +546,6 @@ void printMainMenu() std::cout << "2. Set Logger" << std::endl; std::cout << "3. Help" << std::endl; std::cout << "0. Exit" << std::endl; - std::cout << - "To set the Resource from RAML file, run the service provider with argument of Path of Raml File." - << std::endl; - std::cout << - "Example: ./simulator-server PATH-TO-RAML-FILE" - << std::endl; std::cout << "######################################" << std::endl; } @@ -597,17 +584,6 @@ void setLogger() int main(int argc, char *argv[]) { - std::string configPath = ""; - - if (argc == 2) - { - char *value = argv[1]; - configPath.append(value); - } - else - { - configPath = ""; - } SimLightResource lightResource; printMainMenu(); @@ -624,7 +600,7 @@ int main(int argc, char *argv[]) switch (choice) { - case 1: lightResource.startTest(configPath); + case 1: lightResource.startTest(); std::cout << "Welcome back to main menu !" << std::endl; break; case 2: setLogger(); break; diff --git a/service/simulator/inc/simulator_resource_model.h b/service/simulator/inc/simulator_resource_model.h index fcdfb6b..d596ed9 100644 --- a/service/simulator/inc/simulator_resource_model.h +++ b/service/simulator/inc/simulator_resource_model.h @@ -202,7 +202,7 @@ class SimulatorResourceModel bool compare(Attribute &attribute); - std::vector getAllowedValues(); + std::vector getAllowedValues() const; int getUpdateFrequencyTime() {return m_updateInterval;} void setUpdateFrequencyTime(int interval) {m_updateInterval = interval;} @@ -231,7 +231,7 @@ class SimulatorResourceModel ValueVariant &at(unsigned int index); int size() const; std::vector toString() const; - std::vector getValues(); + std::vector getValues() const; private: std::vector m_values; }; @@ -291,7 +291,7 @@ class SimulatorResourceModel * @param attr - Attribute pointer * */ - void addAttribute(const Attribute &attribute); + void addAttribute(const Attribute &attribute, bool overwrite = false); /** * API to set range of attribute value. diff --git a/service/simulator/inc/simulator_resource_server.h b/service/simulator/inc/simulator_resource_server.h index 715e8bb..b6f61a1 100644 --- a/service/simulator/inc/simulator_resource_server.h +++ b/service/simulator/inc/simulator_resource_server.h @@ -199,7 +199,7 @@ class SimulatorResourceServer * @return ID representing update automation session. * NOTE: API throws @InvalidArgsException, @SimulatorException exceptions. */ - virtual int startUpdateAutomation(AutomationType type, + virtual int startUpdateAutomation(AutomationType type, int updateInterval, updateCompleteCallback callback) = 0; /** @@ -209,6 +209,7 @@ class SimulatorResourceServer * * @param attrName - Name of the attribute to be automated. * @param type - Automation type. + * @param updateInterval -Interval time in milliseconds for attribute value update automation. * @param callback - Callback to get notifiy when update automation is finished. * @param id - Identifier for automation. * @@ -216,7 +217,7 @@ class SimulatorResourceServer * NOTE: API throws @InvalidArgsException, @SimulatorException exceptions. */ virtual int startUpdateAutomation(const std::string &attrName, AutomationType type, - updateCompleteCallback callback) = 0; + int updateInterval, updateCompleteCallback callback) = 0; /** * API to get the Ids of all ongoing resource update automation . diff --git a/service/simulator/java/eclipse-plugin/ClientControllerPlugin/META-INF/MANIFEST.MF b/service/simulator/java/eclipse-plugin/ClientControllerPlugin/META-INF/MANIFEST.MF index 4366174..6822dfb 100644 --- a/service/simulator/java/eclipse-plugin/ClientControllerPlugin/META-INF/MANIFEST.MF +++ b/service/simulator/java/eclipse-plugin/ClientControllerPlugin/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: ClientControllerPlugin Bundle-SymbolicName: ClientControllerPlugin;singleton:=true -Bundle-Version: 0.1.0 +Bundle-Version: 1.0.0.qualifier Bundle-Activator: oic.simulator.clientcontroller.Activator Require-Bundle: org.eclipse.ui, org.eclipse.core.runtime diff --git a/service/simulator/java/eclipse-plugin/ClientControllerPlugin/src/oic/simulator/clientcontroller/manager/ResourceManager.java b/service/simulator/java/eclipse-plugin/ClientControllerPlugin/src/oic/simulator/clientcontroller/manager/ResourceManager.java index 9b8bdb4..c7d225f 100644 --- a/service/simulator/java/eclipse-plugin/ClientControllerPlugin/src/oic/simulator/clientcontroller/manager/ResourceManager.java +++ b/service/simulator/java/eclipse-plugin/ClientControllerPlugin/src/oic/simulator/clientcontroller/manager/ResourceManager.java @@ -47,7 +47,6 @@ import oic.simulator.clientcontroller.utils.Utility; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.swt.graphics.Image; import org.oic.simulator.ILogger.Level; -import org.oic.simulator.InvalidArgsException; import org.oic.simulator.ResourceAttribute; import org.oic.simulator.ResourceAttribute.Range; import org.oic.simulator.ResourceAttribute.Type; @@ -398,7 +397,7 @@ public class ResourceManager { // TODO: For debugging if (null != attributeMap) { RemoteResourceAttribute.printAttributes(attributeMap); - System.out.println("Attributes found: "); + System.out.println("Attributes found: " + (null != attributeMap)); System.out.println("No of attributes: " + attributeMap.size()); resource.setResourceAttributesMap(attributeMap); @@ -582,7 +581,7 @@ public class ResourceManager { } public boolean isResourceObserved(String resourceURI) { - boolean observed; + boolean observed = false; synchronized (observedResourceURIList) { observed = observedResourceURIList.contains(resourceURI); } @@ -1483,16 +1482,6 @@ public class ResourceManager { } try { resourceN.stopVerification(autoId); - } catch (InvalidArgsException e) { - Activator - .getDefault() - .getLogManager() - .log(Level.ERROR.ordinal(), - new Date(), - "[" + e.getClass().getSimpleName() + "]" - + e.code().toString() + "-" - + e.message()); - return; } catch (SimulatorException e) { Activator .getDefault() diff --git a/service/simulator/java/eclipse-plugin/ClientControllerPlugin/src/oic/simulator/clientcontroller/view/dialogs/FindResourcePage.java b/service/simulator/java/eclipse-plugin/ClientControllerPlugin/src/oic/simulator/clientcontroller/view/dialogs/FindResourcePage.java index 9b0d603..6316674 100644 --- a/service/simulator/java/eclipse-plugin/ClientControllerPlugin/src/oic/simulator/clientcontroller/view/dialogs/FindResourcePage.java +++ b/service/simulator/java/eclipse-plugin/ClientControllerPlugin/src/oic/simulator/clientcontroller/view/dialogs/FindResourcePage.java @@ -143,7 +143,8 @@ public class FindResourcePage extends WizardPage { */ // TODO: Temporarily adding a resourceType for testing - // resourceTypeCmb.add("oic.r.light"); + // resourceTypeCmb.add("oic.r.light"); + // Currently for standard resources we are using resource type as sample.light. resourceTypeCmb.add("sample.light"); // By default, selecting the first item in the resourceType combo diff --git a/service/simulator/java/eclipse-plugin/ClientControllerPlugin/src/oic/simulator/clientcontroller/view/dialogs/PostRequestDialog.java b/service/simulator/java/eclipse-plugin/ClientControllerPlugin/src/oic/simulator/clientcontroller/view/dialogs/PostRequestDialog.java index 3b01cff..26c11b4 100644 --- a/service/simulator/java/eclipse-plugin/ClientControllerPlugin/src/oic/simulator/clientcontroller/view/dialogs/PostRequestDialog.java +++ b/service/simulator/java/eclipse-plugin/ClientControllerPlugin/src/oic/simulator/clientcontroller/view/dialogs/PostRequestDialog.java @@ -19,7 +19,6 @@ package oic.simulator.clientcontroller.view.dialogs; import java.util.List; import oic.simulator.clientcontroller.Activator; -import oic.simulator.clientcontroller.manager.ResourceManager; import oic.simulator.clientcontroller.remoteresource.PutPostAttributeModel; import oic.simulator.clientcontroller.utils.Constants; @@ -30,6 +29,7 @@ import org.eclipse.jface.viewers.CheckboxCellEditor; import org.eclipse.jface.viewers.ColumnLabelProvider; import org.eclipse.jface.viewers.EditingSupport; import org.eclipse.jface.viewers.IStructuredContentProvider; +import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.StyledCellLabelProvider; import org.eclipse.jface.viewers.TableViewer; import org.eclipse.jface.viewers.TableViewerColumn; @@ -37,6 +37,8 @@ import org.eclipse.jface.viewers.TextCellEditor; import org.eclipse.jface.viewers.Viewer; import org.eclipse.jface.viewers.ViewerCell; import org.eclipse.swt.SWT; +import org.eclipse.swt.events.ModifyEvent; +import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; @@ -45,6 +47,7 @@ import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Table; +import org.eclipse.swt.widgets.Text; /** * This dialog is used for generating a POST request. @@ -57,15 +60,12 @@ public class PostRequestDialog extends TitleAreaDialog { "Select" }; private final Integer[] attTblColWidth = { 200, 200, 50 }; - private ResourceManager resourceManager; - private List modelList = null; - + public PostRequestDialog(Shell parentShell, List modelList) { super(parentShell); this.modelList = modelList; - resourceManager = Activator.getDefault().getResourceManager(); } @Override @@ -204,11 +204,33 @@ public class PostRequestDialog extends TitleAreaDialog { class AttributeValueEditor extends EditingSupport { private final TableViewer viewer; private final CellEditor editor; - + private final Text txt; public AttributeValueEditor(TableViewer viewer) { super(viewer); this.viewer = viewer; editor = new TextCellEditor(viewer.getTable()); + txt = (Text)editor.getControl(); + if(null != txt) { + txt.addModifyListener(new ModifyListener() { + @Override + public void modifyText(ModifyEvent e) { + IStructuredSelection selection = (IStructuredSelection)AttributeValueEditor.this.viewer.getSelection(); + PutPostAttributeModel att = (PutPostAttributeModel)selection.getFirstElement(); + if(null == att) { + return; + } + String newValue = txt.getText(); + if(null != newValue && !newValue.isEmpty()) { + att.setModified(true); + } + else { + att.setModified(false); + } + AttributeValueEditor.this.viewer.update(att, null); + } + }); + + } } @Override @@ -234,14 +256,6 @@ public class PostRequestDialog extends TitleAreaDialog { // If there is a change, then its corresponding check box should be // checked. String newValue = String.valueOf(value); - String actualValue = resourceManager.getAttributeValue( - resourceManager.getCurrentResourceInSelection(), - model.getAttName()); - if (newValue.equals(actualValue)) { - model.setModified(false); - } else { - model.setModified(true); - } model.setAttValue(newValue); viewer.update(element, null); } diff --git a/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/manager/ImageManager.java b/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/manager/ImageManager.java index 940c1b3..ee700d1 100644 --- a/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/manager/ImageManager.java +++ b/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/manager/ImageManager.java @@ -56,7 +56,7 @@ public class ImageManager { .createFromURL(bundle.getEntry("icons/button_pressed.PNG"))); // Resource icons based on the resource type - r.put(Constants.OIC_R_LIGHT, ImageDescriptor.createFromURL(bundle + r.put(Constants.SAMPLE_LIGHT, ImageDescriptor.createFromURL(bundle .getEntry("/icons/light_16x16.png"))); // Log View related icons diff --git a/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/manager/ResourceManager.java b/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/manager/ResourceManager.java index c065bc2..e6cca32 100644 --- a/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/manager/ResourceManager.java +++ b/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/manager/ResourceManager.java @@ -482,7 +482,7 @@ public class ResourceManager { } public boolean isDisplayName(String displayName) { - boolean exist; + boolean exist = false; synchronized (displayNameMap) { exist = displayNameMap.containsKey(displayName); } @@ -490,7 +490,7 @@ public class ResourceManager { } public String getCompleteUriFromDisplayName(String displayName) { - String completeURI; + String completeURI = null; synchronized (displayNameMap) { completeURI = displayNameMap.get(displayName); } @@ -566,7 +566,7 @@ public class ResourceManager { @Override public void run() { Map resourceTypeMap; - SimulatorResourceServer[] simulatorResourceServers; + SimulatorResourceServer[] simulatorResourceServers = null; try { simulatorResourceServers = SimulatorManager.createResource( configFilePath, noOfInstances, @@ -1122,12 +1122,12 @@ public class ResourceManager { } public List getURIList() { - List list; + List list = null; synchronized (orderedResourceUriMap) { Set typeSet = orderedResourceUriMap.keySet(); List typeList = Utility.convertSetToList(typeSet); if (null == typeList || typeList.size() < 1) { - return null; + return list; } list = new ArrayList(); @@ -1349,7 +1349,7 @@ public class ResourceManager { String attrName = attribute.getAttributeName(); try { autoId = resourceServerN.startAttributeAutomation(attrName, - autoType, automationListener); + autoType, autoUpdateInterval, automationListener); } catch (SimulatorException e) { Activator .getDefault() @@ -1470,7 +1470,7 @@ public class ResourceManager { int autoId = -1; try { autoId = resourceServer.startResourceAutomation( - AutomationType.NORMAL, automationListener); + AutomationType.NORMAL, 500, automationListener); } catch (SimulatorException e) { Activator .getDefault() @@ -1643,4 +1643,4 @@ public class ResourceManager { public void shutdown() { threadHandle.interrupt(); } -} +} \ No newline at end of file diff --git a/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/utils/Constants.java b/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/utils/Constants.java index b444073..09fecfc 100644 --- a/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/utils/Constants.java +++ b/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/utils/Constants.java @@ -103,5 +103,5 @@ public class Constants { public static final String SPLIT_BY_DOT_PATTERN = "\\."; - public static final String OIC_R_LIGHT = "oic.r.light"; + public static final String SAMPLE_LIGHT = "sample.light"; } \ No newline at end of file diff --git a/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/view/dialogs/AutomationSettingDialog.java b/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/view/dialogs/AutomationSettingDialog.java index c2a6925..e2bb48f 100644 --- a/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/view/dialogs/AutomationSettingDialog.java +++ b/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/view/dialogs/AutomationSettingDialog.java @@ -41,6 +41,7 @@ import org.eclipse.swt.widgets.Shell; public class AutomationSettingDialog extends TitleAreaDialog { private CCombo autoTypeCmb; + private CCombo updateFreqCmb; private String automationType; private String updateFrequencyInMillis; @@ -80,6 +81,15 @@ public class AutomationSettingDialog extends TitleAreaDialog { gd.grabExcessHorizontalSpace = true; autoTypeCmb.setLayoutData(gd); + Label updateFreqLbl = new Label(container, SWT.NONE); + updateFreqLbl.setText("Update Frequency(ms)"); + + updateFreqCmb = new CCombo(container, SWT.READ_ONLY | SWT.BORDER); + gd = new GridData(); + gd.horizontalAlignment = SWT.FILL; + gd.grabExcessHorizontalSpace = true; + updateFreqCmb.setLayoutData(gd); + populateSettingsData(); addUIListeners(); @@ -109,6 +119,13 @@ public class AutomationSettingDialog extends TitleAreaDialog { } // Select the default value autoTypeCmb.select(autoTypeCmb.indexOf(value)); + } else if (settingId.equals(Constants.UPDATE_INTERVAL_IN_MS)) { + itr = allowedValues.iterator(); + while (itr.hasNext()) { + updateFreqCmb.add(itr.next()); + } + // Select the default value + updateFreqCmb.select(updateFreqCmb.indexOf(value)); } } } @@ -120,10 +137,18 @@ public class AutomationSettingDialog extends TitleAreaDialog { automationType = autoTypeCmb.getText(); } }); + + updateFreqCmb.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + updateFrequencyInMillis = updateFreqCmb.getText(); + } + }); } public void setInitialSettings() { automationType = autoTypeCmb.getText(); + updateFrequencyInMillis = updateFreqCmb.getText(); } public String getAutomationType() { diff --git a/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/view/dialogs/DeleteResourcePage.java b/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/view/dialogs/DeleteResourcePage.java index a62f0ae..be7dae8 100644 --- a/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/view/dialogs/DeleteResourcePage.java +++ b/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/view/dialogs/DeleteResourcePage.java @@ -207,12 +207,9 @@ public class DeleteResourcePage extends WizardPage { if (deleteCategory == DeleteCategory.ALL) { done = true; } else if (deleteCategory == DeleteCategory.BY_TYPE) { - int selectedItemIndex = resourceTypeCmb.getSelectionIndex(); - if (selectedItemIndex >= 0) { - deleteCandidate = resourceTypeCmb.getItem(selectedItemIndex); - if (null != deleteCandidate && deleteCandidate.length() > 0) { - done = true; - } + deleteCandidate = resourceTypeCmb.getText(); + if (null != deleteCandidate && deleteCandidate.length() > 0) { + done = true; } } else if (deleteCategory == DeleteCategory.BY_URI) { deleteCandidate = resourceUriTxt.getText(); diff --git a/service/simulator/java/jni/resource_attributes_jni.cpp b/service/simulator/java/jni/resource_attributes_jni.cpp index 0203afa..5732eff 100644 --- a/service/simulator/java/jni/resource_attributes_jni.cpp +++ b/service/simulator/java/jni/resource_attributes_jni.cpp @@ -54,7 +54,6 @@ class attribute_value_visitor : public boost::static_visitor return static_cast(str); } -#if 0 // Boolean type value conversion jobject operator ()(const bool &value) const { @@ -62,7 +61,7 @@ class attribute_value_visitor : public boost::static_visitor gSimulatorClassRefs.classBooleanCtor, value); return result; } - +#if 0 // SimulatorResourceModel::Attribute type value conversion jobject operator ()(const SimulatorResourceModel::Attribute &value) const { diff --git a/service/simulator/java/jni/simulator_common_jni.h b/service/simulator/java/jni/simulator_common_jni.h index 860874e..b0fd74b 100644 --- a/service/simulator/java/jni/simulator_common_jni.h +++ b/service/simulator/java/jni/simulator_common_jni.h @@ -28,6 +28,7 @@ typedef struct jclass classObject; jclass classInteger; jclass classDouble; + jclass classBoolean; jclass classString; jclass classHashMap; jclass classVector; @@ -52,6 +53,7 @@ typedef struct jmethodID classIntegerCtor; jmethodID classDoubleCtor; + jmethodID classBooleanCtor; jmethodID classHashMapCtor; jmethodID classHashMapPut; jmethodID classVectorCtor; diff --git a/service/simulator/java/jni/simulator_manager_jni.cpp b/service/simulator/java/jni/simulator_manager_jni.cpp index 6ae6e58..0fc5796 100644 --- a/service/simulator/java/jni/simulator_manager_jni.cpp +++ b/service/simulator/java/jni/simulator_manager_jni.cpp @@ -712,6 +712,11 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) return JNI_ERR; } + if (false == getClassRef(env, "java/lang/Boolean", gSimulatorClassRefs.classBoolean)) + { + return JNI_ERR; + } + if (false == getClassRef(env, "java/lang/String", gSimulatorClassRefs.classString)) { return JNI_ERR; @@ -829,6 +834,11 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) if (!gSimulatorClassRefs.classDoubleCtor) return JNI_ERR; + gSimulatorClassRefs.classBooleanCtor= env->GetMethodID(gSimulatorClassRefs.classBoolean, "", + "(Z)V"); + if (!gSimulatorClassRefs.classBooleanCtor) + return JNI_ERR; + gSimulatorClassRefs.classHashMapCtor = env->GetMethodID(gSimulatorClassRefs.classHashMap, "", "()V"); if (!gSimulatorClassRefs.classHashMapCtor) diff --git a/service/simulator/java/jni/simulator_resource_server_jni.cpp b/service/simulator/java/jni/simulator_resource_server_jni.cpp index 8a4a735..209ef47 100644 --- a/service/simulator/java/jni/simulator_resource_server_jni.cpp +++ b/service/simulator/java/jni/simulator_resource_server_jni.cpp @@ -488,7 +488,7 @@ Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_setAllowedValuesS JNIEXPORT jint JNICALL Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_startResourceAutomation -(JNIEnv *env, jobject object, jint automationType, jobject listener) +(JNIEnv *env, jobject object, jint automationType, jint updateInterval, jobject listener) { SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object); if (!resource) @@ -520,7 +520,7 @@ Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_startResourceAuto try { - automationId = resource->startUpdateAutomation(type, callback); + automationId = resource->startUpdateAutomation(type, updateInterval, callback); } catch (InvalidArgsException &e) { @@ -539,7 +539,7 @@ Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_startResourceAuto JNIEXPORT jint JNICALL Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_startAttributeAutomation -(JNIEnv *env, jobject object, jstring attrName, jint automationType, jobject listener) +(JNIEnv *env, jobject object, jstring attrName, jint automationType, jint updateInterval, jobject listener) { SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object); if (!resource) @@ -578,7 +578,7 @@ Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_startAttributeAut int automationId = -1; try { - automationId = resource->startUpdateAutomation(attrNamePtr, type, callback); + automationId = resource->startUpdateAutomation(attrNamePtr, type, updateInterval, callback); } catch (InvalidArgsException &e) { diff --git a/service/simulator/java/jni/simulator_resource_server_jni.h b/service/simulator/java/jni/simulator_resource_server_jni.h index 765d2b6..298071a 100644 --- a/service/simulator/java/jni/simulator_resource_server_jni.h +++ b/service/simulator/java/jni/simulator_resource_server_jni.h @@ -98,11 +98,11 @@ Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_setAllowedValuesS JNIEXPORT jint JNICALL Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_startResourceAutomation -(JNIEnv *, jobject, jint, jobject); +(JNIEnv *, jobject, jint, jint, jobject); JNIEXPORT jint JNICALL Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_startAttributeAutomation -(JNIEnv *, jobject, jstring, jint, jobject); +(JNIEnv *, jobject, jstring, jint, jint, jobject); JNIEXPORT void JNICALL Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_stopAutomation diff --git a/service/simulator/java/sdk/src/org/oic/simulator/SimulatorResourceModel.java b/service/simulator/java/sdk/src/org/oic/simulator/SimulatorResourceModel.java index 691fd5d..63961e9 100644 --- a/service/simulator/java/sdk/src/org/oic/simulator/SimulatorResourceModel.java +++ b/service/simulator/java/sdk/src/org/oic/simulator/SimulatorResourceModel.java @@ -157,8 +157,7 @@ public class SimulatorResourceModel { protected void finalize() throws Throwable { try { dispose(); - } - finally { + } finally { super.finalize(); } } diff --git a/service/simulator/java/sdk/src/org/oic/simulator/clientcontroller/SimulatorRemoteResource.java b/service/simulator/java/sdk/src/org/oic/simulator/clientcontroller/SimulatorRemoteResource.java index a8aa3a3..1697681 100644 --- a/service/simulator/java/sdk/src/org/oic/simulator/clientcontroller/SimulatorRemoteResource.java +++ b/service/simulator/java/sdk/src/org/oic/simulator/clientcontroller/SimulatorRemoteResource.java @@ -417,8 +417,7 @@ public class SimulatorRemoteResource { protected void finalize() throws Throwable { try { dispose(); - } - finally { + } finally { super.finalize(); } } diff --git a/service/simulator/java/sdk/src/org/oic/simulator/serviceprovider/SimulatorResourceServer.java b/service/simulator/java/sdk/src/org/oic/simulator/serviceprovider/SimulatorResourceServer.java index acc6cd1..d601b53 100644 --- a/service/simulator/java/sdk/src/org/oic/simulator/serviceprovider/SimulatorResourceServer.java +++ b/service/simulator/java/sdk/src/org/oic/simulator/serviceprovider/SimulatorResourceServer.java @@ -334,6 +334,8 @@ public class SimulatorResourceServer { * @param typeOfAutomation * {@link AutomationType} indicating whether the automation is * one-time or recursive. + * @param updateInterval + * Interval time in milliseconds for attribute value update automation. * @param listener * Listener to be notified when automation ends. * @@ -346,9 +348,9 @@ public class SimulatorResourceServer { * This exception will be thrown for other errors. */ public int startResourceAutomation(AutomationType typeOfAutomation, - IAutomation listener) throws InvalidArgsException, + int updateInterval, IAutomation listener) throws InvalidArgsException, SimulatorException { - return startResourceAutomation(typeOfAutomation.getValue(), listener); + return startResourceAutomation(typeOfAutomation.getValue(), updateInterval, listener); } /** @@ -361,6 +363,8 @@ public class SimulatorResourceServer { * @param typeOfAutomation * {@link AutomationType} indicating whether the automation is * one-time or recursive. + * @param updateInterval + * Interval time in milliseconds for attribute value update automation. * @param listener * Listener to be notified when automation ends. * @@ -373,10 +377,11 @@ public class SimulatorResourceServer { * This exception will be thrown for other errors. */ public int startAttributeAutomation(String attrName, - AutomationType typeOfAutomation, IAutomation listener) + AutomationType typeOfAutomation, int updateInterval, + IAutomation listener) throws InvalidArgsException, SimulatorException { return startAttributeAutomation(attrName, typeOfAutomation.getValue(), - listener); + updateInterval, listener); } /** @@ -463,19 +468,18 @@ public class SimulatorResourceServer { SimulatorException; private native int startResourceAutomation(int typeOfAutomation, - IAutomation listener) throws InvalidArgsException, + int updateInterval, IAutomation listener) throws InvalidArgsException, SimulatorException; private native int startAttributeAutomation(String attrName, - int typeOfAutomation, IAutomation listener) + int typeOfAutomation, int updateInterval, IAutomation listener) throws InvalidArgsException, SimulatorException; @Override protected void finalize() throws Throwable { try { dispose(); - } - finally { + } finally { super.finalize(); } } diff --git a/service/simulator/ramlparser/raml/jsonSchemaParser/Definitions.h b/service/simulator/ramlparser/raml/jsonSchemaParser/Definitions.h index d2eec8f..401e448 100755 --- a/service/simulator/ramlparser/raml/jsonSchemaParser/Definitions.h +++ b/service/simulator/ramlparser/raml/jsonSchemaParser/Definitions.h @@ -118,7 +118,7 @@ namespace RAML if (*it == reqValue) break; } - if (m_required.end() == it) + if (m_required.end() != it) { m_required.push_back(reqValue); } diff --git a/service/simulator/ramlparser/raml/jsonSchemaParser/Items.h b/service/simulator/ramlparser/raml/jsonSchemaParser/Items.h index d2463b2..b0a0e50 100755 --- a/service/simulator/ramlparser/raml/jsonSchemaParser/Items.h +++ b/service/simulator/ramlparser/raml/jsonSchemaParser/Items.h @@ -124,7 +124,7 @@ namespace RAML if (*it == reqValue) break; } - if (m_required.end() == it) + if (m_required.end() != it) { m_required.push_back(reqValue); } diff --git a/service/simulator/src/client-controller/attribute_generator.cpp b/service/simulator/src/client-controller/attribute_generator.cpp index 5943264..03abd64 100644 --- a/service/simulator/src/client-controller/attribute_generator.cpp +++ b/service/simulator/src/client-controller/attribute_generator.cpp @@ -20,13 +20,12 @@ #include "attribute_generator.h" -AttributeGenerator::AttributeGenerator(SimulatorResourceModel::Attribute &attribute) +AttributeGenerator::AttributeGenerator(const SimulatorResourceModel::Attribute &attribute) : m_name(attribute.getName()), m_min(INT_MIN), m_max(INT_MAX), m_rangeIndex(-1), - m_nextAllowedValueIndex(0), - m_prevAllowedValueIndex(0), + m_allowedValueIndex(0), m_hasRange(false), m_hasAllowedValue(false) { @@ -47,7 +46,6 @@ AttributeGenerator::AttributeGenerator(SimulatorResourceModel::Attribute &attrib { m_hasAllowedValue = true; } - m_prevAllowedValueIndex = m_allowedValues.size(); } } @@ -58,7 +56,7 @@ bool AttributeGenerator::hasNext() return true; } - if (m_hasAllowedValue && m_nextAllowedValueIndex < m_allowedValues.size()) + if (m_hasAllowedValue && m_allowedValueIndex < m_allowedValues.size()) { return true; } @@ -77,28 +75,113 @@ bool AttributeGenerator::next(SimulatorResourceModel::Attribute &attribute) } else if (m_hasAllowedValue) { - attribute.setValue(m_allowedValues[m_nextAllowedValueIndex++]); + attribute.setValue(m_allowedValues[m_allowedValueIndex++]); return true; } return false; } -bool AttributeGenerator::previous(SimulatorResourceModel::Attribute &attribute) +SimulatorResourceModel::Attribute AttributeGenerator::current() { + SimulatorResourceModel::Attribute attribute; + attribute.setName(m_name); + if (m_hasRange) + { + attribute.setValue(m_rangeIndex); + } + else if (m_hasAllowedValue) + { + attribute.setValue(m_allowedValues[m_allowedValueIndex]); + } + + return attribute; +} +void AttributeGenerator::reset() +{ if (m_hasRange) { - attribute.setValue(m_rangeIndex - 1); - return true; + m_rangeIndex = m_min; } else if (m_hasAllowedValue) { - attribute.setValue(m_allowedValues[m_prevAllowedValueIndex - 1]); + m_allowedValueIndex = 0; + } +} + +AttributeCombinationGen::AttributeCombinationGen( + const std::vector &attributes) +{ + for (auto &attr : attributes) + { + AttributeGenerator attrGen(attr); + m_attrGenList.push_back(attr); + } + + m_index = -1; +} + +bool AttributeCombinationGen::next(SimulatorResourceModel &resModel) +{ + if (!m_attrGenList.size()) + { + return false; + } + + std::lock_guard lock(m_lock); + + // This block will execute for only first time + if (-1 == m_index) + { + for (int index = 0; index < m_attrGenList.size(); index++) + { + // Add the attribute on resource model + addAttributeToModel(index); + } + + m_index = m_attrGenList.size() - 1; + resModel = m_resModel; + return true; + } + + // Get the next attribute from statck top element + if (m_attrGenList[m_index].hasNext()) + { + addAttributeToModel(m_index); + resModel = m_resModel; + return true; + } + else + { + for (int index = m_index; index >= 0; index--) + { + if (!m_attrGenList[index].hasNext()) + { + if (!index) + return false; + + m_attrGenList[index].reset(); + addAttributeToModel(index); + } + else + { + addAttributeToModel(index); + break; + } + } + + resModel = m_resModel; return true; } return false; } +void AttributeCombinationGen::addAttributeToModel(int index) +{ + SimulatorResourceModel::Attribute attribute; + m_attrGenList[index].next(attribute); + m_resModel.addAttribute(attribute, true); +} diff --git a/service/simulator/src/client-controller/attribute_generator.h b/service/simulator/src/client-controller/attribute_generator.h index 3f66372..fab888f 100644 --- a/service/simulator/src/client-controller/attribute_generator.h +++ b/service/simulator/src/client-controller/attribute_generator.h @@ -28,24 +28,37 @@ class AttributeGenerator { public: - AttributeGenerator(SimulatorResourceModel::Attribute &attribute); + AttributeGenerator(const SimulatorResourceModel::Attribute &attribute); bool hasNext(); bool next(SimulatorResourceModel::Attribute &attribute); - bool previous(SimulatorResourceModel::Attribute &attribute); + SimulatorResourceModel::Attribute current(); void reset(); private: std::string m_name; - SimulatorResourceModel::Attribute::ValueType m_type; int m_min; int m_max; int m_rangeIndex; - int m_nextAllowedValueIndex; - int m_prevAllowedValueIndex; + int m_allowedValueIndex; bool m_hasRange; bool m_hasAllowedValue; std::vector m_allowedValues; }; +class AttributeCombinationGen +{ + public: + AttributeCombinationGen(const std::vector &attributes); + bool next(SimulatorResourceModel &resModel); + + private: + void addAttributeToModel(int index); + + std::mutex m_lock; + std::vector m_attrGenList; + int m_index; + SimulatorResourceModel m_resModel; +}; + #endif diff --git a/service/simulator/src/client-controller/put_request_generator.cpp b/service/simulator/src/client-controller/put_request_generator.cpp index 3c8d63b..56bee77 100644 --- a/service/simulator/src/client-controller/put_request_generator.cpp +++ b/service/simulator/src/client-controller/put_request_generator.cpp @@ -75,63 +75,44 @@ void PUTRequestGenerator::SendAllRequests() OC_LOG(DEBUG, TAG, "Sending OP_START event"); m_callback(m_id, OP_START); - // Create attribute generator for value manipulation - std::vector attributeGenList; + // Create attribute combination generator for generating resource model + // with different attribute values + std::vector attributes; for (auto &attributeElement : m_rep->getAttributes()) - attributeGenList.push_back(AttributeGenerator(attributeElement.second)); + { + attributes.push_back(attributeElement.second); + } - if (!attributeGenList.size()) + if (!attributes.size()) { OC_LOG(ERROR, TAG, "Zero attribute found from resource model!"); return; } - bool hasNext = false; - do { - if (!m_stopRequested) + if (m_stopRequested) { - // Get the next possible queryParameter - std::map queryParam = m_queryParamGen.next(); - - while (true) - { - SimulatorResourceModelSP repModel(new SimulatorResourceModel); - for (auto & attributeGen : attributeGenList) - { - if (attributeGen.hasNext()) - { - SimulatorResourceModel::Attribute attribute; - if (true == attributeGen.next(attribute)) - repModel->addAttribute(attribute); - - hasNext = true; - } - else - { - SimulatorResourceModel::Attribute attribute; - if (true == attributeGen.previous(attribute)) - repModel->addAttribute(attribute); - } - } - - if (hasNext) - { - // Send the request - m_requestSender->sendRequest(queryParam, repModel, - std::bind(&PUTRequestGenerator::onResponseReceived, this, - std::placeholders::_1, std::placeholders::_2), true); - - m_requestCnt++; - hasNext = false; - continue; - } - else - { - break; - } - } + break; + } + + // Get the next possible queryParameter + std::map queryParam = m_queryParamGen.next(); + + AttributeCombinationGen attrCombGen(attributes); + + // Get the new model from attribute combination generator + SimulatorResourceModel resModel; + while (!m_stopRequested && attrCombGen.next(resModel)) + { + SimulatorResourceModelSP repModel(new SimulatorResourceModel(resModel)); + + // Send the request + m_requestSender->sendRequest(queryParam, repModel, + std::bind(&PUTRequestGenerator::onResponseReceived, this, + std::placeholders::_1, std::placeholders::_2), true); + + m_requestCnt++; } } while (m_queryParamGen.hasNext()); diff --git a/service/simulator/src/client-controller/simulator_remote_resource_impl.cpp b/service/simulator/src/client-controller/simulator_remote_resource_impl.cpp index 5523ab0..178dccb 100644 --- a/service/simulator/src/client-controller/simulator_remote_resource_impl.cpp +++ b/service/simulator/src/client-controller/simulator_remote_resource_impl.cpp @@ -302,8 +302,8 @@ int SimulatorRemoteResourceImpl::startVerification(RequestType type, if (m_getRequestSender) { return m_autoRequestGenMngr->startOnGET(m_getRequestSender, - m_requestModelList[RequestType::RQ_TYPE_GET]->getQueryParams(), - localCallback); + m_requestModelList[RequestType::RQ_TYPE_GET]->getQueryParams(), + localCallback); } break; @@ -311,9 +311,9 @@ int SimulatorRemoteResourceImpl::startVerification(RequestType type, if (m_putRequestSender) { return m_autoRequestGenMngr->startOnPUT(m_putRequestSender, - m_requestModelList[RequestType::RQ_TYPE_PUT]->getQueryParams(), - m_requestModelList[RequestType::RQ_TYPE_PUT]->getRepSchema(), - localCallback); + m_requestModelList[RequestType::RQ_TYPE_PUT]->getQueryParams(), + m_requestModelList[RequestType::RQ_TYPE_PUT]->getRepSchema(), + localCallback); } break; @@ -321,9 +321,9 @@ int SimulatorRemoteResourceImpl::startVerification(RequestType type, if (m_postRequestSender) { return m_autoRequestGenMngr->startOnPOST(m_putRequestSender, - m_requestModelList[RequestType::RQ_TYPE_POST]->getQueryParams(), - m_requestModelList[RequestType::RQ_TYPE_POST]->getRepSchema(), - localCallback); + m_requestModelList[RequestType::RQ_TYPE_POST]->getQueryParams(), + m_requestModelList[RequestType::RQ_TYPE_POST]->getRepSchema(), + localCallback); } break; diff --git a/service/simulator/src/common/simulator_resource_model.cpp b/service/simulator/src/common/simulator_resource_model.cpp index c26fa19..2a1a67d 100644 --- a/service/simulator/src/common/simulator_resource_model.cpp +++ b/service/simulator/src/common/simulator_resource_model.cpp @@ -129,6 +129,8 @@ class range_validation : public boost::static_visitor { std::vector values = m_attrItem.getAllowedValues(); + if(0 == values.size()) + return true; for (SimulatorResourceModel::Attribute::ValueVariant & val : values) { SimulatorResourceModel::Attribute::ValueVariant vVal = value; @@ -147,6 +149,8 @@ class range_validation : public boost::static_visitor { std::vector values = m_attrItem.getAllowedValues(); + if(0 == values.size()) + return true; for (SimulatorResourceModel::Attribute::ValueVariant & vVal : values) { std::string val = boost::get(vVal); @@ -185,7 +189,7 @@ std::vector SimulatorResourceModel::Attribute::AllowedValues::toStr } std::vector -SimulatorResourceModel::Attribute::AllowedValues::getValues() +SimulatorResourceModel::Attribute::AllowedValues::getValues() const { return m_values; } @@ -261,7 +265,7 @@ bool SimulatorResourceModel::Attribute::compare(SimulatorResourceModel::Attribut } std::vector -SimulatorResourceModel::Attribute::getAllowedValues() +SimulatorResourceModel::Attribute::getAllowedValues() const { return m_allowedValues.getValues(); } @@ -283,10 +287,10 @@ const return m_attributes; } -void SimulatorResourceModel::addAttribute(const SimulatorResourceModel::Attribute &attribute) +void SimulatorResourceModel::addAttribute(const SimulatorResourceModel::Attribute &attribute, bool overwrite) { if (!attribute.getName().empty() && - m_attributes.end() == m_attributes.find(attribute.getName())) + (m_attributes.end() == m_attributes.find(attribute.getName()) || overwrite)) { m_attributes[attribute.getName()] = attribute; } @@ -380,6 +384,8 @@ SimulatorResourceModelSP SimulatorResourceModel::create(const OC::OCRepresentati attribute.setValue(attributeItem.getValue()); if (attributeItem.type() == OC::AttributeType::String) attribute.setValue(attributeItem.getValue()); + if (attributeItem.type() == OC::AttributeType::Boolean) + attribute.setValue(attributeItem.getValue()); attribute.setName(attributeItem.attrname()); resModel->m_attributes[attributeItem.attrname()] = attribute; diff --git a/service/simulator/src/service-provider/resource_update_automation.cpp b/service/simulator/src/service-provider/resource_update_automation.cpp index c66711a..f2f2bc0 100644 --- a/service/simulator/src/service-provider/resource_update_automation.cpp +++ b/service/simulator/src/service-provider/resource_update_automation.cpp @@ -20,6 +20,7 @@ #include "resource_update_automation.h" #include "simulator_resource_server_impl.h" +#include "attribute_generator.h" #include "simulator_exceptions.h" #include "simulator_logger.h" #include "logger.h" @@ -92,7 +93,7 @@ void AttributeUpdateAutomation::updateAttribute() SIM_LOG(ILogger::INFO, "Automation of " << m_attrName << " attribute is completed."); } - // Notify application through callback + // Notify application if (m_callback) m_callback(m_resource->getURI(), m_id); @@ -142,65 +143,61 @@ ResourceUpdateAutomation::ResourceUpdateAutomation(int id, SimulatorResourceServ : m_resource(resource), m_type(type), m_id(id), + m_stopRequested(false), m_updateInterval(interval), m_callback(callback), - m_finishedCallback(finishedCallback) {} + m_finishedCallback(finishedCallback), + m_thread(nullptr) {} void ResourceUpdateAutomation::start() { - m_resModel = m_resource->getModel(); - std::map attributes = m_resModel.getAttributes(); - if (0 == attributes.size()) + SimulatorResourceModel resModel = m_resource->getModel(); + std::map attributesTable = + resModel.getAttributes(); + if (0 == attributesTable.size()) { OC_LOG(ERROR, RTAG, "Resource has zero attributes!"); throw SimulatorException(SIMULATOR_ERROR, "Resource has zero attributes!"); } - int id = 0; - for (auto & attribute : attributes) - { - AttributeUpdateAutomationSP attributeAutomation(new AttributeUpdateAutomation( - id, m_resource, attribute.first, m_type, m_updateInterval, nullptr, - std::bind(&ResourceUpdateAutomation::finished, this, std::placeholders::_1))); + std::vector attributes; + for (auto &attributeEntry : attributesTable) + attributes.push_back(attributeEntry.second); - m_attrUpdationList[id++] = attributeAutomation; - try - { - attributeAutomation->start(); - } - catch (SimulatorException &e) - { - stop(); - throw; - } - } + m_thread = new std::thread(&ResourceUpdateAutomation::updateAttributes, this, attributes); } -void ResourceUpdateAutomation::finished(int id) +void ResourceUpdateAutomation::stop() { - if (m_attrUpdationList.end() != m_attrUpdationList.find(id)) - { - m_attrUpdationList.erase(m_attrUpdationList.find(id)); - } - - if (!m_attrUpdationList.size()) - { - // Notify application through callback - if (m_callback) - m_callback(m_resource->getURI(), m_id); - - if (m_finishedCallback) - m_finishedCallback(m_id); - } + m_stopRequested = true; + if (m_thread) + m_thread->join(); } -void ResourceUpdateAutomation::stop() +void ResourceUpdateAutomation::updateAttributes( + std::vector attributes) { - // Stop all the attributes updation - for (auto & attrAutomation : m_attrUpdationList) + SimulatorResourceServerImpl *resourceImpl = + dynamic_cast(m_resource); + + AttributeCombinationGen attrCombGen(attributes); + SimulatorResourceModel resModel; + while (!m_stopRequested && attrCombGen.next(resModel)) { - (attrAutomation.second)->stop(); + for (auto &attributeEntry : resModel.getAttributes()) + { + resourceImpl->updateAttributeValue(attributeEntry.first, attributeEntry.second.getValue()); + } + + resourceImpl->notifyApp(); + SLEEP_FOR(m_updateInterval); } - m_attrUpdationList.clear(); + // Notify application + if (m_callback) + m_callback(m_resource->getURI(), m_id); + + if (m_finishedCallback && !m_stopRequested) + m_finishedCallback(m_id); } + diff --git a/service/simulator/src/service-provider/resource_update_automation.h b/service/simulator/src/service-provider/resource_update_automation.h index ba80911..6ae1f0e 100644 --- a/service/simulator/src/service-provider/resource_update_automation.h +++ b/service/simulator/src/service-provider/resource_update_automation.h @@ -66,17 +66,17 @@ class ResourceUpdateAutomation void stop(); - void finished(int id); - private: + void updateAttributes(std::vector attributes); + SimulatorResourceServer *m_resource; AutomationType m_type; int m_id; + bool m_stopRequested; int m_updateInterval; - SimulatorResourceModel m_resModel; - std::map m_attrUpdationList; updateCompleteCallback m_callback; std::function m_finishedCallback; + std::thread *m_thread; }; typedef std::shared_ptr ResourceUpdateAutomationSP; diff --git a/service/simulator/src/service-provider/simulator_resource_server_impl.cpp b/service/simulator/src/service-provider/simulator_resource_server_impl.cpp index 426ee9d..4f3b042 100644 --- a/service/simulator/src/service-provider/simulator_resource_server_impl.cpp +++ b/service/simulator/src/service-provider/simulator_resource_server_impl.cpp @@ -66,7 +66,7 @@ void SimulatorResourceServerImpl::setObservable(bool state) } int SimulatorResourceServerImpl::startUpdateAutomation(AutomationType type, - updateCompleteCallback callback) + int updateInterval, updateCompleteCallback callback) { if (!callback) { @@ -80,12 +80,11 @@ int SimulatorResourceServerImpl::startUpdateAutomation(AutomationType type, throw SimulatorException(SIMULATOR_NO_RESOURCE, "Invalid resource!"); } - // This fix is temporarily to give 500 ms default delay in automation until the new UI change is merged. - return m_updateAutomationMgr.startResourceAutomation(this, type, 500, callback); + return m_updateAutomationMgr.startResourceAutomation(this, type, updateInterval, callback); } int SimulatorResourceServerImpl::startUpdateAutomation(const std::string &attrName, - AutomationType type, + AutomationType type, int updateInterval, updateCompleteCallback callback) { if (!callback) @@ -100,8 +99,7 @@ int SimulatorResourceServerImpl::startUpdateAutomation(const std::string &attrNa throw SimulatorException(SIMULATOR_NO_RESOURCE, "Invalid resource!"); } - // This fix is temporarily to give 500 ms default delay in automation until the new UI change is merged. - return m_updateAutomationMgr.startAttributeAutomation(this, attrName, type, 500, callback); + return m_updateAutomationMgr.startAttributeAutomation(this, attrName, type, updateInterval, callback); } std::vector SimulatorResourceServerImpl::getResourceAutomationIds() diff --git a/service/simulator/src/service-provider/simulator_resource_server_impl.h b/service/simulator/src/service-provider/simulator_resource_server_impl.h index 471ed9c..010865c 100644 --- a/service/simulator/src/service-provider/simulator_resource_server_impl.h +++ b/service/simulator/src/service-provider/simulator_resource_server_impl.h @@ -41,11 +41,11 @@ class SimulatorResourceServerImpl : public SimulatorResourceServer bool isObservable() const; - int startUpdateAutomation(AutomationType type, + int startUpdateAutomation(AutomationType type, int updateInterval, updateCompleteCallback callback); int startUpdateAutomation(const std::string &attrName, AutomationType type, - updateCompleteCallback callback); + int updateInterval, updateCompleteCallback callback); std::vector getResourceAutomationIds();