2 # ==================================================================================
3 # Copyright (c) 2020 HCL Technologies Limited.
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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 # ==================================================================================
20 #include <cpprest/http_listener.h>
21 #include <cpprest/json.h>
22 #include <cpprest/uri.h>
24 using namespace web::http;
25 using namespace web::http::experimental::listener;
26 using namespace utility;
27 std::vector<std::string>SubscriptionIds;
28 #define TRACE(msg) wcout << msg
32 json::value const & jvalue)
34 cout<<"\ndisplaying REST Notification\n";
35 wcout << jvalue.serialize().c_str() << endl;
39 void handle_request(http_request request)
41 auto answer = json::value::object();
42 cout<<"\nPrinting POST request content\n";
43 cout<<request.to_string()<<"\n";
46 .then([&answer](pplx::task<json::value> task) {
52 catch (http_exception const & e)
54 cout<<"\ninside catch block";
55 wcout << e.what() << endl;
61 request.reply(status_codes::OK, answer);
64 void handle_post(http_request request)
66 TRACE("\nhandle POST\n");
68 handle_request(request);
71 void handle_put(http_request request)
73 TRACE("\nhandle PUT\n");
75 handle_request(request);
81 utility::string_t port = U("8080");
82 utility::string_t address = U("http://0.0.0.0:");
84 address.append(U("/ric/v1/subscriptions/response"));
85 uri_builder uri(address);
87 auto addr = uri.to_uri().to_string();
88 http_listener listener(addr);
89 //http_listener listener("http://localhost:8080/ric");
90 cout<<"validated uri = "<<uri::validate(addr)<<"\n";
91 ucout << utility::string_t(U("Listening for REST Notification at: ")) << addr << std::endl;
92 listener.support(methods::POST,[](http_request request) { handle_post(request);});
93 listener.support(methods::PUT,[](http_request request){ handle_put(request);});
98 .then([&listener]() { })
103 catch (exception const & e)
105 wcout << e.what() << endl;
110 void signalHandler( int signum ) {
111 cout << "Interrupt signal (" << signum << ") received.\n";
115 int main(int argc, char *argv[]){
118 std::thread::id my_id = std::this_thread::get_id();
119 std::stringstream thread_id;
120 std::stringstream ss;
124 mdclog_write(MDCLOG_INFO, "Starting thread %s", thread_id.str().c_str());
128 //change the priority depending upon application requirement
129 config.loadDefaultSettings();
130 config.loadEnvVarSettings();
131 config.loadCmdlineSettings(argc, argv);
133 //Register signal handler to stop
134 signal(SIGINT, signalHandler);
135 signal(SIGTERM, signalHandler);
137 //getting the listening port and xapp name info
138 std::string port = config[XappSettings::SettingName::BOUNCER_PORT];
139 std::string name = config[XappSettings::SettingName::XAPP_NAME];
142 std::unique_ptr<XappRmr> rmr = std::make_unique<XappRmr>(port);
143 rmr->xapp_rmr_init(true);
146 //Create Subscription Handler if Xapp deals with Subscription.
147 //std::unique_ptr<SubscriptionHandler> sub_handler = std::make_unique<SubscriptionHandler>();
149 SubscriptionHandler sub_handler;
151 //create Bouncer Xapp Instance.
152 std::unique_ptr<Xapp> b_xapp;
153 b_xapp = std::make_unique<Xapp>(std::ref(config),std::ref(*rmr));
155 mdclog_write(MDCLOG_INFO, "Created Bouncer Xapp Instance");
156 //Startup E2 subscription
157 std::thread t1(std::ref(start_server));
159 b_xapp->startup(sub_handler);
164 //start listener threads and register message handlers.
165 int num_threads = std::stoi(config[XappSettings::SettingName::THREADS]);
166 mdclog_write(MDCLOG_INFO, "Starting Listener Threads. Number of Workers = %d", num_threads);
168 std::unique_ptr<XappMsgHandler> mp_handler = std::make_unique<XappMsgHandler>(config[XappSettings::SettingName::XAPP_ID], sub_handler);
170 b_xapp->start_xapp_receiver(std::ref(*mp_handler));
172 sleep(20);//waiting for some time before sending delete.
175 b_xapp->shutdown();//will start the sending delete procedure.