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/introductionpuzzleremover.h"
\r
11 #include "../../include/freenet/identityintroductioninserter.h"
\r
13 #include <zthread/Thread.h>
\r
19 FreenetMasterThread::FreenetMasterThread()
\r
21 std::string fcpport;
\r
23 if(Option::instance()->Get("FCPHost",m_fcphost)==false)
\r
25 m_fcphost="localhost";
\r
26 Option::instance()->Set("FCPHost",m_fcphost);
\r
28 if(Option::instance()->Get("FCPPort",fcpport)==false)
\r
31 Option::instance()->Set("FCPPort",fcpport);
\r
34 // convert fcp port to long, and make sure it's within the valid port range
\r
35 if(StringFunctions::Convert(fcpport,m_fcpport)==false)
\r
38 Option::instance()->Set("FCPPort","9481");
\r
41 m_receivednodehello=false;
\r
45 FreenetMasterThread::~FreenetMasterThread()
\r
50 const bool FreenetMasterThread::FCPConnect()
\r
52 // we were previosly connected, send FCPDisconnect to objects
\r
53 if(m_receivednodehello==true)
\r
55 for(std::vector<IFCPConnected *>::iterator i=m_fcpconnected.begin(); i!=m_fcpconnected.end(); i++)
\r
57 (*i)->FCPDisconnected();
\r
59 m_receivednodehello=false;
\r
62 m_log->WriteLog(LogFile::LOGLEVEL_INFO,__FUNCTION__" trying to connect to node "+m_fcphost);
\r
64 if(m_fcp.Connect(m_fcphost.c_str(),m_fcpport)==true)
\r
66 // send ClientHello message to node
\r
67 m_fcp.SendMessage("ClientHello",2,"Name","FMSClient","ExpectedVersion","2.0");
\r
69 m_log->WriteLog(LogFile::LOGLEVEL_INFO,__FUNCTION__" connected to node");
\r
80 const bool FreenetMasterThread::HandleMessage(FCPMessage &message)
\r
82 if(message.GetName()=="NodeHello")
\r
84 m_receivednodehello=true;
\r
86 // send connected message to all objects, must do this AFTER we received the NodeHello message
\r
87 for(std::vector<IFCPConnected *>::iterator i=m_fcpconnected.begin(); i!=m_fcpconnected.end(); i++)
\r
89 (*i)->FCPConnected();
\r
94 if(m_receivednodehello==true)
\r
97 std::vector<IFCPMessageHandler *>::iterator i=m_fcpmessagehandlers.begin();
\r
98 while(handled==false && i!=m_fcpmessagehandlers.end())
\r
100 handled=(*i)->HandleMessage(message);
\r
106 std::string info("");
\r
107 for(std::map<std::string,std::string>::iterator mi=message.begin(); mi!=message.end(); mi++)
\r
109 info+="\t\t\t\t"+(*mi).first+"="+(*mi).second+"\r\n";
\r
111 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,__FUNCTION__" received unhandled "+message.GetName()+" message. Message content :\r\n"+info);
\r
113 // if unhandled message was alldata - we must retrieve the data
\r
114 if(message.GetName()=="AllData")
\r
117 StringFunctions::Convert(message["DataLength"],length);
\r
118 while(m_fcp.Connected() && m_fcp.ReceiveBufferSize()<length)
\r
122 if(m_fcp.Connected())
\r
124 char *data=new char[length];
\r
125 m_fcp.ReceiveRaw(data,length);
\r
136 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,__FUNCTION__" received "+message.GetName()+" message before NodeHello");
\r
142 void FreenetMasterThread::RegisterFCPConnected(IFCPConnected *obj)
\r
144 m_fcpconnected.push_back(obj);
\r
147 void FreenetMasterThread::RegisterFCPMessageHandler(IFCPMessageHandler *obj)
\r
149 m_fcpmessagehandlers.push_back(obj);
\r
152 void FreenetMasterThread::RegisterPeriodicProcessor(IPeriodicProcessor *obj)
\r
154 m_processors.push_back(obj);
\r
157 void FreenetMasterThread::run()
\r
160 FCPMessage message;
\r
167 if(m_fcp.Connected()==false)
\r
169 if(FCPConnect()==false)
\r
172 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,__FUNCTION__" could not connect to node. Waiting 60 seconds.");
\r
174 // wait 60 seconds - will then try to connect again
\r
177 ZThread::Thread::sleep(60000);
\r
185 // fcp is connected
\r
190 // check for message on receive buffer and handle it
\r
191 if(m_fcp.ReceiveBufferSize()>0)
\r
194 message=m_fcp.ReceiveMessage();
\r
196 if(message.GetName()!="")
\r
198 HandleMessage(message);
\r
202 // let objects do their processing
\r
203 for(std::vector<IPeriodicProcessor *>::iterator i=m_processors.begin(); i!=m_processors.end(); i++)
\r
209 }while(!ZThread::Thread::interrupted() && done==false);
\r
211 m_fcp.Disconnect();
\r
217 void FreenetMasterThread::Setup()
\r
220 // seed random number generator
\r
223 m_registrables.push_back(new UnkeyedIDCreator(&m_fcp));
\r
224 m_registrables.push_back(new IdentityInserter(&m_fcp));
\r
225 m_registrables.push_back(new IdentityRequester(&m_fcp));
\r
226 m_registrables.push_back(new IntroductionPuzzleInserter(&m_fcp));
\r
227 m_registrables.push_back(new IdentityIntroductionRequester(&m_fcp));
\r
228 m_registrables.push_back(new IntroductionPuzzleRequester(&m_fcp));
\r
229 m_registrables.push_back(new IntroductionPuzzleRemover());
\r
230 m_registrables.push_back(new IdentityIntroductionInserter(&m_fcp));
\r
232 for(std::vector<IFreenetRegistrable *>::iterator i=m_registrables.begin(); i!=m_registrables.end(); i++)
\r
234 (*i)->RegisterWithThread(this);
\r
239 void FreenetMasterThread::Shutdown()
\r
241 // delete each registerable object
\r
242 for(std::vector<IFreenetRegistrable *>::iterator i=m_registrables.begin(); i!=m_registrables.end(); i++)
\r