load RI buffer from file by request

This commit is contained in:
orignal 2014-07-23 10:56:41 -04:00
parent c21adbe3ae
commit 028c70d6ee
4 changed files with 43 additions and 17 deletions

View File

@ -260,9 +260,9 @@ namespace data
for (boost::filesystem::directory_iterator it1 (it->path ()); it1 != end; ++it1) for (boost::filesystem::directory_iterator it1 (it->path ()); it1 != end; ++it1)
{ {
#if BOOST_VERSION > 10500 #if BOOST_VERSION > 10500
RouterInfo * r = new RouterInfo (it1->path().string().c_str ()); RouterInfo * r = new RouterInfo (it1->path().string());
#else #else
RouterInfo * r = new RouterInfo(it1->path().c_str()); RouterInfo * r = new RouterInfo(it1->path());
#endif #endif
r->DeleteBuffer (); r->DeleteBuffer ();
m_RouterInfos[r->GetIdentHash ()] = r; m_RouterInfos[r->GetIdentHash ()] = r;

View File

@ -79,7 +79,7 @@ namespace i2p
m_SigningPrivateKey.Initialize (i2p::crypto::dsap, i2p::crypto::dsaq, i2p::crypto::dsag, m_SigningPrivateKey.Initialize (i2p::crypto::dsap, i2p::crypto::dsaq, i2p::crypto::dsag,
CryptoPP::Integer (m_Keys.signingPrivateKey, 20)); CryptoPP::Integer (m_Keys.signingPrivateKey, 20));
m_RouterInfo = i2p::data::RouterInfo (i2p::util::filesystem::GetFullPath (ROUTER_INFO).c_str ()); // TODO m_RouterInfo = i2p::data::RouterInfo (i2p::util::filesystem::GetFullPath (ROUTER_INFO)); // TODO
return true; return true;
} }

View File

@ -17,11 +17,12 @@ namespace i2p
{ {
namespace data namespace data
{ {
RouterInfo::RouterInfo (const char * filename): RouterInfo::RouterInfo (const std::string& fullPath):
m_IsUpdated (false), m_IsUnreachable (false), m_SupportedTransports (0), m_Caps (0) m_FullPath (fullPath), m_IsUpdated (false), m_IsUnreachable (false),
m_SupportedTransports (0), m_Caps (0)
{ {
m_Buffer = new uint8_t[MAX_RI_BUFFER_SIZE]; m_Buffer = new uint8_t[MAX_RI_BUFFER_SIZE];
ReadFromFile (filename); ReadFromFile ();
} }
RouterInfo::RouterInfo (const uint8_t * buf, int len): RouterInfo::RouterInfo (const uint8_t * buf, int len):
@ -63,24 +64,35 @@ namespace data
m_Timestamp = i2p::util::GetMillisecondsSinceEpoch (); m_Timestamp = i2p::util::GetMillisecondsSinceEpoch ();
} }
void RouterInfo::ReadFromFile (const char * filename) bool RouterInfo::LoadFile ()
{ {
std::ifstream s(filename, std::ifstream::binary); std::ifstream s(m_FullPath.c_str (), std::ifstream::binary);
if (s.is_open ()) if (s.is_open ())
{ {
s.seekg (0,std::ios::end); s.seekg (0,std::ios::end);
m_BufferLen = s.tellg (); m_BufferLen = s.tellg ();
if (m_BufferLen < 40) if (m_BufferLen < 40)
{ {
LogPrint("File", filename, " is malformed"); LogPrint("File", m_FullPath, " is malformed");
return; return false;
} }
s.seekg(0, std::ios::beg); s.seekg(0, std::ios::beg);
if (!m_Buffer)
m_Buffer = new uint8_t[MAX_RI_BUFFER_SIZE];
s.read((char *)m_Buffer, m_BufferLen); s.read((char *)m_Buffer, m_BufferLen);
ReadFromBuffer ();
} }
else else
LogPrint ("Can't open file ", filename); {
LogPrint ("Can't open file ", m_FullPath);
return false;
}
return true;
}
void RouterInfo::ReadFromFile ()
{
if (LoadFile ())
ReadFromBuffer ();
} }
void RouterInfo::ReadFromBuffer () void RouterInfo::ReadFromBuffer ()
@ -334,6 +346,16 @@ namespace data
s.write (properties.str ().c_str (), properties.str ().size ()); s.write (properties.str ().c_str (), properties.str ().size ());
} }
const uint8_t * RouterInfo::GetBuffer ()
{
if (!m_Buffer)
{
if (LoadFile ())
LogPrint ("Buffer for ", m_IdentHashAbbreviation, " loaded from file");
}
return m_Buffer;
}
void RouterInfo::CreateBuffer () void RouterInfo::CreateBuffer ()
{ {
m_Timestamp = i2p::util::GetMillisecondsSinceEpoch (); // refresh timstamp m_Timestamp = i2p::util::GetMillisecondsSinceEpoch (); // refresh timstamp
@ -350,6 +372,7 @@ namespace data
void RouterInfo::SaveToFile (const std::string& fullPath) void RouterInfo::SaveToFile (const std::string& fullPath)
{ {
m_FullPath = fullPath;
if (m_Buffer) if (m_Buffer)
{ {
std::ofstream f (fullPath, std::ofstream::binary | std::ofstream::out); std::ofstream f (fullPath, std::ofstream::binary | std::ofstream::out);

View File

@ -63,8 +63,8 @@ namespace data
std::vector<Introducer> introducers; std::vector<Introducer> introducers;
}; };
RouterInfo (const char * filename); RouterInfo (const std::string& fullPath);
RouterInfo (): m_Buffer (nullptr) {}; RouterInfo (): m_Buffer (nullptr) { m_IdentHashBase64[0] = 0; m_IdentHashAbbreviation[0] = 0; };
RouterInfo (const RouterInfo& ) = default; RouterInfo (const RouterInfo& ) = default;
RouterInfo& operator=(const RouterInfo& ) = default; RouterInfo& operator=(const RouterInfo& ) = default;
RouterInfo (const uint8_t * buf, int len); RouterInfo (const uint8_t * buf, int len);
@ -99,8 +99,9 @@ namespace data
bool IsUnreachable () const { return m_IsUnreachable; }; bool IsUnreachable () const { return m_IsUnreachable; };
const uint8_t * GetBuffer () const { return m_Buffer; }; const uint8_t * GetBuffer () const { return m_Buffer; };
int GetBufferLen () const { return m_BufferLen; }; const uint8_t * GetBuffer (); // load if necessary
int GetBufferLen () const { return m_BufferLen; };
void CreateBuffer (); void CreateBuffer ();
void UpdateRoutingKey (); void UpdateRoutingKey ();
@ -119,7 +120,8 @@ namespace data
private: private:
void ReadFromFile (const char * filename); bool LoadFile ();
void ReadFromFile ();
void ReadFromStream (std::istream& s); void ReadFromStream (std::istream& s);
void ReadFromBuffer (); void ReadFromBuffer ();
void WriteToStream (std::ostream& s); void WriteToStream (std::ostream& s);
@ -131,6 +133,7 @@ namespace data
private: private:
std::string m_FullPath;
Identity m_RouterIdentity; Identity m_RouterIdentity;
IdentHash m_IdentHash; IdentHash m_IdentHash;
RoutingKey m_RoutingKey; RoutingKey m_RoutingKey;