1 #include "../../include/freenet/freenetmasterthread.h"
\r
2 #include "../../include/option.h"
\r
3 #include "../../include/stringfunctions.h"
\r
4 #include "../../include/freenet/unkeyedidcreator.h"
\r
5 #include "../../include/freenet/identityinserter.h"
\r
6 #include "../../include/freenet/identityrequester.h"
\r
7 #include "../../include/freenet/introductionpuzzleinserter.h"
\r
8 #include "../../include/freenet/identityintroductionrequester.h"
\r
9 #include "../../include/freenet/introductionpuzzlerequester.h"
\r
10 #include "../../include/freenet/identityintroductioninserter.h"
\r
11 #include "../../include/freenet/trustlistinserter.h"
\r
12 #include "../../include/freenet/trustlistrequester.h"
\r
13 #include "../../include/freenet/messagelistrequester.h"
\r
14 #include "../../include/freenet/messagelistinserter.h"
\r
15 #include "../../include/freenet/messagerequester.h"
\r
16 #include "../../include/freenet/messageinserter.h"
\r
17 #include "../../include/freenet/boardlistinserter.h"
\r
18 #include "../../include/freenet/boardlistrequester.h"
\r
19 #include "../../include/freenet/siteinserter.h"
\r
20 #include "../../include/freenet/fileinserter.h"
\r
22 #include <Poco/UUID.h>
\r
23 #include <Poco/UUIDGenerator.h>
\r
24 #include <Poco/DateTime.h>
\r
25 #include <Poco/Timespan.h>
\r
26 #include <Poco/Thread.h>
\r
34 FreenetMasterThread::FreenetMasterThread()
\r
36 std::string fcpport;
\r
38 if(Option::Instance()->Get("FCPHost",m_fcphost)==false)
\r
40 m_fcphost="localhost";
\r
41 Option::Instance()->Set("FCPHost",m_fcphost);
\r
43 if(Option::Instance()->Get("FCPPort",fcpport)==false)
\r
46 Option::Instance()->Set("FCPPort",fcpport);
\r
49 // convert fcp port to long, and make sure it's within the valid port range
\r
50 if(StringFunctions::Convert(fcpport,m_fcpport)==false)
\r
53 Option::Instance()->Set("FCPPort","9481");
\r
56 m_receivednodehello=false;
\r
60 FreenetMasterThread::~FreenetMasterThread()
\r
65 const bool FreenetMasterThread::FCPConnect()
\r
67 // we were previosly connected, send FCPDisconnect to objects
\r
68 if(m_receivednodehello==true)
\r
70 for(std::vector<IFCPConnected *>::iterator i=m_fcpconnected.begin(); i!=m_fcpconnected.end(); i++)
\r
72 (*i)->FCPDisconnected();
\r
74 m_receivednodehello=false;
\r
77 m_log->information("FreenetMasterThread::FCPConnect trying to connect to node "+m_fcphost);
\r
79 if(m_fcp.Connect(m_fcphost.c_str(),m_fcpport)==true)
\r
81 Poco::UUIDGenerator uuidgen;
\r
86 uuid=uuidgen.createRandom();
\r
90 m_log->fatal("FreenetMasterThread::FCPConnect could not generate UUID");
\r
93 std::string clientname="FMSClient-"+uuid.toString();
\r
94 // send ClientHello message to node
\r
95 m_fcp.SendMessage("ClientHello",2,"Name",clientname.c_str(),"ExpectedVersion","2.0");
\r
97 m_log->information("FreenetMasterThread::FCPConnect connected to node");
\r
108 const bool FreenetMasterThread::HandleMessage(FCPMessage &message)
\r
110 if(message.GetName()=="NodeHello")
\r
112 m_receivednodehello=true;
\r
114 // send connected message to all objects, must do this AFTER we received the NodeHello message
\r
115 for(std::vector<IFCPConnected *>::iterator i=m_fcpconnected.begin(); i!=m_fcpconnected.end(); i++)
\r
117 (*i)->FCPConnected();
\r
122 if(m_receivednodehello==true)
\r
124 bool handled=false;
\r
125 std::vector<IFCPMessageHandler *>::iterator i=m_fcpmessagehandlers.begin();
\r
126 while(handled==false && i!=m_fcpmessagehandlers.end())
\r
128 handled=(*i)->HandleMessage(message);
\r
134 std::string info("");
\r
135 for(std::map<std::string,std::string>::iterator mi=message.begin(); mi!=message.end(); mi++)
\r
137 info+="\t\t\t\t"+(*mi).first+"="+(*mi).second+"\r\n";
\r
139 m_log->debug("FreenetMasterThread::HandleMessage received unhandled "+message.GetName()+" message. Message content :\r\n"+info);
\r
141 // if unhandled message was alldata - we must retrieve the data
\r
142 if(message.GetName()=="AllData")
\r
145 StringFunctions::Convert(message["DataLength"],length);
\r
146 while(m_fcp.Connected() && m_fcp.ReceiveBufferSize()<length)
\r
150 if(m_fcp.Connected())
\r
152 char *data=new char[length];
\r
153 m_fcp.ReceiveRaw(data,length);
\r
164 m_log->error("FreenetMasterThread::HandleMessage received "+message.GetName()+" message before NodeHello");
\r
170 void FreenetMasterThread::RegisterFCPConnected(IFCPConnected *obj)
\r
172 m_fcpconnected.push_back(obj);
\r
175 void FreenetMasterThread::RegisterFCPMessageHandler(IFCPMessageHandler *obj)
\r
177 m_fcpmessagehandlers.push_back(obj);
\r
180 void FreenetMasterThread::RegisterPeriodicProcessor(IPeriodicProcessor *obj)
\r
182 m_processors.push_back(obj);
\r
185 void FreenetMasterThread::run()
\r
188 Poco::DateTime lastreceivedmessage;
\r
189 Poco::DateTime lastconnected;
\r
190 Poco::DateTime now;
\r
191 FCPMessage message;
\r
194 lastconnected-=Poco::Timespan(0,0,1,0,0);
\r
196 m_log->debug("FreenetMasterThread::run thread started.");
\r
202 if(m_fcp.Connected()==false)
\r
204 // wait at least 1 minute since last successful connect
\r
205 now=Poco::Timestamp();
\r
206 if(lastconnected<=(now-Poco::Timespan(0,0,1,0,0)))
\r
208 if(FCPConnect()==false)
\r
211 m_log->error("FreenetMasterThread::run could not connect to node. Waiting 60 seconds.");
\r
213 for(int i=0; i<60 && !IsCancelled(); i++)
\r
215 Poco::Thread::sleep(1000);
\r
220 lastreceivedmessage=Poco::Timestamp();
\r
221 lastconnected=Poco::Timestamp();
\r
226 Poco::Thread::sleep(1000);
\r
229 // fcp is connected
\r
234 // check for message on receive buffer and handle it
\r
235 if(m_fcp.ReceiveBufferSize()>0)
\r
238 message=m_fcp.ReceiveMessage();
\r
240 if(message.GetName()!="")
\r
242 HandleMessage(message);
\r
243 lastreceivedmessage=Poco::Timestamp();
\r
247 // let objects do their processing
\r
248 for(std::vector<IPeriodicProcessor *>::iterator i=m_processors.begin(); i!=m_processors.end(); i++)
\r
253 // if we haven't received any messages from the node in 10 minutes, something is wrong
\r
254 now=Poco::Timestamp();
\r
255 if(lastreceivedmessage<(now-Poco::Timespan(0,0,10,0,0)))
\r
257 m_log->error("FreenetMasterThread::Run The Freenet node has not responded in 10 minutes. Trying to reconnect.");
\r
258 m_fcp.Disconnect();
\r
261 if(m_fcp.Connected()==false)
\r
263 m_log->information("FreenetMasterThread::Run Disconnected from Freenet node.");
\r
267 }while(!IsCancelled() && done==false);
\r
269 m_fcp.Disconnect();
\r
273 m_log->debug("FreenetMasterThread::run thread exiting.");
\r
277 void FreenetMasterThread::Setup()
\r
280 // seed random number generator
\r
283 m_registrables.push_back(new UnkeyedIDCreator(&m_fcp));
\r
284 m_registrables.push_back(new IdentityInserter(&m_fcp));
\r
285 m_registrables.push_back(new IdentityRequester(&m_fcp));
\r
286 m_registrables.push_back(new IntroductionPuzzleInserter(&m_fcp));
\r
287 m_registrables.push_back(new IdentityIntroductionRequester(&m_fcp));
\r
288 m_registrables.push_back(new IntroductionPuzzleRequester(&m_fcp));
\r
289 m_registrables.push_back(new IdentityIntroductionInserter(&m_fcp));
\r
290 m_registrables.push_back(new TrustListInserter(&m_fcp));
\r
291 m_registrables.push_back(new TrustListRequester(&m_fcp));
\r
292 m_registrables.push_back(new MessageListInserter(&m_fcp));
\r
293 m_registrables.push_back(new MessageListRequester(&m_fcp));
\r
294 m_registrables.push_back(new MessageInserter(&m_fcp));
\r
295 m_registrables.push_back(new MessageRequester(&m_fcp));
\r
296 m_registrables.push_back(new BoardListInserter(&m_fcp));
\r
297 m_registrables.push_back(new BoardListRequester(&m_fcp));
\r
298 m_registrables.push_back(new SiteInserter(&m_fcp));
\r
299 m_registrables.push_back(new FileInserter(&m_fcp));
\r
301 for(std::vector<IFreenetRegistrable *>::iterator i=m_registrables.begin(); i!=m_registrables.end(); i++)
\r
303 (*i)->RegisterWithThread(this);
\r
308 void FreenetMasterThread::Shutdown()
\r
310 // delete each registrable object
\r
311 for(std::vector<IFreenetRegistrable *>::iterator i=m_registrables.begin(); i!=m_registrables.end(); i++)
\r