ccnx: Adding Key abstraction
Change-Id: I8ee3754c2c44da0daac964468f72b81757ebaada
diff --git a/ccnx/ccnx-key.cc b/ccnx/ccnx-key.cc
new file mode 100644
index 0000000..19dc84d
--- /dev/null
+++ b/ccnx/ccnx-key.cc
@@ -0,0 +1,115 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+#include "ccnx-key.h"
+#include <tinyxml.h>
+#include <boost/lexical_cast.hpp>
+#include "logging.h"
+
+INIT_LOGGER ("Ccnx.Key");
+
+using namespace std;
+
+namespace Ccnx {
+
+Key::Key()
+ : m_meta("", "", 0, 0)
+{
+}
+
+Key::Key(const PcoPtr &keyObject, const PcoPtr &metaObject = PcoPtr())
+ : m_meta("", "", 0, 0)
+{
+ m_name = keyObject->name();
+ m_raw = keyObject->content();
+ m_hash = *(Hash::FromString(string((const char *)head(m_raw), m_raw.size())));
+ updateMeta(metaObject);
+}
+
+void
+Key::updateMeta(const PcoPtr &metaObject)
+{
+ if (metaObject)
+ {
+ TiXmlDocument doc;
+ Bytes xml = metaObject->content();
+ // just make sure it's null terminated as it's required by TiXmlDocument::parse
+ xml.push_back('\0');
+ doc.Parse((const char *)(head(xml)));
+ if (!doc.Error())
+ {
+ TiXmlElement *root = doc.RootElement();
+ for (TiXmlElement *child = root->FirstChildElement(); child; child = child->NextSiblingElement())
+ {
+ const char *elemName = child->Value();
+ const char *text = child->GetText();
+ if (elemName == "Name")
+ {
+ m_meta.realworldID = text;
+ }
+ else if (elemName == "Affiliation")
+ {
+ m_meta.affiliation = text;
+ }
+ else if (elemName == "Valid_to")
+ {
+ m_meta.validTo = boost::lexical_cast<time_t>(std::string(text));
+ }
+ else if (elemName == "Valid_from")
+ {
+ // this is not included in the key meta yet
+ // but it should eventually be there
+ }
+ else
+ {
+ // ignore known stuff
+ }
+ }
+ }
+ else
+ {
+ _LOG_ERROR("Cannot parse meta info:" << std::string(head(xml), xml.size()));
+ }
+ }
+}
+
+Key::VALIDITY
+Key::validity()
+{
+ if (m_meta.validFrom == 0 && m_meta.validTo == 0)
+ {
+ return OTHER;
+ }
+
+ time_t now = time(NULL);
+ if (now < m_meta.validFrom)
+ {
+ return NOT_YET_VALID;
+ }
+
+ if (now >= m_meta.validTo)
+ {
+ return EXPIRED;
+ }
+
+ return VALID;
+}
+
+} // Ccnx