Add request type(/m3u) for Vu+PlayerHD (IOS).
[vuplus_transtreamproxy] / src / Util.cpp
1 /*
2  * Utils.cpp
3  *
4  *  Created on: 2014. 6. 10.
5  *      Author: oskwon
6  */
7
8 #include <errno.h>
9 #include <stdarg.h>
10 #include <string.h>
11 #include <dirent.h>
12 #include <signal.h>
13 #include <sys/wait.h>
14
15 #include <arpa/inet.h>
16 #include <sys/socket.h>
17
18 #include <sstream>
19 #include <fstream>
20
21 #include "Util.h"
22 #include "Logger.h"
23
24 using namespace std;
25 //----------------------------------------------------------------------
26
27 std::string Util::ultostr(int64_t data)
28 {
29         std::stringstream ss;
30         ss << data;
31         return ss.str();
32 }
33 //----------------------------------------------------------------------
34
35 int Util::strtollu(std::string data)
36 {
37         long long retval;
38         std::stringstream ss;
39         try {
40                 ss.str(data);
41                 ss >> retval;
42         }
43         catch(...) {
44                 return -1;
45         }
46         return retval;
47 }
48 //----------------------------------------------------------------------
49
50 std::string Util::trim(std::string& s, const std::string& drop)
51 {
52         std::string r = s.erase(s.find_last_not_of(drop) + 1);
53         return r.erase(0, r.find_first_not_of(drop));
54 }
55 //----------------------------------------------------------------------
56
57 int Util::split(std::string data, const char delimiter, std::vector<string>& tokens)
58 {
59         std::stringstream data_stream(data);
60         for(std::string token; std::getline(data_stream, token, delimiter); tokens.push_back(trim(token)));
61         return tokens.size();
62 }
63 //----------------------------------------------------------------------
64
65 bool Util::split_key_value(std::string data, std::string delimiter, std::string &key, std::string &value)
66 {
67         int idx = data.find(delimiter);
68         if (idx == string::npos) {
69                 WARNING("split key & value (data : %s, delimiter : %s)", data.c_str(), delimiter.c_str());
70                 return false;
71         }
72         key = data.substr(0, idx);
73         value = data.substr(idx+1, data.length()-idx);
74         return true;
75 }
76 //----------------------------------------------------------------------
77
78 void Util::vlog(const char * format, ...) throw()
79 {
80         static char vlog_buffer[MAX_PRINT_LEN];
81     memset(vlog_buffer, 0, MAX_PRINT_LEN);
82
83     va_list args;
84         va_start(args, format);
85         vsnprintf(vlog_buffer, MAX_PRINT_LEN-1, format, args);
86         va_end(args);
87
88         WARNING("%s", vlog_buffer);
89 }
90 //----------------------------------------------------------------------
91
92 std::string Util::host_addr()
93 {
94         std::stringstream ss;
95     struct sockaddr_in addr;
96     socklen_t addrlen = sizeof(addr);
97
98     getpeername(0, (struct sockaddr*)&addr, &addrlen);
99     ss << inet_ntoa(addr.sin_addr);
100
101     return ss.str();
102 }
103 //-------------------------------------------------------------------------------
104
105 std::vector<int> Util::find_process_by_name(std::string name, int mypid)
106 {
107         std::vector<int> pidlist;
108         char cmdlinepath[256] = {0};
109         DIR* d = opendir("/proc");
110         if (d != 0) {
111                 struct dirent* de;
112                 while ((de = readdir(d)) != 0) {
113                         int pid = atoi(de->d_name);
114                         if (pid > 0) {
115                                 sprintf(cmdlinepath, "/proc/%s/cmdline", de->d_name);
116
117                                 std::string cmdline;
118                                 std::ifstream cmdlinefile(cmdlinepath);
119                                 std::getline(cmdlinefile, cmdline);
120                                 if (!cmdline.empty()) {
121                                         size_t pos = cmdline.find('\0');
122                                         if (pos != string::npos)
123                                         cmdline = cmdline.substr(0, pos);
124                                         pos = cmdline.rfind('/');
125                                         if (pos != string::npos)
126                                         cmdline = cmdline.substr(pos + 1);
127                                         if ((name == cmdline) && ((mypid != pid) || (mypid == 0))) {
128                                                 pidlist.push_back(pid);
129                                         }
130                                 }
131                         }
132                 }
133                 closedir(d);
134         }
135         return pidlist;
136 }
137 //-------------------------------------------------------------------------------
138
139 void Util::kill_process(int pid)
140 {
141         int result = kill(pid, SIGINT);
142         DEBUG("SEND SIGINT to %d, result : %d", pid, result);
143         sleep(1);
144 }
145 //----------------------------------------------------------------------