main.cpp 4.35 KB
Newer Older
gauthier's avatar
mains  
gauthier committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
* 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.
*/

#include "async_shell_cmd.hpp"
#include "common_defs.h"
#include "itti.hpp"
#include "logger.hpp"
#include "options.hpp"
#include "pid_file.hpp"
#include "pgw_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
29
30
#include "smf-api-server.h"
#include "pistache/endpoint.h"
#include "pistache/http.h"
#include "pistache/router.h"


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

38
39
40
using namespace gtpv2c;
using namespace pgwc;
using namespace util;
gauthier's avatar
mains  
gauthier committed
41
using namespace std;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
42
using namespace oai::smf::api;
gauthier's avatar
mains  
gauthier committed
43
44
45
46

itti_mw *itti_inst = nullptr;
async_shell_cmd *async_shell_cmd_inst = nullptr;
pgw_app *pgw_app_inst = nullptr;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
47
smf_config smf_cfg;
gauthier's avatar
mains  
gauthier committed
48

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

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

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

  // Command line options
  if ( !Options::parse( argc, argv ) )
  {
     std::cout << "Options::parse() failed" << std::endl;
     return 1;
  }
90
91
92
93

  // Logger
  Logger::init( "spgwc" , Options::getlogStdout() , Options::getlogRotFilelog());

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
94
  Logger::pgwc_app().startup( "Options parsed" );
gauthier's avatar
mains  
gauthier committed
95
96
97
98
99
100
101
102

  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
103
104
  smf_cfg.load(Options::getlibconfigConfig());
  smf_cfg.display();
gauthier's avatar
mains  
gauthier committed
105
106
107

  // Inter task Interface
  itti_inst = new itti_mw();
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
108
  itti_inst->start(smf_cfg.itti.itti_timer_sched_params);
gauthier's avatar
mains  
gauthier committed
109
110

  // system command
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
111
  async_shell_cmd_inst = new async_shell_cmd(smf_cfg.itti.async_cmd_sched_params);
gauthier's avatar
mains  
gauthier committed
112
113
114
115
116
117

  // PGW application layer
  pgw_app_inst = new pgw_app(Options::getlibconfigConfig());

  // PID file
  // Currently hard-coded value. TODO: add as config option.
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
118
   string pid_file_name = get_exe_absolute_path("/var/run", smf_cfg.instance);
gauthier's avatar
mains  
gauthier committed
119
120
121
122
123
124
  if (! is_pid_file_lock_success(pid_file_name.c_str())) {
    Logger::pgwc_app().error( "Lock PID file %s failed\n", pid_file_name.c_str());
    exit (-EDEADLK);
  }


Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
125
  //SMF API server
126
127
128
  Pistache::Address addr(Pistache::Ipv4::any(), Pistache::Port(8080));
  SMFApiServer smfApiServer(addr, pgw_app_inst);
  smfApiServer.init(2);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
129
130
  //smfApiServer.start();
  //smfApiServer.shutdown();
131
  std::thread smf_api_manager(&SMFApiServer::start, smfApiServer);
gauthier's avatar
mains  
gauthier committed
132
133
134
135
136
137
138
139
140
141
142

  FILE *fp = NULL;
  std::string filename = fmt::format("/tmp/spgwc_{}.status", getpid());
  fp = fopen(filename.c_str(), "w+");
  fprintf(fp, "STARTED\n");
  fflush(fp);
  fclose(fp);

  pause();
  return 0;
}