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
diff --git a/ccnx/ccnx-key.h b/ccnx/ccnx-key.h
new file mode 100644
index 0000000..695afda
--- /dev/null
+++ b/ccnx/ccnx-key.h
@@ -0,0 +1,94 @@
+/* -*- 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>
+ */
+
+#ifndef CCNX_KEY_H
+#define CCNX_KEY_H
+
+#include "ccnx-common.h"
+#include "ccnx-name.h"
+#include "ccnx-pco.h"
+#include "hash-helper.h"
+#include <boost/shared_ptr.hpp>
+
+namespace Ccnx {
+
+class Key
+{
+public:
+ enum VALIDITY
+ {
+ NOT_YET_VALID,
+ VALID,
+ EXPIRED,
+ OTHER
+ };
+
+ Key();
+ Key(const PcoPtr &keyObject, const PcoPtr &metaObject);
+
+ void
+ updateMeta(const PcoPtr &metaObject);
+
+ Name
+ name() { return m_name; }
+
+ Bytes
+ raw() { return m_raw; }
+
+ Hash
+ hash() { return m_hash; }
+
+ std::string
+ realworldID() { return m_meta.realworldID; }
+
+ std::string
+ affilication() { return m_meta.affiliation; }
+
+ VALIDITY
+ validity();
+
+private:
+ struct KeyMeta
+ {
+ KeyMeta(std::string id, std::string affi, time_t from, time_t to)
+ : realworldID(id)
+ , affiliation(affi)
+ , validFrom(from)
+ , validTo(to)
+ {
+ }
+ std::string realworldID;
+ std::string affiliation;
+ time_t validFrom;
+ time_t validTo;
+ };
+
+ Name m_name;
+ Hash m_hash; // publisherPublicKeyHash
+ Bytes m_raw;
+ KeyMeta m_meta;
+};
+
+typedef boost::shared_ptr<Key> KeyPtr;
+
+}
+
+#endif // CCNX_KEY_H
diff --git a/src/hash-helper.h b/src/hash-helper.h
index 86b87c9..d289bb1 100644
--- a/src/hash-helper.h
+++ b/src/hash-helper.h
@@ -40,6 +40,12 @@
static unsigned char _origin;
static HashPtr Origin;
+ Hash ()
+ : m_length(0)
+ , m_buf(0)
+ {
+ }
+
Hash (const void *buf, unsigned int length)
: m_length (length)
{
diff --git a/waf-tools/tinyxml.py b/waf-tools/tinyxml.py
new file mode 100644
index 0000000..3908b38
--- /dev/null
+++ b/waf-tools/tinyxml.py
@@ -0,0 +1,76 @@
+#! /usr/bin/env python
+# encoding: utf-8
+
+'''
+
+When using this tool, the wscript will look like:
+
+ def options(opt):
+ opt.tool_options('tinyxml', tooldir=["waf-tools"])
+
+ def configure(conf):
+ conf.load('compiler_cxx tiny')
+
+ def build(bld):
+ bld(source='main.cpp', target='app', use='TINYXML')
+
+Options are generated, in order to specify the location of tinyxml includes/libraries.
+
+
+'''
+import sys
+import re
+from waflib import Utils,Logs,Errors
+from waflib.Configure import conf
+TINYXML_DIR=['/usr','/usr/local','/opt/local','/sw']
+TINYXML_VERSION_FILE='tinyxml.h'
+TINYXML_VERSION_CODE='''
+#include <iostream>
+#include <tinyxml.h>
+int main() { std::cout << TIXML_MAJOR_VERSION << "." << TIXML_MINOR_VERSION << "." << TIXML_PATCH_VERSION; }
+'''
+
+def options(opt):
+ opt.add_option('--tinyxml',type='string',default='',dest='tinyxml_dir',help='''path to where TinyXML is installed, e.g. /usr/local''')
+@conf
+def __tinyxml_get_version_file(self,dir):
+ try:
+ return self.root.find_dir(dir).find_node('%s/%s' % ('include', TINYXML_VERSION_FILE))
+ except:
+ return None
+@conf
+def tinyxml_get_version(self,dir):
+ val=self.check_cxx(fragment=TINYXML_VERSION_CODE,includes=['%s/%s' % (dir, 'include')], execute=True, define_ret = True, mandatory=True)
+ return val
+@conf
+def tinyxml_get_root(self,*k,**kw):
+ root=k and k[0]or kw.get('path',None)
+ # Logs.pprint ('RED', ' %s' %root)
+ if root and self.__tinyxml_get_version_file(root):
+ return root
+ for dir in TINYXML_DIR:
+ if self.__tinyxml_get_version_file(dir):
+ return dir
+ if root:
+ self.fatal('TinyXML not found in %s'%root)
+ else:
+ self.fatal('TinyXML not found, please provide a --tinyxml argument (see help)')
+@conf
+def check_tinyxml(self,*k,**kw):
+ if not self.env['CXX']:
+ self.fatal('load a c++ compiler first, conf.load("compiler_cxx")')
+
+ var=kw.get('uselib_store','TINYXML')
+ self.start_msg('Checking TinyXML')
+ root = self.tinyxml_get_root(*k,**kw);
+ self.env.TINYXML_VERSION=self.tinyxml_get_version(root)
+
+ self.env['INCLUDES_%s'%var]= '%s/%s' % (root, "include");
+ self.env['LIB_%s'%var] = "tinyxml"
+ self.env['LIBPATH_%s'%var] = '%s/%s' % (root, "lib")
+
+ self.end_msg(self.env.TINYXML_VERSION)
+ if Logs.verbose:
+ Logs.pprint('CYAN',' TinyXML include : %s'%self.env['INCLUDES_%s'%var])
+ Logs.pprint('CYAN',' TinyXML lib : %s'%self.env['LIB_%s'%var])
+ Logs.pprint('CYAN',' TinyXML libpath : %s'%self.env['LIBPATH_%s'%var])
diff --git a/wscript b/wscript
index d82ce50..76afad9 100644
--- a/wscript
+++ b/wscript
@@ -14,6 +14,7 @@
opt.add_option('--auto-update', action='store_true',default=False,dest='autoupdate',help='''(OSX) Download sparkle framework and enable autoupdate feature''')
opt.load('compiler_c compiler_cxx boost ccnx protoc qt4 gnu_dirs')
+ opt.load('tinyxml', tooldir=['waf-tools'])
def configure(conf):
conf.load("compiler_c compiler_cxx gnu_dirs")
@@ -47,6 +48,8 @@
conf.check_cfg(package='sqlite3', args=['--cflags', '--libs'], uselib_store='SQLITE3', mandatory=True)
conf.check_cfg(package='libevent', args=['--cflags', '--libs'], uselib_store='LIBEVENT', mandatory=True)
conf.check_cfg(package='libevent_pthreads', args=['--cflags', '--libs'], uselib_store='LIBEVENT_PTHREADS', mandatory=True)
+ conf.load('tinyxml')
+ conf.check_tinyxml(path=conf.options.tinyxml_dir)
conf.define ("TRAY_ICON", "chronoshare-big.png")
if Utils.unversioned_sys_platform () == "linux":
@@ -161,7 +164,7 @@
target="ccnx",
features=['cxx'],
source = bld.path.ant_glob(['ccnx/**/*.cc', 'ccnx/**/*.cpp']),
- use = 'BOOST BOOST_THREAD SSL CCNX LOG4CXX scheduler executor',
+ use = 'TINYXML BOOST BOOST_THREAD SSL CCNX LOG4CXX scheduler executor',
includes = "ccnx src scheduler executor",
)