X-Git-Url: https://git.pterodactylus.net/?p=fms.git;a=blobdiff_plain;f=src%2Fglobal.cpp;h=42a4726ed834fcba41c71cec1f095bb2798fcfb2;hp=48c3842f9d3800b895f3e9d42213f48d154bd47a;hb=f60495a029c54358f82956482fe203fe2b7b5b23;hpb=964f55fd550fc711c0320ce6a24ad713040695d0 diff --git a/src/global.cpp b/src/global.cpp index 48c3842..42a4726 100644 --- a/src/global.cpp +++ b/src/global.cpp @@ -6,6 +6,8 @@ #include "../include/db/sqlite3db.h" #include "../include/freenet/freenetmasterthread.h" #include "../include/nntp/nntplistener.h" +#include "../include/http/httpthread.h" +#include "../include/threadcontroller.h" #ifdef _WIN32 #include @@ -15,15 +17,44 @@ #include #endif +bool wantshutdown=false; + void SetupDB() { DateTime date; - SQLite3DB::DB *db=SQLite3DB::DB::instance(); + SQLite3DB::DB *db=SQLite3DB::DB::Instance(); db->Open("fms.db3"); db->SetBusyTimeout(10000); // set timeout to 10 seconds db->Execute("VACUUM;"); + + // TODO remove this - temp fix for problem in 0.1.8 + db->Execute("DELETE FROM tblMessageBoard WHERE MessageID NOT IN (SELECT MessageID FROM tblMessage);"); + + db->Execute("CREATE TABLE IF NOT EXISTS tblDBVersion(\ + Major INTEGER,\ + Minor INTEGER\ + );"); + + SQLite3DB::Statement st=db->Prepare("SELECT Major,Minor FROM tblDBVersion;"); + st.Step(); + if(st.RowReturned()) + { + int major; + int minor; + st.ResultInt(0,major); + st.ResultInt(1,minor); + st.Finalize(); + if(major==1 && minor==0) + { + ConvertDB0100To0101(); + } + } + else + { + db->Execute("INSERT INTO tblDBVersion(Major,Minor) VALUES(1,1);"); + } db->Execute("CREATE TABLE IF NOT EXISTS tblOption(\ Option TEXT UNIQUE,\ @@ -34,8 +65,8 @@ void SetupDB() db->Execute("CREATE TABLE IF NOT EXISTS tblLocalIdentity(\ LocalIdentityID INTEGER PRIMARY KEY,\ Name TEXT,\ - PublicKey TEXT,\ - PrivateKey TEXT,\ + PublicKey TEXT UNIQUE,\ + PrivateKey TEXT UNIQUE,\ SingleUse BOOL CHECK(SingleUse IN('true','false')) DEFAULT 'false',\ PublishTrustList BOOL CHECK(PublishTrustList IN('true','false')) DEFAULT 'false',\ PublishBoardList BOOL CHECK(PublishBoardList IN('true','false')) DEFAULT 'false',\ @@ -84,7 +115,7 @@ void SetupDB() db->Execute("CREATE TABLE IF NOT EXISTS tblIdentity(\ IdentityID INTEGER PRIMARY KEY,\ - PublicKey TEXT,\ + PublicKey TEXT UNIQUE,\ Name TEXT,\ SingleUse BOOL CHECK(SingleUse IN('true','false')) DEFAULT 'false',\ PublishTrustList BOOL CHECK(PublishTrustList IN('true','false')) DEFAULT 'false',\ @@ -156,7 +187,7 @@ void SetupDB() db->Execute("CREATE TABLE IF NOT EXISTS tblMessageReplyTo(\ MessageID INTEGER,\ - ReplyToMessageUUID INTEGER,\ + ReplyToMessageUUID TEXT,\ ReplyOrder INTEGER\ );"); @@ -196,11 +227,23 @@ void SetupDB() Inserted BOOL CHECK(Inserted IN('true','false')) DEFAULT 'false'\ );"); + db->Execute("CREATE TABLE IF NOT EXISTS tblAdministrationBoard(\ + BoardID INTEGER UNIQUE,\ + ModifyLocalMessageTrust INTEGER,\ + ModifyLocalTrustListTrust INTEGER\ + );"); + + // MessageInserter will insert a record into this temp table which the MessageListInserter will query for and insert a MessageList when needed + db->Execute("CREATE TEMPORARY TABLE IF NOT EXISTS tmpMessageListInsert(\ + LocalIdentityID INTEGER,\ + Date DATETIME\ + );"); + // low / high / message count for each board db->Execute("CREATE VIEW IF NOT EXISTS vwBoardStats AS \ SELECT tblBoard.BoardID AS 'BoardID', IFNULL(MIN(MessageID),0) AS 'LowMessageID', IFNULL(MAX(MessageID),0) AS 'HighMessageID', COUNT(MessageID) AS 'MessageCount' \ FROM tblBoard LEFT JOIN tblMessageBoard ON tblBoard.BoardID=tblMessageBoard.BoardID \ - WHERE MessageID>=0 \ + WHERE MessageID>=0 OR MessageID IS NULL \ GROUP BY tblBoard.BoardID;"); // calculates peer trust @@ -210,8 +253,19 @@ void SetupDB() ROUND(SUM(TrustListTrust*(LocalTrustListTrust/100.0))/SUM(LocalTrustListTrust/100.0),0) AS 'PeerTrustListTrust' \ FROM tblPeerTrust INNER JOIN tblIdentity ON tblPeerTrust.IdentityID=tblIdentity.IdentityID \ WHERE LocalTrustListTrust>=(SELECT OptionValue FROM tblOption WHERE Option='MinLocalTrustListTrust') \ + AND ( PeerTrustListTrust IS NULL OR PeerTrustListTrust>=(SELECT OptionValue FROM tblOption WHERE Option='MinPeerTrustListTrust') ) \ GROUP BY TargetIdentityID;"); + /* + These peer trust calculations are too CPU intensive to be triggers - they were called every time a new trust list was processed + All trust levels will now be recalculated every hour in the PeriodicDBMaintenance class + */ + // drop existing triggers + db->Execute("DROP TRIGGER IF EXISTS trgDeleteOntblPeerTrust;"); + db->Execute("DROP TRIGGER IF EXISTS trgInsertOntblPeerTrust;"); + db->Execute("DROP TRIGGER IF EXISTS trgUpdateOntblPeerTrust;"); + db->Execute("DROP TRIGGER IF EXISTS trgUpdateLocalTrustLevels;"); +/* // update PeerTrustLevel when deleting a record from tblPeerTrust db->Execute("CREATE TRIGGER IF NOT EXISTS trgDeleteOntblPeerTrust AFTER DELETE ON tblPeerTrust \ FOR EACH ROW \ @@ -240,6 +294,7 @@ void SetupDB() BEGIN \ UPDATE tblIdentity SET PeerMessageTrust=(SELECT PeerMessageTrust FROM vwCalculatedPeerTrust WHERE TargetIdentityID=IdentityID), PeerTrustListTrust=(SELECT PeerTrustListTrust FROM vwCalculatedPeerTrust WHERE TargetIdentityID=IdentityID);\ END;"); +*/ db->Execute("CREATE TRIGGER IF NOT EXISTS trgDeleteMessage AFTER DELETE ON tblMessage \ FOR EACH ROW \ @@ -279,6 +334,44 @@ void SetupDB() db->Execute("DELETE FROM tblIntroductionPuzzleInserts WHERE Day<='"+date.Format("%Y-%m-%d")+"';"); db->Execute("DELETE FROM tblIntroductionPuzzleRequests WHERE Day<='"+date.Format("%Y-%m-%d")+"';"); + date.SetToGMTime(); + // insert SomeDude's public key + db->Execute("INSERT INTO tblIdentity(PublicKey,DateAdded) VALUES('SSK@NuBL7aaJ6Cn4fB7GXFb9Zfi8w1FhPyW3oKgU9TweZMw,iXez4j3qCpd596TxXiJgZyTq9o-CElEuJxm~jNNZAuA,AQACAAE/','"+date.Format("%Y-%m-%d %H:%M:%S")+"');"); + // insert Shadow Panther's public key + db->Execute("INSERT INTO tblIdentity(PublicKey,DateAdded) VALUES('SSK@~mimyB1kmH4f7Cgsd2wM2Qv2NxrZHRMM6IY8~7EWRVQ,fxTKkR0TYhgMYb-vEGAv55sMOxCGD2xhE4ZxWHxdPz4,AQACAAE/','"+date.Format("%Y-%m-%d %H:%M:%S")+"');"); + // insert garfield's public key + db->Execute("INSERT INTO tblIdentity(PublicKey,DateAdded) VALUES('SSK@T8l1IEGU4-PoASFzgc2GYhIgRzUvZsKdoQWeuLHuTmM,QLxAPfkGis8l5NafNpSCdbxzXhBlu9WL8svcqJw9Mpo,AQACAAE/','"+date.Format("%Y-%m-%d %H:%M:%S")+"');"); + +} + +void ConvertDB0100To0101() +{ + // added unique constraint to public and private key + SQLite3DB::DB *db=SQLite3DB::DB::Instance(); + db->Execute("CREATE TEMPORARY TABLE tblLocalIdentityTemp AS SELECT * FROM tblLocalIdentity;"); + db->Execute("DROP TABLE IF EXISTS tblLocalIdentity;"); + db->Execute("CREATE TABLE IF NOT EXISTS tblLocalIdentity(\ + LocalIdentityID INTEGER PRIMARY KEY,\ + Name TEXT,\ + PublicKey TEXT UNIQUE,\ + PrivateKey TEXT UNIQUE,\ + SingleUse BOOL CHECK(SingleUse IN('true','false')) DEFAULT 'false',\ + PublishTrustList BOOL CHECK(PublishTrustList IN('true','false')) DEFAULT 'false',\ + PublishBoardList BOOL CHECK(PublishBoardList IN('true','false')) DEFAULT 'false',\ + InsertingIdentity BOOL CHECK(InsertingIdentity IN('true','false')) DEFAULT 'false',\ + LastInsertedIdentity DATETIME,\ + InsertingPuzzle BOOL CHECK(InsertingPuzzle IN('true','false')) DEFAULT 'false',\ + LastInsertedPuzzle DATETIME,\ + InsertingTrustList BOOL CHECK(InsertingTrustList IN('true','false')) DEFAULT 'false',\ + LastInsertedTrustList DATETIME,\ + InsertingBoardList BOOL CHECK(InsertingBoardList IN('true','false')) DEFAULT 'false',\ + LastInsertedBoardList DATETIME,\ + InsertingMessageList BOOL CHECK(InsertingMessageList IN('true','false')) DEFAULT 'false',\ + LastInsertedMessageList DATETIME\ + );"); + db->Execute("INSERT INTO tblLocalIdentity SELECT * FROM tblLocalIdentityTemp;"); + db->Execute("DROP TABLE IF EXISTS tblLocalIdentityTemp;"); + db->Execute("UPDATE tblDBVersion SET Major=1, Minor=1;"); } void SetupDefaultOptions() @@ -286,7 +379,7 @@ void SetupDefaultOptions() // OptionValue should always be inserted as a string, even if the option really isn't a string - just to keep the field data type consistent std::ostringstream tempstr; // must set tempstr to "" between db inserts - SQLite3DB::DB *db=SQLite3DB::DB::instance(); + SQLite3DB::DB *db=SQLite3DB::DB::Instance(); SQLite3DB::Statement st=db->Prepare("INSERT INTO tblOption(Option,OptionValue,OptionDescription) VALUES(?,?,?);"); // LogLevel @@ -321,7 +414,19 @@ void SetupDefaultOptions() // StartNNTP st.Bind(0,"StartNNTP"); st.Bind(1,"true"); - st.Bind(2,"Start NNTP service."); + st.Bind(2,"Start NNTP server."); + st.Step(); + st.Reset(); + + st.Bind(0,"StartHTTP"); + st.Bind(1,"true"); + st.Bind(2,"Start HTTP server."); + st.Step(); + st.Reset(); + + st.Bind(0,"HTTPListenPort"); + st.Bind(1,"8080"); + st.Bind(2,"Port HTTP server will listen on."); st.Step(); st.Reset(); @@ -334,7 +439,7 @@ void SetupDefaultOptions() // FCPHost st.Bind(0,"FCPHost"); - st.Bind(1,"localhost"); + st.Bind(1,"127.0.0.1"); st.Bind(2,"Host name or address of Freenet node."); st.Step(); st.Reset(); @@ -394,24 +499,42 @@ void SetupDefaultOptions() st.Step(); st.Reset(); + st.Bind(0,"MinPeerMessageTrust"); + st.Bind(1,"30"); + st.Bind(2,"Specifies a peer message trust level that a peer must have before its messages will be downloaded."); + st.Step(); + st.Reset(); + st.Bind(0,"MinLocalTrustListTrust"); - st.Bind(1,"50"); + st.Bind(1,"51"); st.Bind(2,"Specifies a local trust list trust level that a peer must have before its trust list will be included in the weighted average. Any peers below this number will be excluded from the results."); st.Step(); st.Reset(); + st.Bind(0,"MinPeerTrustListTrust"); + st.Bind(1,"30"); + st.Bind(2,"Specifies a peer trust list trust level that a peer must have before its trust list will be included in the weighted average. Any peers below this number will be excluded from the results."); + st.Step(); + st.Reset(); + st.Bind(0,"MessageDownloadMaxDaysBackward"); - st.Bind(1,"3"); + st.Bind(1,"5"); st.Bind(2,"The maximum number of days backward that messages will be downloaded from each identity"); st.Step(); st.Reset(); st.Bind(0,"MessageListDaysBackward"); - st.Bind(1,"3"); + st.Bind(1,"5"); st.Bind(2,"The number of days backward that messages you have inserted will appear in your MessageLists"); st.Step(); st.Reset(); + st.Bind(0,"MaxPeerMessagesPerDay"); + st.Bind(1,"200"); + st.Bind(2,"The maximum number of messages you will download from each peer on a given day."); + st.Step(); + st.Reset(); + } void SetupLogFile() @@ -422,23 +545,23 @@ void SetupLogFile() date.SetToGMTime(); - LogFile::instance()->SetFileName("fms-"+date.Format("%Y-%m-%d")+".log"); - LogFile::instance()->OpenFile(); - LogFile::instance()->SetWriteNewLine(true); - LogFile::instance()->SetWriteDate(true); - LogFile::instance()->SetWriteLogLevel(true); + LogFile::Instance()->SetFileName("fms-"+date.Format("%Y-%m-%d")+".log"); + LogFile::Instance()->OpenFile(); + LogFile::Instance()->SetWriteNewLine(true); + LogFile::Instance()->SetWriteDate(true); + LogFile::Instance()->SetWriteLogLevel(true); - if(Option::instance()->Get("LogLevel",configval)==false) + if(Option::Instance()->Get("LogLevel",configval)==false) { configval="4"; - Option::instance()->Set("LogLevel",configval); + Option::Instance()->Set("LogLevel",configval); } if(StringFunctions::Convert(configval,loglevel)==false) { loglevel=LogFile::LOGLEVEL_DEBUG; - Option::instance()->Set("LogLevel",loglevel); + Option::Instance()->Set("LogLevel",loglevel); } - LogFile::instance()->SetLogLevel((LogFile::LogLevel)loglevel); + LogFile::Instance()->SetLogLevel((LogFile::LogLevel)loglevel); } void SetupNetwork() @@ -449,6 +572,16 @@ void SetupNetwork() #endif } +void Shutdown() +{ + ThreadController::Instance()->ShutdownThreads(); + + ShutdownNetwork(); + + LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_INFO,"FMS shutdown"); + LogFile::Instance()->WriteNewLine(); +} + void ShutdownNetwork() { #ifdef _WIN32 @@ -456,27 +589,26 @@ void ShutdownNetwork() #endif } -void ShutdownThreads(std::vector &threads) +void SigHandler(int signum) +{ + Shutdown(); + exit(0); +} + +/* +void ShutdownThreads(std::vector &threads) { - std::vector::iterator i; + std::vector::iterator i; for(i=threads.begin(); i!=threads.end(); i++) { - if((*i)->wait(1)==false) - { - try - { - (*i)->interrupt(); - } - catch(...) - { - } - } + (*i)->Cancel(); } for(i=threads.begin(); i!=threads.end(); i++) { - LogFile::instance()->WriteLog(LogFile::LOGLEVEL_DEBUG,"ShutdownThreads waiting for thread to exit."); - (*i)->wait(); + LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_DEBUG,"ShutdownThreads waiting for thread to exit."); + //(*i)->wait(); + (*i)->Join(); delete (*i); } @@ -484,33 +616,47 @@ void ShutdownThreads(std::vector &threads) } -void StartThreads(std::vector &threads) +void StartThreads(std::vector &threads) { std::string startfreenet; std::string startnntp; + std::string starthttp; - if(Option::instance()->Get("StartFreenetUpdater",startfreenet)==false) + if(Option::Instance()->Get("StartFreenetUpdater",startfreenet)==false) { startfreenet="true"; - Option::instance()->Set("StartFreenetUpdater","true"); + Option::Instance()->Set("StartFreenetUpdater","true"); } - if(Option::instance()->Get("StartNNTP",startnntp)==false) + if(Option::Instance()->Get("StartNNTP",startnntp)==false) { startnntp="true"; - Option::instance()->Set("StartNNTP","true"); + Option::Instance()->Set("StartNNTP","true"); + } + + if(Option::Instance()->Get("StartHTTP",starthttp)==false) + { + starthttp="true"; + Option::Instance()->Set("StartHTTP","true"); } if(startfreenet=="true") { - ZThread::Thread *t=new ZThread::Thread(new FreenetMasterThread()); + PThread::Thread *t=new PThread::Thread(new FreenetMasterThread()); threads.push_back(t); } if(startnntp=="true") { - ZThread::Thread *t=new ZThread::Thread(new NNTPListener()); + PThread::Thread *t=new PThread::Thread(new NNTPListener()); + threads.push_back(t); + } + + if(starthttp=="true") + { + PThread::Thread *t=new PThread::Thread(new HTTPThread()); threads.push_back(t); } } +*/