00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <ListenSocket.h>
00023 #include <signal.h>
00024 #include "MyHandler.h"
00025 #include "TrackerSock.h"
00026 #include "MyMinderSocket.h"
00027 #include "MyMinionSocket.h"
00028
00029 #define DEB(x)
00030
00031 bool quit = false;
00032
00033
00034 void top(MyHandler& h)
00035 {
00036 std::string msg = "Tip_" + Utility::l2string(static_cast<MinderHandler&>(h).GetHostId());
00037 static_cast<MinderHandler&>(h).SendMessage(Utility::base64(msg));
00038 unlink("top_map.dot");
00039 FILE *fil;
00040 if ((fil = fopen("top_map.dot","wt")) != NULL)
00041 {
00042 fprintf(fil,"digraph \"G\" {\n");
00043 fprintf(fil,"\tedge [arrowhead=open]\n");
00044 #ifdef _WIN32
00045 std::string os = "Win32";
00046 #else
00047 std::string os = "Linux";
00048 #endif
00049 fprintf(fil,"\t\"%ld\" [label=\"%ld\\n%s\\n%s\" style=filled fillcolor=\"#e0e0e0\"]\n",
00050 static_cast<MinderHandler&>(h).GetHostId(),
00051 static_cast<MinderHandler&>(h).GetHostId(),
00052 h.GetVersion().c_str(),
00053 os.c_str());
00054 static_cast<MinderHandler&>(h).Tops(fil);
00055 fclose(fil);
00056 }
00057 }
00058
00059
00060 void sigint(int s)
00061 {
00062 fprintf(stderr,"shutting down\n");
00063 quit = true;
00064 }
00065
00066
00067 void sigpipe(int s)
00068 {
00069 }
00070
00071
00072 void reg_minder(MyHandler& h,const std::string& cmd)
00073 {
00074 MyMinderSocket *m = new MyMinderSocket(h,h.GetString("server/announce_url") );
00075 m -> Init();
00076
00077 m -> Function(cmd);
00078 m -> SetDeleteByHandler(true);
00079 m -> Open(h.GetString("minder/host"),h.GetInt("minder/port"));
00080 h.Add(m);
00081 m -> SetLocalIpPort(Utility::GetLocalAddress(),h.GetLocalPort());
00082 if (!m -> Connecting())
00083 {
00084 m -> SendHello();
00085 }
00086
00087 }
00088
00089
00090 void connect(MyHandler& h)
00091 {
00092 if (h.Count() < h.GetInt("minion/max") )
00093 {
00094 ipaddr_t a;
00095 port_t p;
00096 std::string key;
00097 long host_id;
00098 if (h.GetHost(a,p,key,host_id))
00099 {
00100 MinionSocket *tmp = new MyMinionSocket(h,key,a,p);
00101 tmp -> Init();
00102 ipaddr_t my_ip;
00103 port_t my_port;
00104 h.GetMyIpPort(my_ip, my_port);
00105 tmp -> SetMyIpPort(my_ip,my_port);
00106 tmp -> SetRemoteHostId(host_id);
00107 {
00108 std::string t;
00109 Utility::l2ip(a,t);
00110 DEB( printf("Connect to: %s:%d\n",t.c_str(),p);)
00111 }
00112 if (tmp -> Open(a,p))
00113 {
00114 tmp -> SetDeleteByHandler(true);
00115 h.Add(tmp);
00116
00117 tmp -> SendHello("Hello");
00118 }
00119 else
00120 if (tmp -> Connecting())
00121 {
00122 tmp -> SetDeleteByHandler(true);
00123
00124 h.Add(tmp);
00125 }
00126 else
00127 {
00128 delete tmp;
00129 }
00130 }
00131 else
00132 {
00133 DEB( printf("h.GetHost() failed?\n");)
00134 }
00135 }
00136 }
00137
00138
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218 int main(int argc,char *argv[])
00219 {
00220 MyHandler h( "config.xml" );
00221 ListenSocket<TrackerSock> l(h);
00222 ListenSocket<MyMinionSocket> l3(h);
00223
00224 signal(SIGINT, (__sighandler_t)sigint);
00225 signal(SIGHUP, (__sighandler_t)sigint);
00226 signal(SIGTERM,(__sighandler_t)sigint);
00227 signal(SIGPIPE, (__sighandler_t)sigpipe);
00228
00229
00230 if (h.GetString("server/type") == "normal")
00231 {
00232 std::string bind = h.GetString("server/bind");
00233 port_t port = h.GetInt("server/port");
00234 int queue_size = h.GetInt("server/queue_size");
00235 {
00236 if (l.Bind(bind,port,queue_size))
00237 {
00238 return -1;
00239 }
00240 h.Add(&l);
00241 }
00242 }
00243 else
00244 if (h.GetString("server/type") == "ssl")
00245 {
00246 printf("Tracker type not implemented: 'ssl'\n");
00247 exit(-1);
00248 }
00249 else
00250 {
00251 printf("Tracker type not supported: '%s'\n",h.GetString("server/type").c_str());
00252 exit(-1);
00253 }
00254
00255 {
00256 std::string bind = h.GetString("minion/bind");
00257 port_t port = h.GetInt("minion/port");
00258 while (l3.Bind(port))
00259 {
00260 port++;
00261 }
00262 h.Add(&l3);
00263 h.SetLocalPort(port);
00264 }
00265
00266 {
00267 time_t t = time(NULL);
00268 time_t tc = time(NULL);
00269 time_t tm = time(NULL);
00270 time_t tt = time(NULL);
00271 time_t td = time(NULL);
00272 reg_minder(h,"Hello");
00273 h.Select(1,0);
00274 while (h.GetCount() && !quit)
00275 {
00276 h.Select(1,0);
00277 time_t t2 = time(NULL);
00278 if (t2 - tc > h.GetInt("minion/connect_interval") && h.GetInt("minion/connect_interval") )
00279 {
00280 tc = t2;
00281 connect(h);
00282 }
00283 if (t2 - t > h.GetInt("minion/update_interval"))
00284 {
00285 t = t2;
00286 h.SendList();
00287 }
00288 if (t2 - tm > 15 * 60)
00289 {
00290 tm = t2;
00291 reg_minder(h,"Hello");
00292 }
00293 if (t2 - tt > h.GetInt("minion/top_interval") && h.GetInt("minion/top_interval") )
00294 {
00295 tt = t2;
00296 top(h);
00297 }
00298 if (t2 - td > h.GetInt("server/timeout_check_interval") )
00299 {
00300 td = t2;
00301 h.CheckDropped();
00302 }
00303 }
00304 }
00305
00306 {
00307 reg_minder(h,"Goodbye");
00308 h.Select(1,0);
00309 while (h.MinderSockets())
00310 {
00311 h.Select(1,0);
00312 }
00313 }
00314 }
00315
00316