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)
{
#if BOOST_VERSION > 10500
RouterInfo * r = new RouterInfo (it1->path().string().c_str ());
RouterInfo * r = new RouterInfo (it1->path().string());
#else
RouterInfo * r = new RouterInfo(it1->path().c_str());
RouterInfo * r = new RouterInfo(it1->path());
#endif
r->DeleteBuffer ();
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,
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;
}

View File

@ -17,11 +17,12 @@ namespace i2p
{
namespace data
{
RouterInfo::RouterInfo (const char * filename):
m_IsUpdated (false), m_IsUnreachable (false), m_SupportedTransports (0), m_Caps (0)
RouterInfo::RouterInfo (const std::string& fullPath):
m_FullPath (fullPath), m_IsUpdated (false), m_IsUnreachable (false),
m_SupportedTransports (0), m_Caps (0)
{
m_Buffer = new uint8_t[MAX_RI_BUFFER_SIZE];
ReadFromFile (filename);
ReadFromFile ();
}
RouterInfo::RouterInfo (const uint8_t * buf, int len):
@ -63,24 +64,35 @@ namespace data
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 ())
{
s.seekg (0,std::ios::end);
m_BufferLen = s.tellg ();
if (m_BufferLen < 40)
{
LogPrint("File", filename, " is malformed");
return;
LogPrint("File", m_FullPath, " is malformed");
return false;
}
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);
ReadFromBuffer ();
}
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 ()
@ -334,6 +346,16 @@ namespace data
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 ()
{
m_Timestamp = i2p::util::GetMillisecondsSinceEpoch (); // refresh timstamp
@ -350,6 +372,7 @@ namespace data
void RouterInfo::SaveToFile (const std::string& fullPath)
{
m_FullPath = fullPath;
if (m_Buffer)
{
std::ofstream f (fullPath, std::ofstream::binary | std::ofstream::out);

View File

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