Open-Transactions  0.93.0-ge03d287
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
opentxs::OTServer Class Reference

#include <OTServer.hpp>

Public Member Functions

EXPORT OTServer ()
 
EXPORT ~OTServer ()
 
EXPORT void Init (bool readOnly=false)
 
bool IsFlaggedForShutdown () const
 
bool GetConnectInfo (OTString &hostname, int32_t &port) const
 
const OTPseudonymGetServerNym () const
 
EXPORT void ActivateCron ()
 
void ProcessCron ()
 

Friends

class Transactor
 
class MessageProcessor
 
class UserCommandProcessor
 
class MainFile
 
class PayDividendVisitor
 
class Notary
 

Detailed Description

Definition at line 155 of file OTServer.hpp.

Constructor & Destructor Documentation

opentxs::OTServer::OTServer ( )

Definition at line 355 of file OTServer.cpp.

356  : mainFile_(this)
357  , notary_(this)
358  , transactor_(this)
359  , userCommandProcessor_(this)
360  , m_bReadOnly(false)
361  , m_bShutdownFlag(false)
362  , m_pServerContract()
363 {
364 }
opentxs::OTServer::~OTServer ( )

Definition at line 366 of file OTServer.cpp.

367 {
368  // PID -- Set it to 0 in the lock file so the next time we run OT, it knows
369  // there isn't
370  // another copy already running (otherwise we might wind up with two copies
371  // trying to write
372  // to the same data folder simultaneously, which could corrupt the data...)
373  //
374  // OTLog::vError("m_strDataPath: %s\n", m_strDataPath.Get());
375  // OTLog::vError("SERVER_PID_FILENAME: %s\n", SERVER_PID_FILENAME);
376 
377  OTString strDataPath;
378  const bool bGetDataFolderSuccess = OTDataFolder::Get(strDataPath);
379  if (!m_bReadOnly && bGetDataFolderSuccess) {
380  OTString strPIDPath;
381  OTPaths::AppendFile(strPIDPath, strDataPath, SERVER_PID_FILENAME);
382 
383  std::ofstream pid_outfile(strPIDPath.Get());
384 
385  if (pid_outfile.is_open()) {
386  uint32_t the_pid = 0;
387  pid_outfile << the_pid;
388  pid_outfile.close();
389  }
390  else
391  OTLog::vError("Failed trying to open data locking file (to wipe "
392  "PID back to 0): %s\n",
393  strPIDPath.Get());
394  }
395 }
static EXPORT void vError(const char *szError,...)
Definition: OTLog.cpp:800
#define SERVER_PID_FILENAME
Definition: OTServer.cpp:185
static EXPORT bool AppendFile(OTString &out_strPath, const OTString &strBasePath, const OTString &strFileName)
Definition: OTPaths.cpp:1245
static EXPORT OTString Get()

Member Function Documentation

void opentxs::OTServer::ActivateCron ( )

Definition at line 300 of file OTServer.cpp.

301 {
302  OTLog::vOutput(1, "OTServer::ActivateCron: %s \n",
303  m_Cron.ActivateCron() ? "(STARTED)" : "FAILED");
304 }
bool ActivateCron()
Definition: OTCron.hpp:225
static EXPORT void vOutput(int32_t nVerbosity, const char *szOutput,...)
Definition: OTLog.cpp:768
bool opentxs::OTServer::GetConnectInfo ( OTString hostname,
int32_t &  port 
) const

Definition at line 849 of file OTServer.cpp.

850 {
851  if (!m_pServerContract) return false;
852 
853  return m_pServerContract->GetConnectInfo(strHostname, nPort);
854 }
const OTPseudonym & opentxs::OTServer::GetServerNym ( ) const

Definition at line 345 of file OTServer.cpp.

346 {
347  return m_nymServer;
348 }
void opentxs::OTServer::Init ( bool  readOnly = false)

Definition at line 397 of file OTServer.cpp.

398 {
399  m_bReadOnly = readOnly;
400 
402  OTLog::vError("Unable to Init data folders!");
403  OT_FAIL;
404  }
405  if (!ConfigLoader::load(m_strWalletFilename)) {
406  OTLog::vError("Unable to Load Config File!");
407  OT_FAIL;
408  }
409 
410  OTString dataPath;
411  bool bGetDataFolderSuccess = OTDataFolder::Get(dataPath);
412 
413  // PID -- Make sure we're not running two copies of OT on the same data
414  // simultaneously here.
415  if (bGetDataFolderSuccess) {
416  // If we want to WRITE to the data location, then we can't be in
417  // read-only mode.
418  if (!readOnly) {
419  // 1. READ A FILE STORING THE PID. (It will already exist, if OT is
420  // already running.)
421  //
422  // We open it for reading first, to see if it already exists. If it
423  // does, we read the number. 0 is fine, since we overwrite with 0 on
424  // shutdown. But any OTHER number means OT is still running. Or it
425  // means it was killed while running and didn't shut down properly,
426  // and that you need to delete the pid file by hand before running
427  // OT again. (This is all for the purpose of preventing two copies
428  // of OT running at the same time and corrupting the data folder.)
429  //
430  OTString strPIDPath;
431  OTPaths::AppendFile(strPIDPath, dataPath, SERVER_PID_FILENAME);
432 
433  std::ifstream pid_infile(strPIDPath.Get());
434 
435  // 2. (IF FILE EXISTS WITH ANY PID INSIDE, THEN DIE.)
436  if (pid_infile.is_open()) {
437  uint32_t old_pid = 0;
438  pid_infile >> old_pid;
439  pid_infile.close();
440 
441  // There was a real PID in there.
442  if (old_pid != 0) {
443  uint64_t lPID = old_pid;
445  "\n\n\nIS OPEN-TRANSACTIONS ALREADY RUNNING?\n\n"
446  "I found a PID (%llu) in the data lock file, located "
447  "at: %s\n\n"
448  "If the OT process with PID %llu is truly not running "
449  "anymore, "
450  "then just ERASE THAT FILE and then RESTART.\n",
451  lPID, strPIDPath.Get(), lPID);
452  exit(-1);
453  }
454  // Otherwise, though the file existed, the PID within was 0.
455  // (Meaning the previous instance of OT already set it to 0 as
456  // it was shutting down.)
457  }
458  // Next let's record our PID to the same file, so other copies of OT
459  // can't trample on US.
460 
461  // 3. GET THE CURRENT (ACTUAL) PROCESS ID.
462  uint64_t the_pid = 0;
463 
464 #ifdef _WIN32
465  the_pid = GetCurrentProcessId();
466 #else
467  the_pid = getpid();
468 #endif
469 
470  // 4. OPEN THE FILE IN WRITE MODE, AND SAVE THE PID TO IT.
471  std::ofstream pid_outfile(strPIDPath.Get());
472 
473  if (pid_outfile.is_open()) {
474  pid_outfile << the_pid;
475  pid_outfile.close();
476  }
477  else {
478  OTLog::vError("Failed trying to open data locking file (to "
479  "store PID %llu): %s\n",
480  the_pid, strPIDPath.Get());
481  }
482  }
483  }
485 
486  // Load up the transaction number and other OTServer data members.
487  bool mainFileExists = m_strWalletFilename.Exists()
488  ? OTDB::Exists(".", m_strWalletFilename.Get())
489  : false;
490 
491  if (!mainFileExists) {
492  if (readOnly) {
494  "Error: Main file non-existent (%s). "
495  "Plus, unable to create, since read-only flag is set.\n",
496  m_strWalletFilename.Get());
497  OT_FAIL;
498  }
499  else {
500  std::string strContract;
501  std::string strServerID;
502  std::string strCert;
503  std::string strNymID;
504  std::string strCachedKey;
505  askInteractively(strContract, strServerID, strCert, strNymID,
506  strCachedKey);
507  mainFileExists = mainFile_.CreateMainFile(
508  strContract, strServerID, strCert, strNymID, strCachedKey);
509  }
510  }
511 
512  if (mainFileExists) {
513  if (!mainFile_.LoadMainFile(readOnly)) {
514  OTLog::vError("Error in Loading Main File!\n");
515  OT_FAIL;
516  }
517  }
518 
519  // With the Server's private key loaded, and the latest transaction number
520  // loaded, and all the various other data (contracts, etc) the server is now
521  // ready for operation!
522 }
static EXPORT void vError(const char *szError,...)
Definition: OTLog.cpp:800
#define SERVER_PID_FILENAME
Definition: OTServer.cpp:185
#define OTDB_DEFAULT_PACKER
Definition: OTStorage.hpp:167
EXPORT bool InitDefaultStorage(StorageType eStoreType, PackType ePackType)
Definition: OTStorage.cpp:491
static EXPORT bool IsInitialized()
EXPORT bool Exists() const
Definition: OTString.cpp:1035
static bool load(OTString &walletFilename)
bool LoadMainFile(bool readOnly=false)
Definition: MainFile.cpp:380
#define OT_FAIL
Definition: Assert.hpp:139
EXPORT const char * Get() const
Definition: OTString.cpp:1045
EXPORT bool Exists(std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")
Definition: OTStorage.cpp:584
bool CreateMainFile(const std::string &strContract, const std::string &strServerID, const std::string &strCert, const std::string &strNymID, const std::string &strCachedKey)
Definition: MainFile.cpp:279
#define OTDB_DEFAULT_STORAGE
Definition: OTStorage.hpp:169
static EXPORT bool AppendFile(OTString &out_strPath, const OTString &strBasePath, const OTString &strFileName)
Definition: OTPaths.cpp:1245
static EXPORT OTString Get()
bool opentxs::OTServer::IsFlaggedForShutdown ( ) const

Definition at line 350 of file OTServer.cpp.

351 {
352  return m_bShutdownFlag;
353 }
void opentxs::OTServer::ProcessCron ( )

Currently the test server calls this 10 times per second. It also processes all the input/output at the same rate. It sleeps in between. (See testserver.cpp for the call and OTLog::Sleep() for the sleep code.)

Definition at line 311 of file OTServer.cpp.

312 {
313  if (!m_Cron.IsActivated()) return;
314 
315  bool bAddedNumbers = false;
316 
317  // Cron requires transaction numbers in order to process.
318  // So every time before I call Cron.Process(), I make sure to replenish
319  // first.
320  while (m_Cron.GetTransactionCount() < OTCron::GetCronRefillAmount()) {
321  int64_t lTransNum = 0;
322  bool bSuccess = transactor_.issueNextTransactionNumber(
323  m_nymServer, lTransNum, false);
324 
325  if (bSuccess) {
326  m_Cron.AddTransactionNumber(lTransNum);
327  bAddedNumbers = true;
328  }
329  else
330  break;
331  }
332 
333  if (bAddedNumbers) {
334  m_Cron.SaveCron();
335  }
336 
337  m_Cron.ProcessCronItems(); // This needs to be called regularly for trades,
338  // markets, payment plans, etc to process.
339 
340  // NOTE: TODO: OTHER RE-OCCURRING SERVER FUNCTIONS CAN GO HERE AS WELL!!
341  //
342  // Such as sweeping server accounts after expiration dates, etc.
343 }
EXPORT void AddTransactionNumber(const int64_t &lTransactionNum)
Definition: OTCron.cpp:415
EXPORT bool SaveCron()
Definition: OTCron.cpp:179
bool issueNextTransactionNumber(OTPseudonym &nym, int64_t &txNumber, bool storeNumber=true)
Definition: Transactor.cpp:211
EXPORT void ProcessCronItems()
Definition: OTCron.cpp:648
bool IsActivated() const
Definition: OTCron.hpp:221
EXPORT int32_t GetTransactionCount() const
Definition: OTCron.cpp:408
static int32_t GetCronRefillAmount()
Definition: OTCron.hpp:205

Friends And Related Function Documentation

friend class MainFile
friend

Definition at line 160 of file OTServer.hpp.

friend class MessageProcessor
friend

Definition at line 158 of file OTServer.hpp.

friend class Notary
friend

Definition at line 162 of file OTServer.hpp.

friend class PayDividendVisitor
friend

Definition at line 161 of file OTServer.hpp.

friend class Transactor
friend

Definition at line 157 of file OTServer.hpp.

friend class UserCommandProcessor
friend

Definition at line 159 of file OTServer.hpp.


The documentation for this class was generated from the following files: