X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=src%2Frest-server%2Fpistacheserver.cpp;fp=src%2Frest-server%2Fpistacheserver.cpp;h=7f7fed9d6de5bfa229f792a1c75566d7967aaa45;hb=e64778dac480d66d93449efd3e9767cea4f980f9;hp=0000000000000000000000000000000000000000;hpb=56044b79a2282e50a8c271b36289b1724cb40cd8;p=ric-plt%2Fxapp-frame-cpp.git diff --git a/src/rest-server/pistacheserver.cpp b/src/rest-server/pistacheserver.cpp new file mode 100644 index 0000000..7f7fed9 --- /dev/null +++ b/src/rest-server/pistacheserver.cpp @@ -0,0 +1,200 @@ +/* +# ================================================================================== +# Copyright (c) 2020 HCL Technologies Limited. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ================================================================================== +*/ +#include"pistacheserver.h" +namespace xapp +{ + + pistacheserver::pistacheserver(Pistache::Address addr,std::vector method,std::vector static_routing,std::vector dynamic_routing) + : httpEndpoint(std::make_shared(addr)) + { + this->t=new std::thread*[1]; + this->method=method; + this->static_routing=static_routing; + this->dynamic_routing=dynamic_routing; + assert(static_routing.size()==method.size() && dynamic_routing.size()==method.size()); + stat_sz=cal_stat_size(); + dyn_sz=cal_dyn_size(); + } + int pistacheserver::cal_stat_size()//caclualting no of true in static_routing + { + int size=0; + for(int i=0;ibase=base; + } + void pistacheserver::setup_static_route(std::unordered_map route)//routing path + { + this->route_static=route; + } + void pistacheserver::setup_dynamic_route(std::unordered_map route)//routing path + { + this->route_dynamic=route; + } +void pistacheserver::add_static_cb(std::unordered_map cb) +{ + this->cb_static=cb; +} +void pistacheserver::add_dynamic_cb(std::unordered_map cb) +{ + this->cb_dynamic=cb; +} + + void pistacheserver::init(size_t thr = 2) + { + auto opts = Pistache::Http::Endpoint::options().threads(thr).flags(Pistache::Tcp::Options::ReuseAddr)/*.flags(Pistache::Tcp::Options::ReusePort)*/; // how many threads for the server + httpEndpoint->init(opts); + 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. + + 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. + for (int i=0;isetHandler(router.handler()); + t[0]=new std::thread(&pistacheserver::thread_start,this); + t[0]->detach(); + } + void pistacheserver::thread_start() + { + this->httpEndpoint->serve(); + + } + + void pistacheserver::shutdown() + { + httpEndpoint->shutdown(); + } + + void pistacheserver::setupRoutes_get(int index) + { + using namespace Pistache::Rest; + auto search_s=route_static.find(index); + auto search_d=route_dynamic.find(index); + auto cbs_search=cb_static.find(index); + auto cbd_search=cb_dynamic.find(index); + assert (search_s!=route_static.end() || search_d!=route_dynamic.end()); + if (static_routing[index-1]) + { + std::cout<<"static routing get"<second, Routes::bind(cbs_search->second)); + } + if (dynamic_routing[index-1]) + { + std::cout<<"dynamic routing get"<second+ dynamic_id, Routes::bind(cbd_search->second)); + } + + // Default handler, called when a route is not found + router.addCustomHandler(Routes::bind(&pistacheserver::default_handler, this)); + } + void pistacheserver::setupRoutes_post(int index) + { + using namespace Pistache::Rest; + auto search_s=route_static.find(index); + auto search_d=route_dynamic.find(index); + auto cbs_search=cb_static.find(index); + auto cbd_search=cb_dynamic.find(index); + assert (search_s!=route_static.end() || search_d!=route_dynamic.end()); + if (static_routing[index-1]) + { + std::cout<<"static routing post"<second, Routes::bind(cbs_search->second)); + } + if (dynamic_routing[index-1]) + { + std::cout<<"dynamic routing post"<second+ dynamic_id, Routes::bind(cbd_search->second)); + } + // Default handler, called when a route is not found + router.addCustomHandler(Routes::bind(&pistacheserver::default_handler, this)); + } + void pistacheserver::setupRoutes_del(int index) + { + using namespace Pistache::Rest; + auto search_s=route_static.find(index); + auto search_d=route_dynamic.find(index); + auto cbs_search=cb_static.find(index); + auto cbd_search=cb_dynamic.find(index); + assert (search_s!=route_static.end() || search_d!=route_dynamic.end()); + if (static_routing[index-1]) + { + std::cout<<"static routing delete"<second, Routes::bind(cbs_search->second)); + } + if (dynamic_routing[index-1]) + { + std::cout<<"dynamic routing delete"<second+ dynamic_id, Routes::bind(cbd_search->second)); + } + // Default handler, called when a route is not found + router.addCustomHandler(Routes::bind(&pistacheserver::default_handler, this)); + } + +} + + +