1 | |
---|
2 | #include <lsf-1.0/utils.hpp> |
---|
3 | #include <lsf-1.0/system.hpp> |
---|
4 | #include <lsf-1.0/network.hpp> |
---|
5 | #include <lsf-1.0/n4d.hpp> |
---|
6 | #include <lsf-1.0/filesystem.hpp> |
---|
7 | |
---|
8 | #include <xmlrpc-c/base.hpp> |
---|
9 | #include <signal.h> |
---|
10 | |
---|
11 | #include <iostream> |
---|
12 | #include <vector> |
---|
13 | #include <cstdlib> |
---|
14 | #include <cstring> |
---|
15 | |
---|
16 | using namespace net::lliurex; |
---|
17 | using namespace std; |
---|
18 | |
---|
19 | bool quit_request=false; |
---|
20 | |
---|
21 | void signal_handler(int sig) |
---|
22 | { |
---|
23 | quit_request=true; |
---|
24 | } |
---|
25 | |
---|
26 | int main(int argc,char * argv[]) |
---|
27 | { |
---|
28 | |
---|
29 | |
---|
30 | |
---|
31 | struct sigaction action; |
---|
32 | memset(&action, 0, sizeof(struct sigaction)); |
---|
33 | action.sa_handler = signal_handler; |
---|
34 | sigaction(SIGTERM, &action, NULL); |
---|
35 | |
---|
36 | memset(&action, 0, sizeof(struct sigaction)); |
---|
37 | action.sa_handler = signal_handler; |
---|
38 | sigaction(SIGINT, &action, NULL); |
---|
39 | |
---|
40 | vector<string> options; |
---|
41 | |
---|
42 | for(int n=1;n<argc;n++) |
---|
43 | { |
---|
44 | options.push_back(string(argv[n])); |
---|
45 | } |
---|
46 | |
---|
47 | for(int n=0;n<options.size();n++) |
---|
48 | { |
---|
49 | if(options[n]=="utils") |
---|
50 | { |
---|
51 | cout<<"*** testing utils ***"<<endl; |
---|
52 | |
---|
53 | cout<<"*******************"<<endl; |
---|
54 | cout<<"utils::str::Split:"<<endl; |
---|
55 | cout<<"*******************"<<endl; |
---|
56 | |
---|
57 | vector<string> stack = utils::str::Split(" hello world ",' '); |
---|
58 | |
---|
59 | for(int n=0;n<stack.size();n++) |
---|
60 | { |
---|
61 | cout<<"["<<stack[n]<<"]"<<endl; |
---|
62 | } |
---|
63 | |
---|
64 | cout<<"*******************"<<endl; |
---|
65 | cout<<"utils::str::Strip:"<<endl; |
---|
66 | cout<<"*******************"<<endl; |
---|
67 | |
---|
68 | string stripped = utils::str::Strip(" oh\nyeah \n"); |
---|
69 | cout<<"["<<stripped<<"]"<<endl; |
---|
70 | |
---|
71 | cout<<"*******************"<<endl; |
---|
72 | cout<<"utils::str::Replace:"<<endl; |
---|
73 | cout<<"*******************"<<endl; |
---|
74 | |
---|
75 | string replaced = utils::str::Replace("It is time to {0} and chew {1}","{0}","kick butts"); |
---|
76 | replaced = utils::str::Replace(replaced,"{1}","bubble gum"); |
---|
77 | replaced = utils::str::Replace(replaced,"butts","ass"); |
---|
78 | cout<<"["<<replaced<<"]"<<endl; |
---|
79 | |
---|
80 | cout<<"*******************"<<endl; |
---|
81 | cout<<"utils::parse::IsInteger:"<<endl; |
---|
82 | cout<<"*******************"<<endl; |
---|
83 | |
---|
84 | string num="234"; |
---|
85 | bool isnum = utils::parse::IsInteger(num); |
---|
86 | cout<<"- status: "<<num<<"->"<<isnum<<endl; |
---|
87 | |
---|
88 | num="f234"; |
---|
89 | isnum = utils::parse::IsInteger(num); |
---|
90 | cout<<"- status: "<<num<<"->"<<isnum<<endl; |
---|
91 | |
---|
92 | |
---|
93 | cout<<"*******************"<<endl; |
---|
94 | cout<<"utils::parse::IsFloat:"<<endl; |
---|
95 | cout<<"*******************"<<endl; |
---|
96 | |
---|
97 | num="-3.14"; |
---|
98 | isnum = utils::parse::IsFloat(num); |
---|
99 | cout<<"- status: "<<num<<"->"<<isnum<<endl; |
---|
100 | |
---|
101 | num="45.e"; |
---|
102 | isnum = utils::parse::IsFloat(num); |
---|
103 | cout<<"- status: "<<num<<"->"<<isnum<<endl; |
---|
104 | |
---|
105 | |
---|
106 | } |
---|
107 | |
---|
108 | if(options[n]=="system") |
---|
109 | { |
---|
110 | cout<<"*** testing system ***"<<endl; |
---|
111 | |
---|
112 | |
---|
113 | cout<<"*******************"<<endl; |
---|
114 | cout<<"system::GetProcessList:"<<endl; |
---|
115 | cout<<"*******************"<<endl; |
---|
116 | |
---|
117 | vector<unsigned int > plist; |
---|
118 | |
---|
119 | plist=system::GetProcessList(); |
---|
120 | |
---|
121 | for(int n=0;n<plist.size();n++) |
---|
122 | { |
---|
123 | system::ProcessInfo pinfo = system::GetProcessInfo(plist[n]); |
---|
124 | if(pinfo.cmdline=="") |
---|
125 | cout<<"* "<<plist[n]<<": ["<<pinfo.comm<<"]"<<endl; |
---|
126 | else |
---|
127 | cout<<"* "<<plist[n]<<": "<<pinfo.cmdline<<endl; |
---|
128 | } |
---|
129 | |
---|
130 | |
---|
131 | cout<<"*******************"<<endl; |
---|
132 | cout<<"system::GetModuleList:"<<endl; |
---|
133 | cout<<"*******************"<<endl; |
---|
134 | vector<string> mlist; |
---|
135 | |
---|
136 | mlist=system::GetModuleList(); |
---|
137 | |
---|
138 | for(int n=0;n<mlist.size();n++) |
---|
139 | { |
---|
140 | cout<<"- "<<mlist[n]<<endl; |
---|
141 | } |
---|
142 | |
---|
143 | cout<<"*******************"<<endl; |
---|
144 | cout<<"system::GetMountList:"<<endl; |
---|
145 | cout<<"*******************"<<endl; |
---|
146 | vector<system::MountInfo> minfo; |
---|
147 | |
---|
148 | minfo=system::GetMountList(); |
---|
149 | |
---|
150 | for(int n=0;n<minfo.size();n++) |
---|
151 | { |
---|
152 | cout<<"* device: "<<minfo[n].device<<" on "<<minfo[n].mount_point<<" type: "<<minfo[n].type<<endl; |
---|
153 | } |
---|
154 | |
---|
155 | cout<<"*******************"<<endl; |
---|
156 | cout<<"system::GetSystemMemory:"<<endl; |
---|
157 | cout<<"system::GetFreeSystemMemory:"<<endl; |
---|
158 | cout<<"*******************"<<endl; |
---|
159 | cout<<"System memory: "<<system::GetSystemMemory()/1024<<"KB"<<endl; |
---|
160 | cout<<"Free system memory: "<<system::GetFreeSystemMemory()/1024<<"KB"<<endl; |
---|
161 | |
---|
162 | }// system |
---|
163 | |
---|
164 | if(options[n]=="network") |
---|
165 | { |
---|
166 | cout<<"*******************"<<endl; |
---|
167 | cout<<"network::GetDeviceList:"<<endl; |
---|
168 | cout<<"*******************"<<endl; |
---|
169 | |
---|
170 | vector<network::DeviceInfo> dlist; |
---|
171 | |
---|
172 | dlist=network::GetDeviceList(); |
---|
173 | |
---|
174 | for(int n=0;n<dlist.size();n++) |
---|
175 | { |
---|
176 | cout<<"* interface: "<<dlist[n].name<<endl; |
---|
177 | cout<<"\t-MAC:"<<network::MACToString(dlist[n].mac)<<endl; |
---|
178 | cout<<"\t-address:"<<network::AddressToString(dlist[n].address)<<endl; |
---|
179 | cout<<"\t-link:"<<dlist[n].link<<endl; |
---|
180 | cout<<"\t-promisc:"<<dlist[n].promisc<<endl; |
---|
181 | |
---|
182 | } |
---|
183 | }//network |
---|
184 | |
---|
185 | if(options[n]=="n4d") |
---|
186 | { |
---|
187 | cout<<"*******************"<<endl; |
---|
188 | cout<<"n4d::GetVariableList:"<<endl; |
---|
189 | cout<<"*******************"<<endl; |
---|
190 | |
---|
191 | xmlrpc_c::value_struct ret_struct(n4d::GetVariableList("https://anubis:9779")); |
---|
192 | |
---|
193 | map<string,xmlrpc_c::value>ret = ret_struct; |
---|
194 | map<string,xmlrpc_c::value>::iterator iter; |
---|
195 | |
---|
196 | for(iter=ret.begin();iter!=ret.end();iter++) |
---|
197 | { |
---|
198 | xmlrpc_c::value_struct val(iter->second); |
---|
199 | map<string,xmlrpc_c::value>val_map=val; |
---|
200 | |
---|
201 | if(val_map["value"].type()==xmlrpc_c::value::TYPE_STRING) |
---|
202 | { |
---|
203 | xmlrpc_c::value_string strv(val_map["value"]); |
---|
204 | cout<<"* item: "<<iter->first<<" --> "<<((string)strv)<<endl; |
---|
205 | } |
---|
206 | else cout<<"* item: "<<iter->first<<" type of "<<val_map["value"].type()<<endl; |
---|
207 | |
---|
208 | } |
---|
209 | |
---|
210 | cout<<"*******************"<<endl; |
---|
211 | cout<<"n4d::SetVariable:"<<endl; |
---|
212 | cout<<"*******************"<<endl; |
---|
213 | xmlrpc_c::value_string var_set("lliurex was here"); |
---|
214 | n4d::SetVariable("VARIABLE2",var_set,"cless","lliurex","https://anubis:9779"); |
---|
215 | |
---|
216 | cout<<"*******************"<<endl; |
---|
217 | cout<<"n4d::GetVariable:"<<endl; |
---|
218 | cout<<"*******************"<<endl; |
---|
219 | |
---|
220 | xmlrpc_c::value var_ret=n4d::GetVariable("VARIABLE2","https://anubis:9779"); |
---|
221 | |
---|
222 | if(var_ret.type()==xmlrpc_c::value::TYPE_STRING) |
---|
223 | { |
---|
224 | xmlrpc_c::value_string vstr(var_ret); |
---|
225 | cout<<"* value: "<<((string)vstr)<<endl; |
---|
226 | } |
---|
227 | |
---|
228 | |
---|
229 | |
---|
230 | |
---|
231 | |
---|
232 | }//n4d |
---|
233 | |
---|
234 | if(options[n]=="filesystem") |
---|
235 | { |
---|
236 | |
---|
237 | cout<<"*******************"<<endl; |
---|
238 | cout<<"filesystem::GetWorkingDir:"<<endl; |
---|
239 | cout<<"filesystem::Exists:"<<endl; |
---|
240 | cout<<"filesystem::IsDir:"<<endl; |
---|
241 | cout<<"filesystem::ListDir:"<<endl; |
---|
242 | cout<<"filesystem::List:"<<endl; |
---|
243 | cout<<"filesystem::DirName:"<<endl; |
---|
244 | cout<<"filesystem::BaseName:"<<endl; |
---|
245 | cout<<"filesystem::Home:"<<endl; |
---|
246 | cout<<"filesystem::CreateDir:"<<endl; |
---|
247 | cout<<"*******************"<<endl; |
---|
248 | |
---|
249 | cout<<"Working dir: "<<filesystem::GetWorkingDir()<<endl; |
---|
250 | |
---|
251 | if(filesystem::Exists("/tmp")) |
---|
252 | { |
---|
253 | vector<string> files; |
---|
254 | files=filesystem::ListDir("/tmp"); |
---|
255 | |
---|
256 | for(int n=0;n<files.size();n++) |
---|
257 | { |
---|
258 | if(filesystem::IsDir("/tmp/"+files[n])) |
---|
259 | cout<<"\tDir: "<<files[n]<<endl; |
---|
260 | else |
---|
261 | cout<<"\tFile: "<<files[n]<<endl; |
---|
262 | } |
---|
263 | |
---|
264 | cout<<"Home: "<<filesystem::Home()<<endl; |
---|
265 | filesystem::CreateDir("/tmp/alpha/bravo/charlie",true); |
---|
266 | |
---|
267 | |
---|
268 | } |
---|
269 | else |
---|
270 | { |
---|
271 | cout<<"/tmp does not exists in the filesystem"<<endl; |
---|
272 | } |
---|
273 | |
---|
274 | vector<string> match; |
---|
275 | match=filesystem::List("/*/*.conf"); |
---|
276 | string dirname=""; |
---|
277 | for(int n=0;n<match.size();n++) |
---|
278 | { |
---|
279 | string dname = filesystem::DirName(match[n]); |
---|
280 | if(dname!=dirname) |
---|
281 | { |
---|
282 | dirname=dname; |
---|
283 | cout<<dirname<<":"<<endl; |
---|
284 | } |
---|
285 | cout<<"\t"<<filesystem::BaseName(match[n])<<endl; |
---|
286 | |
---|
287 | |
---|
288 | } |
---|
289 | |
---|
290 | }//filesystem |
---|
291 | |
---|
292 | if(options[n]=="filesystem-notify") |
---|
293 | { |
---|
294 | cout<<"*******************"<<endl; |
---|
295 | cout<<"filesystem::NotifyHandler:"<<endl; |
---|
296 | cout<<"*******************"<<endl; |
---|
297 | filesystem::NotifyHandler nh("/tmp"); |
---|
298 | |
---|
299 | while(!quit_request) |
---|
300 | { |
---|
301 | nh.Push(); |
---|
302 | } |
---|
303 | }//filesystem-notify |
---|
304 | } |
---|
305 | |
---|
306 | |
---|
307 | |
---|
308 | } |
---|