Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/webitel/mod_grpc
Browse files Browse the repository at this point in the history
  • Loading branch information
navrotskyj committed Jun 7, 2022
2 parents 04e2c6e + a49d286 commit cc6080f
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 40 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ set(GENERATED_PROTOBUF_FILES ${FS_PB_H_FILE} ${FS_PB_CPP_FILE}
${FS_GRPC_PB_H_FILE} ${FS_GRPC_P_CPP_FILE})


add_library(mod_grpc SHARED src/mod_grpc.h src/mod_grpc.cpp src/Call.cpp src/Call.h src/CallManager.cpp src/CallManager.h src/Cluster.cpp src/Cluster.h ${FS_PB_CPP_FILE} ${FS_PB_H_FILE} ${FS_GRPC_PB_CPP_FILE} ${FS_GRPC_PB_H_FILE})
add_library(mod_grpc SHARED src/mod_grpc.h src/mod_grpc.cpp src/Call.cpp src/Call.h src/CallManager.cpp src/CallManager.h src/Cluster.cpp src/Cluster.h ${FS_PB_CPP_FILE} ${FS_PB_H_FILE} ${FS_GRPC_PB_CPP_FILE} ${FS_GRPC_PB_H_FILE} src/utils.h)
target_include_directories(mod_grpc PRIVATE ${GENERATED_PROTOBUF_PATH} ${FREESWITCH_INCLUDE_DIR} )
target_link_libraries(mod_grpc PRIVATE ${_PROTOBUF_LIBPROTOBUF} ${_GRPC_GRPCPP_UNSECURE}
-pthread)
Expand Down
8 changes: 2 additions & 6 deletions src/Call.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <iterator>
#include <ctime>
#include <iostream>
#include "utils.h"

extern "C" {
#include <switch.h>
Expand Down Expand Up @@ -73,11 +74,6 @@ static const char* callEventStr(CallActions e) {
}
}

static long int unixTimestamp() {
switch_time_t ts = switch_micro_time_now();
return static_cast<long int> (ts/1000);
}

class CallEndpoint {
public:
std::string type;
Expand Down Expand Up @@ -117,7 +113,7 @@ class BaseCallEvent {
switch_event_add_header_string(out, SWITCH_STACK_BOTTOM, HEADER_NAME_ID, uuid_.c_str());
switch_event_add_header_string(out, SWITCH_STACK_BOTTOM, HEADER_NAME_NODE_NAME, node_.c_str());
switch_event_add_header_string(out, SWITCH_STACK_BOTTOM, HEADER_NAME_DOMAIN_ID, domain_id_.c_str());
switch_event_add_header_string(out, SWITCH_STACK_BOTTOM, HEADER_NAME_TIMESTAMP, std::to_string(unixTimestamp()).c_str());
switch_event_add_header_string(out, SWITCH_STACK_BOTTOM, HEADER_NAME_TIMESTAMP, std::to_string(mod_grpc::unixTimestamp()).c_str());

if (!user_id_.empty()) {
switch_event_add_header_string(out, SWITCH_STACK_BOTTOM, HEADER_NAME_USER_ID, user_id_.c_str());
Expand Down
1 change: 1 addition & 0 deletions src/Cluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ extern "C" {
#include <condition_variable>
#include "switch_curl.h"
#include "CallManager.h"
#include "utils.h"

namespace mod_grpc {
static long sendRequest(const char *uri, const char *body);
Expand Down
104 changes: 71 additions & 33 deletions src/mod_grpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ namespace mod_grpc {
switch_core_session_rwunlock(session);
} else {
reply->mutable_error()->set_type(fs::ErrorExecute_Type_ERROR);
reply->mutable_error()->set_message("no such channel");
reply->mutable_error()->set_message("No such channel!");
}

return Status::OK;
Expand Down Expand Up @@ -754,7 +754,12 @@ namespace mod_grpc {
}

std::string toJson(const PushData *data) {
return "{\"call_id\":\"" + data->call_id + "\"}";
return "{\"call_id\":\"" + data->call_id + "\",\"from_number\":\"" + data->from_number + "\",\"from_name\":\"" + data->from_name + "\"}";
}

static size_t writeCallback(char *contents, size_t size, size_t nmemb, void *userp) {
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}

long ServerImpl::SendPushAPN(const char *devices, const PushData *data) {
Expand All @@ -768,6 +773,9 @@ namespace mod_grpc {
switch_curl_easy_setopt(cli, CURLOPT_URL, (this->push_apn_uri + "/" + std::string(devices)).c_str());
headers = switch_curl_slist_append(headers, "Content-Type: application/json");
headers = switch_curl_slist_append(headers, this->push_apn_topic.c_str());
headers = switch_curl_slist_append(headers, (std::string("apns-expiration: ") +
std::to_string((long)((unixTimestamp() + this->push_wait_callback + 2000) / 1000))
).c_str());

switch_curl_easy_setopt(cli, CURLOPT_HTTPHEADER, headers);

Expand All @@ -792,21 +800,28 @@ namespace mod_grpc {
/* disconnect if we cannot validate server's cert */
curl_easy_setopt(cli, CURLOPT_SSL_VERIFYPEER, 1L);

//#ifdef DEBUG_CURL
#ifdef DEBUG_CURL
switch_curl_easy_setopt(cli, CURLOPT_VERBOSE, 1L);
//#endif
#endif
switch_curl_easy_setopt(cli, CURLOPT_CUSTOMREQUEST, "POST");

switch_curl_easy_setopt(cli, CURLOPT_POSTFIELDS, body.c_str());

switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
"body\n%s\n", body.c_str());
// std::string readBuffer;
// switch_curl_easy_setopt(cli, CURLOPT_WRITEFUNCTION, writeCallback);
// switch_curl_easy_setopt(cli, CURLOPT_WRITEDATA, &readBuffer);

// switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
// "body\n%s\n", body.c_str());

res = switch_curl_easy_perform(cli);
if(res == CURLE_OK) {
curl_easy_getinfo(cli, CURLINFO_RESPONSE_CODE, &response_code);
}

// switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
// "response (%s)\n%s\n",(this->push_apn_uri + "/" + std::string(devices)).c_str(), readBuffer.c_str());

switch_curl_easy_cleanup(cli);
switch_curl_slist_free_all(headers);
};
Expand Down Expand Up @@ -866,55 +881,78 @@ namespace mod_grpc {
return this->push_wait_callback;
}

static switch_status_t wbt_outgoing_channel(switch_core_session_t * session) {
switch_channel_t *channel = switch_core_session_get_channel(session);
const char *wbt_push_fcm = switch_channel_get_variable(channel, "wbt_push_fcm");
const char *wbt_push_apn = switch_channel_get_variable(channel, "wbt_push_apn");
static void split_str(std::string const &str, const char delim,
std::vector<std::string> &out) {
// create a stream from the string
std::stringstream s(str);

std::string s2;
while (std::getline(s, s2, delim)) {
out.push_back(s2); // store the string in s2
}
}

static switch_status_t wbt_outgoing_channel(switch_core_session_t * session, switch_event_t * event, switch_caller_profile_t * cp, switch_core_session_t * peer_session, switch_originate_flag_t flag) {
if (!peer_session) {

return SWITCH_STATUS_SUCCESS;
}

switch_channel_t *channel = switch_core_session_get_channel(peer_session);
auto dir = switch_event_get_header(event, "sip_h_X-Webitel-Direction");
if (!dir || strcmp(dir, "internal") != 0) {

return SWITCH_STATUS_SUCCESS;
}
auto uuid = switch_channel_get_uuid(channel);
if (!uuid) {
//todo
return SWITCH_STATUS_SUCCESS;
}

switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(peer_session), SWITCH_LOG_DEBUG, "checking tweaks for %s\n", uuid);
const char *wbt_push_fcm = switch_event_get_header(event, "wbt_push_fcm");
const char *wbt_push_apn = switch_event_get_header(event, "wbt_push_apn");
const char *wbt_from_name = switch_event_get_header(event, "wbt_from_name");
const char *wbt_from_number = switch_event_get_header(event, "wbt_from_number");
int send = 0;
PushData data;
data.call_id = std::string(uuid);
data.from_name = wbt_from_name ? std::string(wbt_from_name) : "";
data.from_number = wbt_from_number ? std::string(wbt_from_number) : "";
if (wbt_push_fcm && server_->UseFCM()) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "start request FCM %s\n", switch_channel_get_uuid(channel));
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(peer_session), SWITCH_LOG_DEBUG, "start request FCM %s\n", uuid);
auto res = server_->SendPushFCM(wbt_push_fcm, &data);
//switch_sleep(1 * 1000 * 1000);
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "stop request FCM %s [%ld]\n", switch_channel_get_uuid(channel), res);
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(peer_session), SWITCH_LOG_DEBUG, "stop request FCM %s [%ld]\n", uuid, res);
if (res == 200) {
send++;
}
}
if (wbt_push_apn && server_->UseAPN()) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "start APN request %s\n", switch_channel_get_uuid(channel));
auto res = server_->SendPushAPN(wbt_push_apn, &data);
//switch_sleep(1 * 1000 * 1000);
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "stop APN request %s [%ld]\n", switch_channel_get_uuid(channel), res);
if (res == 200) {
send++;
std::vector <std::string> out;
split_str(wbt_push_apn, ',', out);
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(peer_session), SWITCH_LOG_DEBUG, "start APN request %s tokens[%s]\n", uuid, wbt_push_apn);
for (const auto &token: out) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(peer_session), SWITCH_LOG_DEBUG, "start APN request %s\n", uuid);
auto res = server_->SendPushAPN(token.c_str(), &data);
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(peer_session), SWITCH_LOG_DEBUG, "stop APN request %s [%ld]\n", uuid, res);
if (res == 200) {
send++;
}
}
}

if (send && server_->PushWaitCallback() > 0) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "start wait callback %s [%d]\n", switch_channel_get_uuid(channel), server_->PushWaitCallback());
// switch_sleep(3 * 1000 * 1000);
//switch_channel_wait_for_app_flag(channel, CF_APP_T38_POSSIBLE, "T38", SWITCH_TRUE, 2000)
//todo
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(peer_session), SWITCH_LOG_DEBUG, "start wait callback %s [%d]\n", uuid, server_->PushWaitCallback());
switch_channel_wait_for_flag(channel, CF_SLA_INTERCEPT, SWITCH_TRUE, server_->PushWaitCallback(), NULL); //10s
switch_channel_clear_flag(channel, CF_SLA_INTERCEPT);
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "stop wait callback %s\n", switch_channel_get_uuid(channel));
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(peer_session), SWITCH_LOG_DEBUG, "stop wait callback %s\n", uuid);
}
return SWITCH_STATUS_SUCCESS;
}

static switch_status_t wbt_tweaks_on_init(switch_core_session_t *session) {
switch_channel_t *channel = switch_core_session_get_channel(session);
auto dir = switch_channel_get_variable(channel, "sip_h_X-Webitel-Direction");

if (dir && strcmp(dir, "internal") == 0) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "checking tweaks for %s\n", switch_channel_get_uuid(channel));
wbt_outgoing_channel(session);
}

// switch_core_event_hook_add_outgoing_channel(session, wbt_outgoing_channel);
switch_core_event_hook_add_outgoing_channel(session, wbt_outgoing_channel);
return SWITCH_STATUS_SUCCESS;
}

Expand Down
16 changes: 16 additions & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// Created by root on 3/10/22.
//

#ifndef MOD_GRPC_UTILS_H
#define MOD_GRPC_UTILS_H

#include <switch.h>

namespace mod_grpc {
static long int unixTimestamp() {
switch_time_t ts = switch_micro_time_now();
return static_cast<long int> (ts/1000);
}
}
#endif //MOD_GRPC_UTILS_H

0 comments on commit cc6080f

Please sign in to comment.