X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ffreenet%2Ffreenetmasterthread.cpp;h=9c75a06f0e62bb6881c4074278c24a227b78212d;hb=59a5414ec47a2932a7802fcd1d98c4d80166564f;hp=4d16043326041a26271a60df1d0c936b90fe18dd;hpb=76805933f794915a72b7f0a21b12af6654759f4f;p=fms.git diff --git a/src/freenet/freenetmasterthread.cpp b/src/freenet/freenetmasterthread.cpp index 4d16043..9c75a06 100644 --- a/src/freenet/freenetmasterthread.cpp +++ b/src/freenet/freenetmasterthread.cpp @@ -4,6 +4,7 @@ #include "../../include/freenet/unkeyedidcreator.h" #include "../../include/freenet/identityinserter.h" #include "../../include/freenet/identityrequester.h" +#include "../../include/freenet/unknownidentityrequester.h" #include "../../include/freenet/introductionpuzzleinserter.h" #include "../../include/freenet/identityintroductionrequester.h" #include "../../include/freenet/introductionpuzzlerequester.h" @@ -11,6 +12,7 @@ #include "../../include/freenet/trustlistinserter.h" #include "../../include/freenet/trustlistrequester.h" #include "../../include/freenet/messagelistrequester.h" +#include "../../include/freenet/inactivemessagelistrequester.h" #include "../../include/freenet/messagelistinserter.h" #include "../../include/freenet/messagerequester.h" #include "../../include/freenet/messageinserter.h" @@ -32,29 +34,8 @@ #include #endif -FreenetMasterThread::FreenetMasterThread() +FreenetMasterThread::FreenetMasterThread():m_receivednodehello(false) { - std::string fcpport; - - if(Option::Instance()->Get("FCPHost",m_fcphost)==false) - { - m_fcphost="localhost"; - Option::Instance()->Set("FCPHost",m_fcphost); - } - if(Option::Instance()->Get("FCPPort",fcpport)==false) - { - fcpport="9481"; - Option::Instance()->Set("FCPPort",fcpport); - } - - // convert fcp port to long, and make sure it's within the valid port range - if(StringFunctions::Convert(fcpport,m_fcpport)==false) - { - m_fcpport=9481; - Option::Instance()->Set("FCPPort","9481"); - } - - m_receivednodehello=false; } @@ -93,7 +74,7 @@ const bool FreenetMasterThread::FCPConnect() std::string clientname="FMSClient-"+uuid.toString(); // send ClientHello message to node - m_fcp.SendMessage("ClientHello",2,"Name",clientname.c_str(),"ExpectedVersion","2.0"); + m_fcp.Send(FCPv2::Message("ClientHello",2,"Name",clientname.c_str(),"ExpectedVersion","2.0")); m_log->information("FreenetMasterThread::FCPConnect connected to node"); @@ -106,7 +87,7 @@ const bool FreenetMasterThread::FCPConnect() } -const bool FreenetMasterThread::HandleMessage(FCPMessage &message) +const bool FreenetMasterThread::HandleMessage(FCPv2::Message &message) { if(message.GetName()=="NodeHello") { @@ -123,6 +104,7 @@ const bool FreenetMasterThread::HandleMessage(FCPMessage &message) if(m_receivednodehello==true) { bool handled=false; + std::vector::iterator i=m_fcpmessagehandlers.begin(); while(handled==false && i!=m_fcpmessagehandlers.end()) { @@ -133,7 +115,7 @@ const bool FreenetMasterThread::HandleMessage(FCPMessage &message) if(handled==false) { std::string info(""); - for(std::map::iterator mi=message.begin(); mi!=message.end(); mi++) + for(std::map::iterator mi=message.GetFields().begin(); mi!=message.GetFields().end(); mi++) { info+="\t\t\t\t"+(*mi).first+"="+(*mi).second+"\r\n"; } @@ -144,15 +126,11 @@ const bool FreenetMasterThread::HandleMessage(FCPMessage &message) { long length; StringFunctions::Convert(message["DataLength"],length); - while(m_fcp.Connected() && m_fcp.ReceiveBufferSize()0) { - char *data=new char[length]; - m_fcp.ReceiveRaw(data,length); - delete [] data; + m_fcp.ReceiveIgnore(length); } } } @@ -189,81 +167,96 @@ void FreenetMasterThread::run() Poco::DateTime lastreceivedmessage; Poco::DateTime lastconnected; Poco::DateTime now; - FCPMessage message; + FCPv2::Message message; bool done=false; lastconnected-=Poco::Timespan(0,0,1,0,0); m_log->debug("FreenetMasterThread::run thread started."); + LoadDatabase(); + + + Setup(); do { - if(m_fcp.Connected()==false) + try { - // wait at least 1 minute since last successful connect - now=Poco::Timestamp(); - if(lastconnected<=(now-Poco::Timespan(0,0,1,0,0))) + if(m_fcp.IsConnected()==false) { - if(FCPConnect()==false) + // wait at least 1 minute since last successful connect + now=Poco::Timestamp(); + if(lastconnected<=(now-Poco::Timespan(0,0,1,0,0))) { + if(FCPConnect()==false) + { - m_log->error("FreenetMasterThread::run could not connect to node. Waiting 60 seconds."); + m_log->error("FreenetMasterThread::run could not connect to node. Waiting 60 seconds."); - for(int i=0; i<60 && !IsCancelled(); i++) + for(int i=0; i<60 && !IsCancelled(); i++) + { + Poco::Thread::sleep(1000); + } + } + else { - Poco::Thread::sleep(1000); + lastreceivedmessage=Poco::Timestamp(); + lastconnected=Poco::Timestamp(); } } else { - lastreceivedmessage=Poco::Timestamp(); - lastconnected=Poco::Timestamp(); + Poco::Thread::sleep(1000); } } + // fcp is connected else { - Poco::Thread::sleep(1000); - } - } - // fcp is connected - else - { - m_fcp.Update(1); + m_fcp.Update(1000); - // check for message on receive buffer and handle it - if(m_fcp.ReceiveBufferSize()>0) - { - message.Reset(); - message=m_fcp.ReceiveMessage(); + // check for message on receive buffer and handle it + if(m_fcp.MessageReady()==true) + { + message.Clear(); + m_fcp.Receive(message); + + if(message.GetName()!="") + { + HandleMessage(message); + lastreceivedmessage=Poco::Timestamp(); + } + } - if(message.GetName()!="") + // let objects do their processing + for(std::vector::iterator i=m_processors.begin(); i!=m_processors.end(); i++) { - HandleMessage(message); - lastreceivedmessage=Poco::Timestamp(); + (*i)->Process(); } - } - // let objects do their processing - for(std::vector::iterator i=m_processors.begin(); i!=m_processors.end(); i++) - { - (*i)->Process(); - } + // if we haven't received any messages from the node in 10 minutes, something is wrong + now=Poco::Timestamp(); + if(lastreceivedmessage<(now-Poco::Timespan(0,0,10,0,0))) + { + m_log->error("FreenetMasterThread::Run The Freenet node has not responded in 10 minutes. Trying to reconnect."); + m_fcp.Disconnect(); + } - // if we haven't received any messages from the node in 10 minutes, something is wrong - now=Poco::Timestamp(); - if(lastreceivedmessage<(now-Poco::Timespan(0,0,10,0,0))) - { - m_log->error("FreenetMasterThread::Run The Freenet node has not responded in 10 minutes. Trying to reconnect."); - m_fcp.Disconnect(); - } + if(m_fcp.IsConnected()==false) + { + m_log->information("FreenetMasterThread::Run Disconnected from Freenet node."); + } - if(m_fcp.Connected()==false) - { - m_log->information("FreenetMasterThread::Run Disconnected from Freenet node."); } - + } + catch(Poco::Exception &e) + { + m_log->error("FreenetMasterThread::run caught exception : "+e.displayText()); + } + catch(...) + { + m_log->error("FreenetMasterThread::run caught unknown exception"); } }while(!IsCancelled() && done==false); @@ -278,27 +271,49 @@ void FreenetMasterThread::run() void FreenetMasterThread::Setup() { + Option option(m_db); + if(option.Get("FCPHost",m_fcphost)==false) + { + m_fcphost="localhost"; + option.Set("FCPHost",m_fcphost); + } + if(option.GetInt("FCPPort",m_fcpport)==false) + { + m_fcpport=9481; + option.Set("FCPPort",m_fcpport); + } + else + { + if(m_fcpport<1 || m_fcpport>65535) + { + m_fcpport=9481; + option.Set("FCPPort",m_fcpport); + } + } + // seed random number generator srand(time(NULL)); - m_registrables.push_back(new UnkeyedIDCreator(&m_fcp)); - m_registrables.push_back(new IdentityInserter(&m_fcp)); - m_registrables.push_back(new IdentityRequester(&m_fcp)); - m_registrables.push_back(new IntroductionPuzzleInserter(&m_fcp)); - m_registrables.push_back(new IdentityIntroductionRequester(&m_fcp)); - m_registrables.push_back(new IntroductionPuzzleRequester(&m_fcp)); - m_registrables.push_back(new IdentityIntroductionInserter(&m_fcp)); - m_registrables.push_back(new TrustListInserter(&m_fcp)); - m_registrables.push_back(new TrustListRequester(&m_fcp)); - m_registrables.push_back(new MessageListInserter(&m_fcp)); - m_registrables.push_back(new MessageListRequester(&m_fcp)); - m_registrables.push_back(new MessageInserter(&m_fcp)); - m_registrables.push_back(new MessageRequester(&m_fcp)); - m_registrables.push_back(new BoardListInserter(&m_fcp)); - m_registrables.push_back(new BoardListRequester(&m_fcp)); - m_registrables.push_back(new SiteInserter(&m_fcp)); - m_registrables.push_back(new FileInserter(&m_fcp)); - m_registrables.push_back(new FMSVersionRequester(&m_fcp)); + m_registrables.push_back(new UnkeyedIDCreator(m_db,&m_fcp)); + m_registrables.push_back(new IdentityInserter(m_db,&m_fcp)); + m_registrables.push_back(new IdentityRequester(m_db,&m_fcp)); + m_registrables.push_back(new UnknownIdentityRequester(m_db,&m_fcp)); + m_registrables.push_back(new IntroductionPuzzleInserter(m_db,&m_fcp)); + m_registrables.push_back(new IdentityIntroductionRequester(m_db,&m_fcp)); + m_registrables.push_back(new IntroductionPuzzleRequester(m_db,&m_fcp)); + m_registrables.push_back(new IdentityIntroductionInserter(m_db,&m_fcp)); + m_registrables.push_back(new TrustListInserter(m_db,&m_fcp)); + m_registrables.push_back(new TrustListRequester(m_db,&m_fcp)); + m_registrables.push_back(new MessageListInserter(m_db,&m_fcp)); + m_registrables.push_back(new MessageListRequester(m_db,&m_fcp)); + m_registrables.push_back(new InactiveMessageListRequester(m_db,&m_fcp)); + m_registrables.push_back(new MessageInserter(m_db,&m_fcp)); + m_registrables.push_back(new MessageRequester(m_db,&m_fcp)); + m_registrables.push_back(new BoardListInserter(m_db,&m_fcp)); + m_registrables.push_back(new BoardListRequester(m_db,&m_fcp)); + m_registrables.push_back(new SiteInserter(m_db,&m_fcp)); + m_registrables.push_back(new FileInserter(m_db,&m_fcp)); + m_registrables.push_back(new FMSVersionRequester(m_db,&m_fcp)); for(std::vector::iterator i=m_registrables.begin(); i!=m_registrables.end(); i++) {