aboutsummaryrefslogtreecommitdiffstats
path: root/icnet/utils
diff options
context:
space:
mode:
authorMauro Sardara <msardara+fdio@cisco.com>2017-06-05 16:48:29 +0200
committerMauro Sardara <msardara+fdio@cisco.com>2017-06-05 17:45:15 +0200
commitd22d2b4785e2f4eafc8dda2ae032931f89c7e45f (patch)
tree47fa6879217c4b08e8a78efc33b8cd007a110866 /icnet/utils
parent52ab9bf241528b0cb1d24384d22b017391be2899 (diff)
- Added new interface between applications and library:
- Application retrieve resources using the common HTTP url format. - Translation between network names and application names performed by the library - Added basic error handling - Added utils for http connections - Added support for differetn build types (DEBUG, RELEASE, RELEASE with debug symbols, RELEASE with min size executable) - Added support for iOS Change-Id: I8ba2a5d8bd70a4f7721e1bbc2efe3fb81ed2c98c Signed-off-by: Mauro Sardara <msardara+fdio@cisco.com>
Diffstat (limited to 'icnet/utils')
-rw-r--r--icnet/utils/icnet_utils_array.cc52
-rw-r--r--icnet/utils/icnet_utils_array.h45
-rw-r--r--icnet/utils/icnet_utils_daemonizator.cc74
-rw-r--r--icnet/utils/icnet_utils_daemonizator.h29
-rw-r--r--icnet/utils/icnet_utils_hash.cc64
-rw-r--r--icnet/utils/icnet_utils_hash.h42
-rw-r--r--icnet/utils/icnet_utils_string_tokenizer.cc52
-rw-r--r--icnet/utils/icnet_utils_string_tokenizer.h38
-rw-r--r--icnet/utils/icnet_utils_uri.cc137
-rw-r--r--icnet/utils/icnet_utils_uri.h51
10 files changed, 584 insertions, 0 deletions
diff --git a/icnet/utils/icnet_utils_array.cc b/icnet/utils/icnet_utils_array.cc
new file mode 100644
index 00000000..413119cb
--- /dev/null
+++ b/icnet/utils/icnet_utils_array.cc
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "icnet_utils_array.h"
+
+namespace icnet {
+
+namespace utils {
+
+Array::Array(const void *array, size_t size) {
+ this->array_ = array;
+ this->size_ = size;
+}
+
+Array::Array() {
+ this->array_ = nullptr;
+ this->size_ = 0;
+}
+
+const void *Array::data() const {
+ return array_;
+}
+
+std::size_t Array::size() const {
+ return size_;
+}
+
+Array &Array::setData(const void *data) {
+ array_ = data;
+ return *this;
+}
+
+Array &Array::setSize(std::size_t size) {
+ size_ = size;
+ return *this;
+}
+
+}
+
+} \ No newline at end of file
diff --git a/icnet/utils/icnet_utils_array.h b/icnet/utils/icnet_utils_array.h
new file mode 100644
index 00000000..f13c0b47
--- /dev/null
+++ b/icnet/utils/icnet_utils_array.h
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <cstddef>
+
+namespace icnet {
+
+namespace utils {
+
+class Array {
+ public:
+ explicit Array(const void *array, size_t size);
+
+ Array();
+
+ const void *data() const;
+
+ std::size_t size() const;
+
+ Array &setData(const void *data);
+
+ Array &setSize(std::size_t size);
+
+ private:
+ std::size_t size_;
+ const void *array_;
+};
+
+}
+
+}
diff --git a/icnet/utils/icnet_utils_daemonizator.cc b/icnet/utils/icnet_utils_daemonizator.cc
new file mode 100644
index 00000000..2bb6dd05
--- /dev/null
+++ b/icnet/utils/icnet_utils_daemonizator.cc
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "icnet_utils_daemonizator.h"
+#include "icnet_errors_runtime_exception.h"
+#include <cstdlib>
+#include <unistd.h>
+#include <iostream>
+#include <sys/stat.h>
+
+namespace icnet {
+
+namespace utils {
+
+void Daemonizator::daemonize() {
+ pid_t process_id = 0;
+ pid_t sid = 0;
+
+ // Create child process
+ process_id = fork();
+
+ // Indication of fork() failure
+ if (process_id < 0) {
+ throw errors::RuntimeException("Fork failed.");
+ }
+
+ // PARENT PROCESS. Need to kill it.
+ if (process_id > 0) {
+ std::cout << "Process id of child process " << process_id << std::endl;
+ // return success in exit status
+ exit(EXIT_SUCCESS);
+ }
+
+ // unmask the file mode
+ umask(0);
+
+ // set new session
+ sid = setsid();
+ if (sid < 0) {
+ // Return failure
+ exit(EXIT_FAILURE);
+ }
+
+ // Change the current working directory to root.
+ int ret = chdir("/");
+
+ if (ret < 0) {
+ throw errors::RuntimeException("Error changing working directory to root");
+ }
+
+ // Close stdin. stdout and stderr
+
+ close(STDIN_FILENO);
+ close(STDOUT_FILENO);
+ close(STDERR_FILENO);
+
+ // Really start application
+}
+
+}
+
+}
diff --git a/icnet/utils/icnet_utils_daemonizator.h b/icnet/utils/icnet_utils_daemonizator.h
new file mode 100644
index 00000000..20cd80bb
--- /dev/null
+++ b/icnet/utils/icnet_utils_daemonizator.h
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+namespace icnet {
+
+namespace utils {
+
+class Daemonizator {
+ public:
+ static void daemonize();
+};
+
+}
+
+} \ No newline at end of file
diff --git a/icnet/utils/icnet_utils_hash.cc b/icnet/utils/icnet_utils_hash.cc
new file mode 100644
index 00000000..39dd0229
--- /dev/null
+++ b/icnet/utils/icnet_utils_hash.cc
@@ -0,0 +1,64 @@
+/*
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "icnet_utils_hash.h"
+
+namespace icnet {
+
+namespace utils {
+
+uint32_t Hash::hash32(const void *data, std::size_t len) {
+ const uint32_t fnv1a_offset = 0x811C9DC5;
+ return Hash::cumulativeHash32(data, len, fnv1a_offset);
+}
+
+uint32_t Hash::cumulativeHash32(const void *data, std::size_t len, uint32_t lastValue) {
+ // Standard FNV 32-bit prime: see http://www.isthe.com/chongo/tech/comp/fnv/#FNV-param
+ const uint32_t fnv1a_prime = 0x01000193;
+ uint32_t hash = lastValue;
+
+ const char *chardata = (char *) data;
+
+ for (std::size_t i = 0; i < len; i++) {
+ hash = hash ^ chardata[i];
+ hash = hash * fnv1a_prime;
+ }
+
+ return hash;
+}
+
+uint64_t Hash::hash64(const void *data, std::size_t len) {
+ // Standard FNV 64-bit offset: see http://www.isthe.com/chongo/tech/comp/fnv/#FNV-param
+ const uint64_t fnv1a_offset = 0xCBF29CE484222325ULL;
+ return cumulativeHash64(data, len, fnv1a_offset);
+}
+
+uint64_t Hash::cumulativeHash64(const void *data, std::size_t len, uint64_t lastValue) {
+ // Standard FNV 64-bit prime: see http://www.isthe.com/chongo/tech/comp/fnv/#FNV-param
+ const uint64_t fnv1a_prime = 0x00000100000001B3ULL;
+ uint64_t hash = lastValue;
+ const char *chardata = (char *) data;
+
+ for (std::size_t i = 0; i < len; i++) {
+ hash = hash ^ chardata[i];
+ hash = hash * fnv1a_prime;
+ }
+
+ return hash;
+}
+
+}
+
+} \ No newline at end of file
diff --git a/icnet/utils/icnet_utils_hash.h b/icnet/utils/icnet_utils_hash.h
new file mode 100644
index 00000000..9ae01f39
--- /dev/null
+++ b/icnet/utils/icnet_utils_hash.h
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <cstdint>
+#include <cstddef>
+
+namespace icnet {
+
+namespace utils {
+
+//const uint32_t FNV1A_PRIME_32 = 0x01000193;
+//const uint32_t FNV1A_OFFSET_32 = 0x811C9DC5;
+//const uint64_t FNV1A_PRIME_64 = 0x00000100000001B3ULL;
+//const uint64_t FNV1A_OFFSET_64 = 0xCBF29CE484222325ULL;
+
+class Hash {
+ public:
+ static uint32_t cumulativeHash32(const void *data, std::size_t len, uint32_t lastValue);
+ static uint64_t cumulativeHash64(const void *data, std::size_t len, uint64_t lastValue);
+ static uint32_t hash32(const void *data, std::size_t len);
+ static uint64_t hash64(const void *data, std::size_t len);
+ private:
+
+};
+
+}
+
+} \ No newline at end of file
diff --git a/icnet/utils/icnet_utils_string_tokenizer.cc b/icnet/utils/icnet_utils_string_tokenizer.cc
new file mode 100644
index 00000000..ead1052a
--- /dev/null
+++ b/icnet/utils/icnet_utils_string_tokenizer.cc
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <icnet_errors_tokenizer_exception.h>
+#include "icnet_utils_string_tokenizer.h"
+#include "icnet_errors.h"
+
+namespace icnet {
+
+namespace utils {
+
+StringTokenizer::StringTokenizer(const std::string &str)
+ : str_(str), delimiter_(" ") {
+}
+
+StringTokenizer::StringTokenizer(const std::string &str, const std::string &delim)
+ : str_(str), delimiter_(delim) {
+}
+
+bool StringTokenizer::hasMoreTokens() {
+ return str_.find(delimiter_) != std::string::npos && !str_.empty();
+}
+
+std::string StringTokenizer::nextToken() {
+ unsigned long pos = str_.find(delimiter_);
+
+ bool token_found = std::string::npos != pos;
+
+ if (!token_found && str_.empty()) {
+ throw errors::TokenizerException();
+ }
+
+ std::string token = str_.substr(0, pos);
+ str_.erase(0, token_found ? pos + delimiter_.length() : pos);
+
+ return token;
+}
+
+}
+} \ No newline at end of file
diff --git a/icnet/utils/icnet_utils_string_tokenizer.h b/icnet/utils/icnet_utils_string_tokenizer.h
new file mode 100644
index 00000000..7dc6b458
--- /dev/null
+++ b/icnet/utils/icnet_utils_string_tokenizer.h
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <string>
+
+namespace icnet {
+
+namespace utils {
+
+class StringTokenizer {
+ public:
+ StringTokenizer(const std::string &str);
+ StringTokenizer(const std::string &str, const std::string &delim);
+
+ bool hasMoreTokens();
+ std::string nextToken();
+ private:
+ std::string str_;
+ std::string delimiter_;
+};
+
+}
+
+}
diff --git a/icnet/utils/icnet_utils_uri.cc b/icnet/utils/icnet_utils_uri.cc
new file mode 100644
index 00000000..8268bdf9
--- /dev/null
+++ b/icnet/utils/icnet_utils_uri.cc
@@ -0,0 +1,137 @@
+/*
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "icnet_utils_uri.h"
+#include "icnet_errors_runtime_exception.h"
+
+namespace icnet {
+
+namespace utils {
+
+Uri::Uri() {
+
+}
+
+Uri &Uri::parse(const std::string &uri) {
+ if (uri.length() == 0) {
+ throw errors::RuntimeException("Malformed URI.");
+ }
+
+ iterator_t uriEnd = uri.end();
+
+ // get query start
+ iterator_t queryStart = std::find(uri.begin(), uriEnd, '?');
+
+ // protocol
+ iterator_t protocolStart = uri.begin();
+ iterator_t protocolEnd = std::find(protocolStart, uriEnd, ':'); //"://");
+
+ if (protocolEnd != uriEnd) {
+ std::string prot = &*(protocolEnd);
+ if ((prot.length() > 3) && (prot.substr(0, 3) == "://")) {
+ protocol_ = std::string(protocolStart, protocolEnd);
+ protocolEnd += 3; // ://
+ } else {
+ protocolEnd = uri.begin(); // no protocol
+ }
+ } else {
+ protocolEnd = uri.begin(); // no protocol
+ }
+ // host
+ iterator_t hostStart = protocolEnd;
+ iterator_t pathStart = std::find(hostStart, uriEnd, '/'); // get pathStart
+
+ iterator_t hostEnd = std::find(protocolEnd,
+ (pathStart != uriEnd) ? pathStart : queryStart,
+ ':'); // check for port
+
+ locator_ = std::string(hostStart, hostEnd);
+
+ // port
+ if ((hostEnd != uriEnd) && ((&*(hostEnd))[0] == ':')) {
+ hostEnd++;
+ iterator_t portEnd = (pathStart != uriEnd) ? pathStart : queryStart;
+ port_ = std::string(hostEnd, portEnd);
+ }
+
+ // path
+ if (pathStart != uriEnd) {
+ path_ = std::string(pathStart, queryStart);
+ }
+ // query
+ if (queryStart != uriEnd) {
+ query_string_ = std::string(queryStart, uri.end());
+ }
+
+ return *this;
+
+}
+
+Uri &Uri::parseProtocolAndLocator(const std::string &locator) {
+
+ iterator_t total_end = locator.end();
+
+ // protocol
+ iterator_t protocol_start = locator.begin();
+ iterator_t protocol_end = std::find(protocol_start, total_end, ':'); //"://");
+
+ if (protocol_end != total_end) {
+ std::string prot = &*(protocol_end);
+ if ((prot.length() > 3) && (prot.substr(0, 3) == "://")) {
+ protocol_ = std::string(protocol_start, protocol_end);
+ protocol_end += 3; // ://
+ } else {
+ throw errors::RuntimeException("Malformed locator. (Missing \"://\")");
+ }
+ } else {
+ throw errors::RuntimeException("Malformed locator. No protocol specified.");
+ }
+
+ // locator
+ iterator_t host_start = protocol_end;
+ iterator_t host_end = std::find(protocol_end, total_end, '/');
+
+ if (host_start == host_end) {
+ throw errors::RuntimeException("Malformed locator. Locator name is missing");
+ }
+
+ locator_ = std::string(host_start, host_end);
+
+ return *this;
+}
+
+std::string Uri::getLocator() {
+ return locator_;
+}
+
+std::string Uri::getPath() {
+ return path_;
+}
+
+std::string Uri::getPort() {
+ return port_;
+}
+
+std::string Uri::getProtocol() {
+ return protocol_;
+}
+
+std::string Uri::getQueryString() {
+ return query_string_;
+}
+
+} // end namespace utils
+
+} // end namespace icnet \ No newline at end of file
diff --git a/icnet/utils/icnet_utils_uri.h b/icnet/utils/icnet_utils_uri.h
new file mode 100644
index 00000000..38172282
--- /dev/null
+++ b/icnet/utils/icnet_utils_uri.h
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <string>
+#include <algorithm> // find
+
+namespace icnet {
+
+namespace utils {
+
+class Uri {
+
+ typedef std::string::const_iterator iterator_t;
+
+ public:
+ Uri();
+
+ Uri &parse(const std::string &uri);
+
+ Uri &parseProtocolAndLocator(const std::string &locator);
+
+ std::string getQueryString();
+
+ std::string getPath();
+
+ std::string getProtocol();
+
+ std::string getLocator();
+
+ std::string getPort();
+ private:
+ std::string query_string_, path_, protocol_, locator_, port_;
+}; // uri
+
+}
+
+} \ No newline at end of file