RIC-641 Fixing client/server model definitions and adding client and server API
[ric-plt/xapp-frame-cpp.git] / src / rest-server / pistacheserver.cpp
1 /*
2 # ==================================================================================
3 # Copyright (c) 2020 HCL Technologies Limited.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 # ==================================================================================
17 */
18 #include"pistacheserver.h"
19 namespace xapp
20 {
21  
22         pistacheserver::pistacheserver(Pistache::Address addr,std::vector<std::string> method,std::vector<bool> static_routing,std::vector<bool> dynamic_routing)
23         : httpEndpoint(std::make_shared<Pistache::Http::Endpoint>(addr))
24     { 
25         this->t=new std::thread*[1];
26         this->method=method;
27         this->static_routing=static_routing;
28         this->dynamic_routing=dynamic_routing;
29         assert(static_routing.size()==method.size() && dynamic_routing.size()==method.size());
30         stat_sz=cal_stat_size();
31         dyn_sz=cal_dyn_size();
32     }
33    int pistacheserver::cal_stat_size()//caclualting no of true in static_routing
34         {
35                 int size=0;
36                 for(int i=0;i<static_routing.size();i++)
37                 {
38                         if (static_routing[i])
39                                 size++;
40                 }
41                 return size;
42         }
43
44          int pistacheserver::cal_dyn_size() //calaculating no true in dynamic_routing
45         {
46                 int size=0;
47                 for(int i=0;i<dynamic_routing.size();i++)
48                 {
49                         if (dynamic_routing[i])
50                                 size++;
51                 }
52                 return size;
53         }
54
55     void pistacheserver::setup_base_url(std::string base)
56     {
57         this->base=base;
58     }
59     void pistacheserver::setup_static_route(std::unordered_map<int,std::string> route)//routing path
60     {
61         this->route_static=route;
62     }
63  void pistacheserver::setup_dynamic_route(std::unordered_map<int,std::string> route)//routing path
64     {
65         this->route_dynamic=route;
66     }
67 void pistacheserver::add_static_cb(std::unordered_map<int,usr_callback> cb)
68 {
69         this->cb_static=cb;
70 }
71 void pistacheserver::add_dynamic_cb(std::unordered_map<int,usr_callback> cb) 
72 {
73         this->cb_dynamic=cb;
74 }
75
76     void pistacheserver::init(size_t thr = 2) 
77     {
78                  auto opts = Pistache::Http::Endpoint::options().threads(thr).flags(Pistache::Tcp::Options::ReuseAddr)/*.flags(Pistache::Tcp::Options::ReusePort)*/; // how many threads for the server
79                  httpEndpoint->init(opts);
80                 assert(route_static.size()==stat_sz && route_dynamic.size()==dyn_sz); //no of true in satatic_routing and dynamic_routig should match with size of route_static and  route_dynamic respectively. 
81
82                 assert(cb_static.size()==stat_sz && cb_dynamic.size()==dyn_sz); //no of true in satatic_routing and dynamic_routig should match with size of cb_static and cb_dynamic respectively.
83                 for (int i=0;i<method.size();i++)
84         {
85                 if (method[i]=="get")
86                 {
87                         setupRoutes_get(i+1);
88                 }
89                 else if (method[i]=="post" )
90                 {
91                         setupRoutes_post(i+1);
92                 }
93
94                 else if (method[i]=="del")
95                 {
96                         setupRoutes_del(i+1);
97                 }
98         
99                 else
100                 {
101                         std::cout<<"Wrong Method called \n";
102                 }
103         }
104
105
106     }
107
108
109     void pistacheserver::start()
110     {
111         httpEndpoint->setHandler(router.handler());
112         t[0]=new std::thread(&pistacheserver::thread_start,this);
113         t[0]->detach();
114     }
115     void pistacheserver::thread_start()
116     {
117        this->httpEndpoint->serve();
118
119     }
120
121     void pistacheserver::shutdown()
122     {
123         httpEndpoint->shutdown();
124     }
125
126     void pistacheserver::setupRoutes_get(int index)
127     {
128         using namespace Pistache::Rest;
129         auto search_s=route_static.find(index);
130         auto search_d=route_dynamic.find(index);
131         auto cbs_search=cb_static.find(index);
132         auto cbd_search=cb_dynamic.find(index);
133         assert (search_s!=route_static.end() || search_d!=route_dynamic.end()); 
134         if (static_routing[index-1])
135                 {
136                 std::cout<<"static routing get"<<std::endl;
137                 assert (search_s!=route_static.end() && cbs_search!=cb_static.end());
138                 Routes::Get(router, base + search_s->second, Routes::bind(cbs_search->second));
139                 }
140         if (dynamic_routing[index-1])
141                 {
142                 std::cout<<"dynamic routing get"<<std::endl;
143                 assert (search_d!=route_dynamic.end() && cbd_search!=cb_dynamic.end());
144                 Routes::Get(router, base + search_d->second+ dynamic_id, Routes::bind(cbd_search->second));
145                 }
146
147         // Default handler, called when a route is not found
148         router.addCustomHandler(Routes::bind(&pistacheserver::default_handler, this));
149     }
150     void pistacheserver::setupRoutes_post(int index)
151     {
152         using namespace Pistache::Rest;
153         auto search_s=route_static.find(index);
154         auto search_d=route_dynamic.find(index);
155         auto cbs_search=cb_static.find(index);
156         auto cbd_search=cb_dynamic.find(index);
157         assert (search_s!=route_static.end() || search_d!=route_dynamic.end()); 
158         if (static_routing[index-1])
159                 {
160                 std::cout<<"static routing post"<<std::endl;
161                 assert (search_s!=route_static.end() && cbs_search!=cb_static.end());
162                 Routes::Post(router, base + search_s->second, Routes::bind(cbs_search->second));
163                 }
164         if (dynamic_routing[index-1])
165                 {
166                 std::cout<<"dynamic routing post"<<std::endl;
167                 assert (search_d!=route_dynamic.end() && cbd_search!=cb_dynamic.end());
168                 Routes::Post(router, base + search_d->second+ dynamic_id, Routes::bind(cbd_search->second));
169                 }
170         // Default handler, called when a route is not found
171         router.addCustomHandler(Routes::bind(&pistacheserver::default_handler, this));
172     }
173     void pistacheserver::setupRoutes_del(int index)
174     {
175         using namespace Pistache::Rest;
176         auto search_s=route_static.find(index);
177         auto search_d=route_dynamic.find(index);
178         auto cbs_search=cb_static.find(index);
179         auto cbd_search=cb_dynamic.find(index);
180         assert (search_s!=route_static.end() || search_d!=route_dynamic.end()); 
181         if (static_routing[index-1])
182                 {
183                 std::cout<<"static routing delete"<<std::endl;
184                 assert (search_s!=route_static.end() && cbs_search!=cb_static.end());
185                 Routes::Delete(router, base + search_s->second, Routes::bind(cbs_search->second));
186                 }
187         if (dynamic_routing[index-1])
188                 {
189                 std::cout<<"dynamic routing delete"<<std::endl;
190                 assert (search_d!=route_dynamic.end() && cbd_search!=cb_dynamic.end());
191                 Routes::Delete(router, base + search_d->second+ dynamic_id, Routes::bind(cbd_search->second));
192                 }
193         // Default handler, called when a route is not found
194         router.addCustomHandler(Routes::bind(&pistacheserver::default_handler, this));
195     }
196
197 }
198
199
200