main.cpp 3.88 KB
Newer Older
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
1
/*
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 * Copyright (c) 2017 Sprint
 *
 * 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.
 */
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
16

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
17
#include "logger.hpp"
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
18
#include "nrf-api-server.h"
Rohan's avatar
Rohan committed
19
#include "nrf-http2-server.h"
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
20
21
#include "nrf_app.hpp"
#include "nrf_client.hpp"
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
22
#include "options.hpp"
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
23
#include "pid_file.hpp"
Rohan's avatar
Rohan committed
24
#include "conversions.hpp"
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
25

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
26
27
28
29
30
31
#include "pistache/endpoint.h"
#include "pistache/http.h"
#include "pistache/router.h"

#include <signal.h>
#include <stdint.h>
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
32
33
34
35
#include <stdlib.h>  // srand
#include <unistd.h>  // get_pid(), pause()
#include <iostream>
#include <thread>
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
36

37
using namespace oai::nrf::app;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
38
using namespace util;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
39
using namespace std;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
40

Raphael Defosseux's avatar
Raphael Defosseux committed
41
nrf_app* nrf_app_inst = nullptr;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
42
nrf_config nrf_cfg;
43
NRFApiServer* api_server           = nullptr;
Rohan's avatar
Rohan committed
44
nrf_http2_server* nrf_api_server_2 = nullptr;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
45
46

//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
47
void my_app_signal_handler(int s) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
48
  std::cout << "Caught signal " << s << std::endl;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
49
  Logger::system().startup("exiting");
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
50
  std::cout << "Freeing Allocated memory..." << std::endl;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
51
52
53
54
  if (api_server) {
    api_server->shutdown();
    delete api_server;
    api_server = nullptr;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
55
  }
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
56
  std::cout << "NRF API Server memory done" << std::endl;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
57

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
58
  if (nrf_app_inst) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
59
    delete nrf_app_inst;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
60
61
62
63
    nrf_app_inst = nullptr;
  }

  std::cout << "NRF APP memory done" << std::endl;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
64
  std::cout << "Freeing allocated memory done" << std::endl;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
65

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
66
67
  exit(0);
}
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
68

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
69
//------------------------------------------------------------------------------
Raphael Defosseux's avatar
Raphael Defosseux committed
70
int main(int argc, char** argv) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
71
  srand(time(NULL));
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
72
73

  // Command line options
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
74
  if (!Options::parse(argc, argv)) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
75
76
    std::cout << "Options::parse() failed" << std::endl;
    return 1;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
77
78
79
  }

  // Logger
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
80
81
  Logger::init("nrf", Options::getlogStdout(), Options::getlogRotFilelog());
  Logger::nrf_app().startup("Options parsed");
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
82
83
84
85
86
87
88
89

  struct sigaction sigIntHandler;
  sigIntHandler.sa_handler = my_app_signal_handler;
  sigemptyset(&sigIntHandler.sa_mask);
  sigIntHandler.sa_flags = 0;
  sigaction(SIGINT, &sigIntHandler, NULL);

  // Config
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
90
91
  nrf_cfg.load(Options::getlibconfigConfig());
  nrf_cfg.display();
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
92

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
93
  // Event subsystem
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
94
95
  nrf_event ev;

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
96
  // NRF application layer
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
97
98
  nrf_app_inst = new nrf_app(Options::getlibconfigConfig(), ev);

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
99
  // Task Manager
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
100
101
  task_manager tm(ev);
  std::thread task_manager_thread(&task_manager::run, &tm);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
102
103
104

  // PID file
  // Currently hard-coded value. TODO: add as config option.
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
105
  string pid_file_name = get_exe_absolute_path("/var/run", nrf_cfg.instance);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
106
107
108
  if (!is_pid_file_lock_success(pid_file_name.c_str())) {
    Logger::nrf_app().error("Lock PID file %s failed\n", pid_file_name.c_str());
    exit(-EDEADLK);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
109
  }
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
110

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
111
112
  // NRF Pistache API server (HTTP1)
  Pistache::Address addr(
Raphael Defosseux's avatar
Raphael Defosseux committed
113
      std::string(inet_ntoa(*((struct in_addr*) &nrf_cfg.sbi.addr4))),
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
114
      Pistache::Port(nrf_cfg.sbi.port));
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
115
116
117
  api_server = new NRFApiServer(addr, nrf_app_inst);
  api_server->init(2);
  std::thread nrf_manager(&NRFApiServer::start, api_server);
Rohan's avatar
Rohan committed
118
119
120
121
122
123

  // NRF NGHTTP API server (HTTP2)
  nrf_api_server_2 = new nrf_http2_server(
      conv::toString(nrf_cfg.sbi.addr4), nrf_cfg.sbi_http2_port, nrf_app_inst);
  std::thread nrf_http2_manager(&nrf_http2_server::start, nrf_api_server_2);

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
124
  nrf_manager.join();
Rohan's avatar
Rohan committed
125
126
  nrf_http2_manager.join();

Raphael Defosseux's avatar
Raphael Defosseux committed
127
  FILE* fp             = NULL;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
128
  std::string filename = fmt::format("/tmp/nrf_{}.status", getpid());
Raphael Defosseux's avatar
Raphael Defosseux committed
129
  fp                   = fopen(filename.c_str(), "w+");
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
130
131
132
  fprintf(fp, "STARTED\n");
  fflush(fp);
  fclose(fp);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
133

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
134
135
136
  pause();
  return 0;
}