main.cpp 5.34 KB
Newer Older
gauthier's avatar
mains    
gauthier 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

gauthier's avatar
mains    
gauthier committed
17
18
19
20
21
22
#include "async_shell_cmd.hpp"
#include "common_defs.h"
#include "itti.hpp"
#include "logger.hpp"
#include "options.hpp"
#include "pid_file.hpp"
23
#include "smf_app.hpp"
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
24
#include "smf_config.hpp"
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
25
26
27
28
#include "smf-api-server.h"
#include "pistache/endpoint.h"
#include "pistache/http.h"
#include "pistache/router.h"
29
#include "smf-http2-server.h"
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
30

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

38
using namespace smf;
39
using namespace util;
gauthier's avatar
mains    
gauthier committed
40
using namespace std;
41
using namespace oai::smf_server::api;
gauthier's avatar
mains    
gauthier committed
42

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
43
44
45
itti_mw* itti_inst                    = nullptr;
async_shell_cmd* async_shell_cmd_inst = nullptr;
smf_app* smf_app_inst                 = nullptr;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
46
smf_config smf_cfg;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
47
48
SMFApiServer* smf_api_server_1     = nullptr;
smf_http2_server* smf_api_server_2 = nullptr;
49

gauthier's avatar
gauthier committed
50
51
52
void send_heartbeat_to_tasks(const uint32_t sequence);

//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
53
54
void send_heartbeat_to_tasks(const uint32_t sequence) {
  itti_msg_ping* itti_msg = new itti_msg_ping(TASK_SMF_APP, TASK_ALL, sequence);
gauthier's avatar
gauthier committed
55
  std::shared_ptr<itti_msg_ping> i = std::shared_ptr<itti_msg_ping>(itti_msg);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
56
  int ret                          = itti_inst->send_broadcast_msg(i);
gauthier's avatar
gauthier committed
57
  if (RETURNok != ret) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
58
59
    Logger::smf_app().error(
        "Could not send ITTI message %s to task TASK_ALL", i->get_msg_name());
gauthier's avatar
gauthier committed
60
61
62
  }
}

gauthier's avatar
mains    
gauthier committed
63
//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
64
void my_app_signal_handler(int s) {
gauthier's avatar
mains    
gauthier committed
65
  std::cout << "Caught signal " << s << std::endl;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
66
  Logger::system().startup("exiting");
67
  itti_inst->send_terminate_msg(TASK_SMF_APP);
gauthier's avatar
mains    
gauthier committed
68
69
  itti_inst->wait_tasks_end();
  std::cout << "Freeing Allocated memory..." << std::endl;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
70
71
  if (async_shell_cmd_inst) delete async_shell_cmd_inst;
  async_shell_cmd_inst = nullptr;
gauthier's avatar
mains    
gauthier committed
72
  std::cout << "Async Shell CMD memory done." << std::endl;
73
74
75
76
77
78
79
80
81
82
  if (smf_api_server_1) {
    smf_api_server_1->shutdown();
    delete smf_api_server_1;
    smf_api_server_1 = nullptr;
  }
  if (smf_api_server_2) {
    smf_api_server_2->stop();
    delete smf_api_server_2;
    smf_api_server_2 = nullptr;
  }
83
  std::cout << "SMF API Server memory done." << std::endl;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
84
85
  if (itti_inst) delete itti_inst;
  itti_inst = nullptr;
gauthier's avatar
mains    
gauthier committed
86
  std::cout << "ITTI memory done." << std::endl;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
87
88
  if (smf_app_inst) delete smf_app_inst;
  smf_app_inst = nullptr;
89
  std::cout << "SMF APP memory done." << std::endl;
gauthier's avatar
mains    
gauthier committed
90
91
92
93
  std::cout << "Freeing Allocated memory done" << std::endl;
  exit(0);
}
//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
94
95
int main(int argc, char** argv) {
  srand(time(NULL));
gauthier's avatar
mains    
gauthier committed
96
97

  // Command line options
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
98
99
100
  if (!Options::parse(argc, argv)) {
    std::cout << "Options::parse() failed" << std::endl;
    return 1;
gauthier's avatar
mains    
gauthier committed
101
  }
102
103

  // Logger
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
104
105
  Logger::init("smf", Options::getlogStdout(), Options::getlogRotFilelog());
  Logger::smf_app().startup("Options parsed");
gauthier's avatar
mains    
gauthier committed
106
107
108
109
110
111
112
113

  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
114
115
  smf_cfg.load(Options::getlibconfigConfig());
  smf_cfg.display();
gauthier's avatar
mains    
gauthier committed
116

117
  // Inter-task Interface
gauthier's avatar
mains    
gauthier committed
118
  itti_inst = new itti_mw();
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
119
  itti_inst->start(smf_cfg.itti.itti_timer_sched_params);
gauthier's avatar
mains    
gauthier committed
120
121

  // system command
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
122
123
  async_shell_cmd_inst =
      new async_shell_cmd(smf_cfg.itti.async_cmd_sched_params);
gauthier's avatar
mains    
gauthier committed
124

125
126
  // SMF application layer
  smf_app_inst = new smf_app(Options::getlibconfigConfig());
gauthier's avatar
mains    
gauthier committed
127
128
129

  // PID file
  // Currently hard-coded value. TODO: add as config option.
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
130
131
132
133
  string pid_file_name = get_exe_absolute_path("/var/run", smf_cfg.instance);
  if (!is_pid_file_lock_success(pid_file_name.c_str())) {
    Logger::smf_app().error("Lock PID file %s failed\n", pid_file_name.c_str());
    exit(-EDEADLK);
gauthier's avatar
mains    
gauthier committed
134
135
  }

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
136
137
138
139
  // SMF Pistache API server (HTTP1)
  Pistache::Address addr(
      std::string(inet_ntoa(*((struct in_addr*) &smf_cfg.sbi.addr4))),
      Pistache::Port(smf_cfg.sbi.port));
140
  smf_api_server_1 = new SMFApiServer(addr, smf_app_inst);
141
  smf_api_server_1->init(2);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
142
  // smf_api_server_1->start();
143
  std::thread smf_http1_manager(&SMFApiServer::start, smf_api_server_1);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
144
145
146
147
  // SMF NGHTTP API server (HTTP2)
  smf_api_server_2 = new smf_http2_server(
      conv::toString(smf_cfg.sbi.addr4), smf_cfg.sbi_http2_port, smf_app_inst);
  // smf_api_server_2->start();
148
  std::thread smf_http2_manager(&smf_http2_server::start, smf_api_server_2);
149

150
151
152
  // Register to NRF and discover appropriate UPFs
  smf_app_inst->start_nf_registration_discovery();

153
154
155
  smf_http1_manager.join();
  smf_http2_manager.join();

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
156
  FILE* fp             = NULL;
157
  std::string filename = fmt::format("/tmp/smf_{}.status", getpid());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
158
  fp                   = fopen(filename.c_str(), "w+");
gauthier's avatar
mains    
gauthier committed
159
160
161
162
163
164
165
  fprintf(fp, "STARTED\n");
  fflush(fp);
  fclose(fp);

  pause();
  return 0;
}