Initial merge-commit of the OIC code. Should successfully do discovery for single...
[platform/upstream/iotivity.git] / OCLib / OCPlatform.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Corporation All Rights Reserved.
4 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
5
6 //******************************************************************
7 // File name:
8 //     OCPlatform.cpp
9 //
10 // Description: Implementation of the OCPlatform.
11 //
12 //
13 //
14 //*********************************************************************
15
16 #include <random>
17
18 #include "OCPlatform.h"
19
20 namespace OC 
21 {
22         // Constructor. Internally calls private init function
23         OCPlatform::OCPlatform(const PlatformConfig& config)
24         {
25                 init(config);
26         }
27
28         // Destructor
29         OCPlatform::~OCPlatform(void)
30         {
31                 std::cout << "platform destructor called" << std::endl;
32                 cleanup();
33         }
34
35         void OCPlatform::init(const PlatformConfig& config)
36         {
37                 std::unique_ptr<WrapperFactory> wrapperInstance(new WrapperFactory());
38                 m_WrapperInstance = std::move(wrapperInstance);
39
40                 if(config.mode == ModeType::Server)
41                 {
42                         // Call server wrapper init 
43                         m_server = m_WrapperInstance->CreateServerWrapper(config);
44                 }
45                 else if(config.mode == ModeType::Client)
46                 {
47                         // Call client wrapper init
48                         m_client = m_WrapperInstance->CreateClientWrapper(config);
49                 }
50                 else 
51                 {
52                         // This must be both server and client
53                         m_server = m_WrapperInstance->CreateServerWrapper(config);
54                         m_client = m_WrapperInstance->CreateClientWrapper(config);
55                 }
56         }
57
58         void OCPlatform::cleanup()
59         {
60                 if(m_server)
61                 {
62                         //delete m_server;
63                 }
64
65                 if(m_client)
66                 {
67                         //delete m_client;
68                 }
69         }
70
71     
72         void OCPlatform::findResource(const std::string& host, const std::string& resourceName, 
73                 std::function<void(OCResource::Ptr)> resourceHandler)
74         {
75                 if(m_client)
76                 {
77                         m_client->ListenForResource(host, resourceName, resourceHandler);
78                 }
79         }
80     
81
82         void OCPlatform::registerResource(const std::string& resourceURI, const std::string& resourceTypeName, 
83                         named_property_binding_vector properties)
84         {
85                 if(m_server)
86                 {
87                         try{
88                                 m_server->registerResource(resourceURI, resourceTypeName, properties); 
89                         }catch(std::exception e) // define our own expception. 
90                         {
91                                 throw e;
92                         }
93                 }
94         }
95
96 } //namespace OC