1 #include "../../include/freenet/freenetmasterthread.h"
\r
2 #include "../../include/option.h"
\r
3 #include "../../include/uuidgenerator.h"
\r
4 #include "../../include/stringfunctions.h"
\r
5 #include "../../include/freenet/unkeyedidcreator.h"
\r
6 #include "../../include/freenet/identityinserter.h"
\r
7 #include "../../include/freenet/identityrequester.h"
\r
8 #include "../../include/freenet/introductionpuzzleinserter.h"
\r
9 #include "../../include/freenet/identityintroductionrequester.h"
\r
10 #include "../../include/freenet/introductionpuzzlerequester.h"
\r
11 #include "../../include/freenet/identityintroductioninserter.h"
\r
12 #include "../../include/freenet/trustlistinserter.h"
\r
13 #include "../../include/freenet/trustlistrequester.h"
\r
14 #include "../../include/freenet/messagelistrequester.h"
\r
15 #include "../../include/freenet/messagerequester.h"
\r
16 #include "../../include/freenet/messageinserter.h"
\r
17 #include "../../include/freenet/messagelistinserter.h"
\r
18 #include "../../include/freenet/periodicdbmaintenance.h"
\r
19 #include "../../include/freenet/boardlistinserter.h"
\r
20 #include "../../include/freenet/boardlistrequester.h"
\r
21 #include "../../include/freenet/siteinserter.h"
\r
22 #include "../../include/freenet/fileinserter.h"
\r
23 #include "../../include/pthreadwrapper/thread.h"
\r
31 FreenetMasterThread::FreenetMasterThread()
\r
33 std::string fcpport;
\r
35 if(Option::Instance()->Get("FCPHost",m_fcphost)==false)
\r
37 m_fcphost="localhost";
\r
38 Option::Instance()->Set("FCPHost",m_fcphost);
\r
40 if(Option::Instance()->Get("FCPPort",fcpport)==false)
\r
43 Option::Instance()->Set("FCPPort",fcpport);
\r
46 // convert fcp port to long, and make sure it's within the valid port range
\r
47 if(StringFunctions::Convert(fcpport,m_fcpport)==false)
\r
50 Option::Instance()->Set("FCPPort","9481");
\r
53 m_receivednodehello=false;
\r
57 FreenetMasterThread::~FreenetMasterThread()
\r
62 const bool FreenetMasterThread::FCPConnect()
\r
64 // we were previosly connected, send FCPDisconnect to objects
\r
65 if(m_receivednodehello==true)
\r
67 for(std::vector<IFCPConnected *>::iterator i=m_fcpconnected.begin(); i!=m_fcpconnected.end(); i++)
\r
69 (*i)->FCPDisconnected();
\r
71 m_receivednodehello=false;
\r
74 m_log->WriteLog(LogFile::LOGLEVEL_INFO,"FreenetMasterThread::FCPConnect trying to connect to node "+m_fcphost);
\r
76 if(m_fcp.Connect(m_fcphost.c_str(),m_fcpport)==true)
\r
79 std::string clientname="FMSClient-"+uuid.Generate();
\r
80 // send ClientHello message to node
\r
81 m_fcp.SendMessage("ClientHello",2,"Name",clientname.c_str(),"ExpectedVersion","2.0");
\r
83 m_log->WriteLog(LogFile::LOGLEVEL_INFO,"FreenetMasterThread::FCPConnect connected to node");
\r
94 const bool FreenetMasterThread::HandleMessage(FCPMessage &message)
\r
96 if(message.GetName()=="NodeHello")
\r
98 m_receivednodehello=true;
\r
100 // send connected message to all objects, must do this AFTER we received the NodeHello message
\r
101 for(std::vector<IFCPConnected *>::iterator i=m_fcpconnected.begin(); i!=m_fcpconnected.end(); i++)
\r
103 (*i)->FCPConnected();
\r
108 if(m_receivednodehello==true)
\r
110 bool handled=false;
\r
111 std::vector<IFCPMessageHandler *>::iterator i=m_fcpmessagehandlers.begin();
\r
112 while(handled==false && i!=m_fcpmessagehandlers.end())
\r
114 handled=(*i)->HandleMessage(message);
\r
120 std::string info("");
\r
121 for(std::map<std::string,std::string>::iterator mi=message.begin(); mi!=message.end(); mi++)
\r
123 info+="\t\t\t\t"+(*mi).first+"="+(*mi).second+"\r\n";
\r
125 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"FreenetMasterThread::HandleMessage received unhandled "+message.GetName()+" message. Message content :\r\n"+info);
\r
127 // if unhandled message was alldata - we must retrieve the data
\r
128 if(message.GetName()=="AllData")
\r
131 StringFunctions::Convert(message["DataLength"],length);
\r
132 while(m_fcp.Connected() && m_fcp.ReceiveBufferSize()<length)
\r
136 if(m_fcp.Connected())
\r
138 char *data=new char[length];
\r
139 m_fcp.ReceiveRaw(data,length);
\r
150 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"FreenetMasterThread::HandleMessage received "+message.GetName()+" message before NodeHello");
\r
156 void FreenetMasterThread::RegisterFCPConnected(IFCPConnected *obj)
\r
158 m_fcpconnected.push_back(obj);
\r
161 void FreenetMasterThread::RegisterFCPMessageHandler(IFCPMessageHandler *obj)
\r
163 m_fcpmessagehandlers.push_back(obj);
\r
166 void FreenetMasterThread::RegisterPeriodicProcessor(IPeriodicProcessor *obj)
\r
168 m_processors.push_back(obj);
\r
171 void FreenetMasterThread::Run()
\r
174 DateTime lastreceivedmessage;
\r
175 DateTime lastconnected;
\r
177 FCPMessage message;
\r
180 lastconnected.SetToGMTime();
\r
181 lastconnected.Add(0,-1);
\r
183 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"FreenetMasterThread::run thread started.");
\r
189 if(m_fcp.Connected()==false)
\r
191 // wait at least 1 minute since last successful connect
\r
193 if(lastconnected<=(now-(1.0/1440.0)))
\r
195 if(FCPConnect()==false)
\r
198 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"FreenetMasterThread::run could not connect to node. Waiting 60 seconds.");
\r
200 for(int i=0; i<60 && !IsCancelled(); i++)
\r
207 lastreceivedmessage.SetToGMTime();
\r
208 lastconnected.SetToGMTime();
\r
216 // fcp is connected
\r
221 // check for message on receive buffer and handle it
\r
222 if(m_fcp.ReceiveBufferSize()>0)
\r
225 message=m_fcp.ReceiveMessage();
\r
227 if(message.GetName()!="")
\r
229 HandleMessage(message);
\r
230 lastreceivedmessage.SetToGMTime();
\r
234 // let objects do their processing
\r
235 for(std::vector<IPeriodicProcessor *>::iterator i=m_processors.begin(); i!=m_processors.end(); i++)
\r
240 // if we haven't received any messages from the node in 10 minutes, something is wrong
\r
242 if(lastreceivedmessage<(now-(1.0/144.0)))
\r
244 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"FreenetMasterThread::Run The Freenet node has not responded in 10 minutes. Trying to reconnect.");
\r
245 m_fcp.Disconnect();
\r
248 if(m_fcp.Connected()==false)
\r
250 m_log->WriteLog(LogFile::LOGLEVEL_INFO,"FreenetMasterThread::Run Disconnected from Freenet node.");
\r
254 }while(!IsCancelled() && done==false);
\r
256 m_fcp.Disconnect();
\r
260 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"FreenetMasterThread::run thread exiting.");
\r
264 void FreenetMasterThread::Setup()
\r
267 // seed random number generator
\r
270 m_registrables.push_back(new UnkeyedIDCreator(&m_fcp));
\r
271 m_registrables.push_back(new IdentityInserter(&m_fcp));
\r
272 m_registrables.push_back(new IdentityRequester(&m_fcp));
\r
273 m_registrables.push_back(new IntroductionPuzzleInserter(&m_fcp));
\r
274 m_registrables.push_back(new IdentityIntroductionRequester(&m_fcp));
\r
275 m_registrables.push_back(new IntroductionPuzzleRequester(&m_fcp));
\r
276 m_registrables.push_back(new IdentityIntroductionInserter(&m_fcp));
\r
277 m_registrables.push_back(new TrustListInserter(&m_fcp));
\r
278 m_registrables.push_back(new TrustListRequester(&m_fcp));
\r
279 m_registrables.push_back(new MessageListInserter(&m_fcp));
\r
280 m_registrables.push_back(new MessageListRequester(&m_fcp));
\r
281 m_registrables.push_back(new MessageRequester(&m_fcp));
\r
282 m_registrables.push_back(new MessageInserter(&m_fcp));
\r
283 m_registrables.push_back(new BoardListInserter(&m_fcp));
\r
284 m_registrables.push_back(new BoardListRequester(&m_fcp));
\r
285 m_registrables.push_back(new SiteInserter(&m_fcp));
\r
286 m_registrables.push_back(new FileInserter(&m_fcp));
\r
287 m_registrables.push_back(new PeriodicDBMaintenance());
\r
289 for(std::vector<IFreenetRegistrable *>::iterator i=m_registrables.begin(); i!=m_registrables.end(); i++)
\r
291 (*i)->RegisterWithThread(this);
\r
296 void FreenetMasterThread::Shutdown()
\r
298 // delete each registrable object
\r
299 for(std::vector<IFreenetRegistrable *>::iterator i=m_registrables.begin(); i!=m_registrables.end(); i++)
\r