diff options
76 files changed, 2353 insertions, 631 deletions
@@ -27,5 +27,4 @@ Makefile *.swp libtool *~ -*.pyc .idea diff --git a/CMakeLists.txt b/CMakeLists.txt index e166c4cf..e5ed91f4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,14 +15,16 @@ cmake_minimum_required(VERSION 3.2) project(Libicnet) set(CMAKE_CXX_STANDARD 11) -## Set build folders -#set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/build) -#set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin) -#set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib) +if (NOT CMAKE_BUILD_TYPE) + message(STATUS "No build type selected, default to Release") + set(CMAKE_BUILD_TYPE "Release") +endif () set(CCNX_API icnet/ccnx) set(CP_API icnet/transport) -set(COMMON_INCLUDES icnet/common-includes) +set(ERRORS icnet/errors) +set(UTILS icnet/utils) +set(HTTP icnet/http) set(APPS apps) option(BUILD_APPS "Build apps" ON) @@ -40,62 +42,88 @@ include_directories( ${CMAKE_BINARY_DIR} ${PROJECT_SOURCE_DIR}/${CP_API} ${PROJECT_SOURCE_DIR}/${CCNX_API} - ${PROJECT_SOURCE_DIR}/${COMMON_INCLUDES} + ${PROJECT_SOURCE_DIR}/${ERRORS} + ${PROJECT_SOURCE_DIR}/${UTILS} + ${PROJECT_SOURCE_DIR}/${HTTP} ) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/") -find_package(LongBow REQUIRED) -include_directories(${LONGBOW_INCLUDE_DIRS}) +set(CMAKE_VERBOSE_MAKEFILE on) -find_package(Libparc REQUIRED) -include_directories(${LIBPARC_INCLUDE_DIRS}) +if(COMPILE_FOR_IOS) + set(OPENSSL_ROOT_DIR $ENV{CCNX_DEPENDENCIES}) + find_host_package ( OpenSSL REQUIRED ) -find_package(CCNX_Common REQUIRED) -include_directories(${CCNX_COMMON_INCLUDE_DIRS}) - -find_package(CCNX_Transport_Rta REQUIRED) -include_directories(${CCNX_TRANSPORT_RTA_INCLUDE_DIRS}) + set(BOOST_ROOT $ENV{CCNX_DEPENDENCIES}) + find_host_package(Boost 1.53.0 COMPONENTS system REQUIRED) + include_directories(SYSTEM ${Boost_INCLUDE_DIR}) -find_package(CCNX_Portal REQUIRED) -include_directories(${CCNX_PORTAL_INCLUDE_DIRS}) + find_host_package(LongBow REQUIRED) + include_directories(${LONGBOW_INCLUDE_DIRS}) -find_package(Threads REQUIRED) -include_directories(${CMAKE_THREADS_INCLUDE_DIRS}) + find_host_package(Libparc REQUIRED) + include_directories(${LIBPARC_INCLUDE_DIRS}) -find_package(Threads REQUIRED) + find_host_package(CCNX_Common REQUIRED) + include_directories(${CCNX_COMMON_INCLUDE_DIRS}) -set(CMAKE_VERBOSE_MAKEFILE off) + find_host_package(CCNX_Transport_Rta REQUIRED) + include_directories(${CCNX_TRANSPORT_RTA_INCLUDE_DIRS}) -if(ANDROID_API) - include_directories(${ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++/4.9/include) - include_directories(${ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi/include) - set(CCNX_PORTAL_LIBRARIES "$ENV{CCNX_DEPENDENCIES}/lib/libccnx_api_portal.a" ) - set(CCNX_TRANSPORT_RTA_LIBRARIES "$ENV{CCNX_DEPENDENCIES}/lib/libccnx_api_notify.a" "$ENV{CCNX_DEPENDENCIES}/lib/libccnx_api_control.a" "$ENV{CCNX_DEPENDENCIES}/lib/libccnx_transport_rta.a") - set(CCNX_COMMON_LIBRARIES "$ENV{CCNX_DEPENDENCIES}/lib/libccnx_common.a") - set(LIBPARC_LIBRARIES "$ENV{CCNX_DEPENDENCIES}/lib/libparc.a") - set(LONGBOW_LIBRARIES "$ENV{CCNX_DEPENDENCIES}/lib/liblongbow.a" "$ENV{CCNX_DEPENDENCIES}/lib/liblongbow-ansiterm.a" "$ENV{CCNX_DEPENDENCIES}/lib/liblongbow-textplain.a" "$ENV{CCNX_DEPENDENCIES}/lib/libcrypto.a" "$ENV{CCNX_DEPENDENCIES}/lib/libssl.a") - set(Boost_LIBRARIES $ENV{CCNX_DEPENDENCIES}/lib/libboost_regex.a $ENV{CCNX_DEPENDENCIES}/lib/libboost_system.a $ENV{CCNX_DEPENDENCIES}/lib/libboost_thread.a $ENV{CCNX_DEPENDENCIES}/lib/libboost_filesystem.a $ENV{CCNX_DEPENDENCIES}/lib/libboost_date_time.a $ENV{CCNX_DEPENDENCIES}/lib/libboost_chrono.a) - set(ANDROID_LIBRARIES "${ANDROID_NDK}/platforms/android-23/arch-arm/usr/lib/libz.a" "${ANDROID_NDK}/platforms/android-23/arch-arm/usr/lib/liblog.so" "$ENV{NDK}/sources/cxx-stl/gnu-libstdc++/4.9/libs/$ENV{ABI}/libgnustl_shared.so" "$ENV{CCNX_DEPENDENCIES}/lib/libcrystax.a" "$ENV{CCNX_DEPENDENCIES}/lib/libevent.a") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_C_FLAGS} ${ANDROID_C_FLAGS} -std=c++11 -Wall -fpermissive -O3") + find_host_package(CCNX_Portal REQUIRED) + include_directories(${CCNX_PORTAL_INCLUDE_DIRS}) else () - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -fpermissive -O3") + find_package(LongBow REQUIRED) + include_directories(${LONGBOW_INCLUDE_DIRS}) + + find_package(Libparc REQUIRED) + include_directories(${LIBPARC_INCLUDE_DIRS}) + + find_package(CCNX_Common REQUIRED) + include_directories(${CCNX_COMMON_INCLUDE_DIRS}) + + find_package(CCNX_Transport_Rta REQUIRED) + include_directories(${CCNX_TRANSPORT_RTA_INCLUDE_DIRS}) + + find_package(CCNX_Portal REQUIRED) + include_directories(${CCNX_PORTAL_INCLUDE_DIRS}) + + find_package(Threads REQUIRED) + include_directories(${CMAKE_THREADS_INCLUDE_DIRS}) + find_package(Boost 1.53.0 COMPONENTS system REQUIRED) include_directories(SYSTEM ${Boost_INCLUDE_DIR}) - if (BUILD_APPS) - set(SUBFOLDERS ${APPS}) - endif (BUILD_APPS) +endif() + +if(ANDROID_API) + include_directories(${ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi/include) + include_directories(${ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++/4.9/include) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${ANDROID_C_FLAGS} -Wall") +else() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") + + if (NOT COMPILE_FOR_IOS) + if (BUILD_APPS) + set(SUBFOLDERS ${APPS}) + endif (BUILD_APPS) + endif() + endif (ANDROID_API) +set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${CMAKE_CXX_FLAGS}") +set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${CMAKE_CXX_FLAGS} -fpermissive") +set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} ${CMAKE_CXX_FLAGS} -fpermissive") +set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} ${CMAKE_CXX_FLAGS} -fpermissive") + set(LIBRARIES - ${CCNX_PORTAL_LIBRARIES} - ${CCNX_TRANSPORT_RTA_LIBRARIES} - ${CCNX_COMMON_LIBRARIES} - ${LIBPARC_LIBRARIES} - ${Boost_LIBRARIES} - ${CMAKE_THREAD_LIBS_INIT} - ${LONGBOW_LIBRARIES} - ${ANDROID_LIBRARIES}) + ${CCNX_PORTAL_LIBRARIES} + ${CCNX_TRANSPORT_RTA_LIBRARIES} + ${CCNX_COMMON_LIBRARIES} + ${LIBPARC_LIBRARIES} + ${Boost_LIBRARIES} + ${CMAKE_THREAD_LIBS_INIT} + ${LONGBOW_LIBRARIES}) set(SUBFOLDERS ${SUBFOLDERS} icnet) diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt index 6708f3b3..a661847f 100755 --- a/apps/CMakeLists.txt +++ b/apps/CMakeLists.txt @@ -14,4 +14,4 @@ cmake_minimum_required(VERSION 3.2) #include( CTest ) -subdirs(consumers producers general iping) +subdirs(consumers producers general iping http) diff --git a/apps/consumers/CMakeLists.txt b/apps/consumers/CMakeLists.txt index 68e79834..86752da7 100755 --- a/apps/consumers/CMakeLists.txt +++ b/apps/consumers/CMakeLists.txt @@ -14,17 +14,8 @@ cmake_minimum_required(VERSION 3.2) set(CONSUMER_SOURCE_FILES icnet_consumer_test.cc) -set(IGET_SOURCE_FILES icnet_iget.cc) -set(CONSUMERDASH_SOURCE_FILES icnet_consumer_dash.cc) add_executable(consumer-test ${CONSUMER_SOURCE_FILES}) -add_executable(iget ${IGET_SOURCE_FILES}) -add_executable(consumer-dash ${CONSUMERDASH_SOURCE_FILES}) target_link_libraries(consumer-test icnet ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES}) -target_link_libraries(iget icnet ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES}) -target_link_libraries(consumer-dash icnet ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES}) - install(TARGETS consumer-test DESTINATION ${CMAKE_INSTALL_PREFIX}/bin COMPONENT library) -install(TARGETS iget DESTINATION ${CMAKE_INSTALL_PREFIX}/bin COMPONENT library) -install(TARGETS consumer-dash DESTINATION ${CMAKE_INSTALL_PREFIX}/bin COMPONENT library) diff --git a/apps/consumers/icnet_consumer_dash.cc b/apps/consumers/icnet_consumer_dash.cc deleted file mode 100755 index 4ec4a2c9..00000000 --- a/apps/consumers/icnet_consumer_dash.cc +++ /dev/null @@ -1,217 +0,0 @@ -/* - * 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_socket_consumer.h" - -#define DEFAULT_BETA 0.99 -#define DEFAULT_GAMMA 0.07 - -namespace icnet { - -class CallbackContainer { - public: - CallbackContainer() - : work_(new boost::asio::io_service::work(io_service_)), - handler_(std::async(std::launch::async, [this]() { io_service_.run(); })) { - seen_manifest_segments_ = 0; - seen_data_segments_ = 0; - byte_counter_ = 0; - } - - ~CallbackContainer() { - work_.reset(); - } - - void processPayload(ConsumerSocket &c, const uint8_t *buffer, size_t bufferSize) { - std::cout << "Content retrieved!! Size: " << bufferSize << std::endl; - } - - bool verifyData(ConsumerSocket &c, const ContentObject &contentObject) { - if (contentObject.getContentType() == PayloadType::DATA) { - std::cout << "VERIFY CONTENT" << std::endl; - } else if (contentObject.getContentType() == PayloadType::MANIFEST) { - std::cout << "VERIFY MANIFEST" << std::endl; - } - - return true; - } - - void processLeavingInterest(ConsumerSocket &c, const Interest &interest) { - // std::cout << "LEAVES " << interest.getName().toUri() << std::endl; - } - - private: - int seen_manifest_segments_; - int seen_data_segments_; - int byte_counter_; - boost::asio::io_service io_service_; - std::shared_ptr<boost::asio::io_service::work> work_; - std::future<void> handler_; -}; - -class Verificator { - public: - Verificator() { - }; - - ~Verificator() { - } - - bool onPacket(ConsumerSocket &c, const ContentObject &contentObject) { - return true; - } - -}; - -void becomeDaemon() { - pid_t process_id = 0; - pid_t sid = 0; - - // Create child process - process_id = fork(); - - // Indication of fork() failure - if (process_id < 0) { - printf("fork failed!\n"); - // Return failure in exit status - exit(EXIT_FAILURE); - } - - // PARENT PROCESS. Need to kill it. - if (process_id > 0) { - printf("process_id of child process %d \n", process_id); - // 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. - chdir("/"); - - // Close stdin. stdout and stderr - close(STDIN_FILENO); - close(STDOUT_FILENO); - close(STDERR_FILENO); - - // Really start application -} - -int main(int argc, char **argv) { - double beta = DEFAULT_BETA; - double drop_factor = DEFAULT_GAMMA; - bool daemon = false; - bool rtt_stats = false; - int n_segment = 427; - bool looping = false; - - int opt; - while ((opt = getopt(argc, argv, "b:d:DRn:l")) != -1) { - switch (opt) { - case 'b': - beta = std::stod(optarg); - break; - case 'd': - drop_factor = std::stod(optarg); - break; - case 'D': - daemon = true; - break; - case 'R': - rtt_stats = true; - break; - case 'n': - n_segment = std::stoi(optarg); - break; - case 'l': - looping = true; - break; - default: - exit(EXIT_FAILURE); - } - } - - std::string name = "ccnx:/webserver/get/sintel/18000"; - - if (argv[optind] == 0) { - std::cerr << "Using default name ccnx:/webserver/sintel/18000" << std::endl; - } else { - name = argv[optind]; - } - - if (daemon) { - becomeDaemon(); - } - - ConsumerSocket c(Name(name.c_str()), TransportProtocolAlgorithms::RAAQM); - - CallbackContainer stubs; - Verificator verificator; - - c.setSocketOption(GeneralTransportOptions::INTEREST_LIFETIME, 1001); - c.setSocketOption(RaaqmTransportOptions::BETA_VALUE, beta); - c.setSocketOption(RaaqmTransportOptions::DROP_FACTOR, drop_factor); - c.setSocketOption(GeneralTransportOptions::MAX_INTEREST_RETX, 10); - c.setSocketOption(OtherOptions::VIRTUAL_DOWNLOAD, true); - c.setSocketOption(RaaqmTransportOptions::RTT_STATS, rtt_stats); - - c.setSocketOption(ConsumerCallbacksOptions::CONTENT_OBJECT_TO_VERIFY, - (ConsumerContentObjectVerificationCallback) std::bind(&Verificator::onPacket, - &verificator, - std::placeholders::_1, - std::placeholders::_2)); - - c.setSocketOption(ConsumerCallbacksOptions::CONTENT_RETRIEVED, - (ConsumerContentCallback) std::bind(&CallbackContainer::processPayload, - &stubs, - std::placeholders::_1, - std::placeholders::_2, - std::placeholders::_3)); - - c.setSocketOption(ConsumerCallbacksOptions::INTEREST_OUTPUT, - (ConsumerInterestCallback) std::bind(&CallbackContainer::processLeavingInterest, - &stubs, - std::placeholders::_1, - std::placeholders::_2)); - - do { - std::stringstream ss; - for (int i = 1; i < n_segment; i++) { - ss << "ccnx:/seg_" << i << ".m4s"; - auto str = ss.str(); - c.consume(Name(str)); - ss.str(""); - } - } while (looping); - - c.stop(); - - return 0; - -} - -} // end namespace icnet - -int main(int argc, char **argv) { - return icnet::main(argc, argv); -} diff --git a/apps/consumers/icnet_consumer_test.cc b/apps/consumers/icnet_consumer_test.cc index d5b57d6f..5c7eecb0 100755 --- a/apps/consumers/icnet_consumer_test.cc +++ b/apps/consumers/icnet_consumer_test.cc @@ -13,13 +13,16 @@ * limitations under the License. */ -#include "icnet_socket_consumer.h" +#include "icnet_transport_socket_consumer.h" +#include "icnet_utils_daemonizator.h" #define DEFAULT_BETA 0.99 #define DEFAULT_GAMMA 0.07 namespace icnet { +namespace transport { + class CallbackContainer { public: CallbackContainer() @@ -34,20 +37,20 @@ class CallbackContainer { work_.reset(); } - void processPayload(ConsumerSocket &c, const uint8_t *buffer, size_t bufferSize) { - std::cout << "Content retrieved!! Size: " << bufferSize << std::endl; + void processPayload(ConsumerSocket &c, std::vector<uint8_t> &&payload) { + std::cout << "Content retrieved!! Size: " << payload.size() << std::endl; - io_service_.dispatch([buffer, bufferSize]() { - std::ofstream file("ciao.txt", std::ofstream::binary); - file.write((char *) buffer, bufferSize); + io_service_.dispatch([payload]() { + std::ofstream file("consumer_test_file", std::ofstream::binary); + file.write((char *) payload.data(), payload.size()); file.close(); }); } bool verifyData(ConsumerSocket &c, const ContentObject &contentObject) { - if (contentObject.getContentType() == PayloadType::DATA) { + if (contentObject.getPayloadType() == PayloadType::DATA) { std::cout << "VERIFY CONTENT" << std::endl; - } else if (contentObject.getContentType() == PayloadType::MANIFEST) { + } else if (contentObject.getPayloadType() == PayloadType::MANIFEST) { std::cout << "VERIFY MANIFEST" << std::endl; } @@ -82,48 +85,6 @@ class Verificator { } }; -void becomeDaemon() { - pid_t process_id = 0; - pid_t sid = 0; - - // Create child process - process_id = fork(); - - // Indication of fork() failure - if (process_id < 0) { - printf("fork failed!\n"); - // Return failure in exit status - exit(EXIT_FAILURE); - } - - // PARENT PROCESS. Need to kill it. - if (process_id > 0) { - printf("process_id of child process %d \n", process_id); - // 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. - chdir("/"); - - // Close stdin. stdout and stderr - close(STDIN_FILENO); - close(STDOUT_FILENO); - close(STDERR_FILENO); - - // Really start application -} - int main(int argc, char *argv[]) { double beta = DEFAULT_BETA; double dropFactor = DEFAULT_GAMMA; @@ -159,7 +120,7 @@ int main(int argc, char *argv[]) { } if (daemon) { - becomeDaemon(); + utils::Daemonizator::daemonize(); } ConsumerSocket c(Name(name.c_str()), TransportProtocolAlgorithms::RAAQM); @@ -184,8 +145,7 @@ int main(int argc, char *argv[]) { (ConsumerContentCallback) std::bind(&CallbackContainer::processPayload, &stubs, std::placeholders::_1, - std::placeholders::_2, - std::placeholders::_3)); + std::placeholders::_2)); c.setSocketOption(ConsumerCallbacksOptions::INTEREST_OUTPUT, (ConsumerInterestCallback) std::bind(&CallbackContainer::processLeavingInterest, @@ -203,6 +163,8 @@ int main(int argc, char *argv[]) { } // end namespace icnet +} // end namespace transport + int main(int argc, char *argv[]) { - return icnet::main(argc, argv); + return icnet::transport::main(argc, argv); } diff --git a/apps/consumers/icnet_iget.cc b/apps/consumers/icnet_iget.cc deleted file mode 100755 index db5ef173..00000000 --- a/apps/consumers/icnet_iget.cc +++ /dev/null @@ -1,159 +0,0 @@ -/* - * 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_socket_consumer.h" - -typedef std::chrono::time_point<std::chrono::system_clock> Time; -typedef std::chrono::milliseconds TimeDuration; - -Time t1 = std::chrono::system_clock::now(); - -#define DEFAULT_BETA 0.99 -#define DEFAULT_GAMMA 0.07 - -namespace icnet { - -class CallbackContainer { - public: - CallbackContainer() - : work_(new boost::asio::io_service::work(io_service_)), - handler_(std::async(std::launch::async, [this]() { io_service_.run(); })) { - seen_manifest_segments_ = 0; - seen_data_segments_ = 0; - byte_counter_ = 0; - } - - ~CallbackContainer() { - work_.reset(); - } - - void processPayload(ConsumerSocket &c, const uint8_t *buffer, size_t buffer_size) { - Name m_name; - c.getSocketOption(GeneralTransportOptions::NAME_PREFIX, m_name); - std::string filename = m_name.toString().substr(1 + m_name.toString().find_last_of("/")); - io_service_.dispatch([buffer, buffer_size, filename]() { - std::cout << "Saving to: " << filename << " " << buffer_size / 1024 << "kB" << std::endl; - Time t3 = std::chrono::system_clock::now();; - std::ofstream file(filename.c_str(), std::ofstream::binary); - file.write((char *) buffer, buffer_size); - file.close(); - Time t2 = std::chrono::system_clock::now();; - TimeDuration dt = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1); - TimeDuration dt3 = std::chrono::duration_cast<std::chrono::milliseconds>(t3 - t1); - long msec = dt.count(); - long msec3 = dt3.count(); - std::cout << "Elapsed Time: " << msec / 1000.0 << " seconds -- " << buffer_size * 8 / msec / 1000.0 - << "[Mbps] -- " << buffer_size * 8 / msec3 / 1000.0 << "[Mbps]" << std::endl; - }); - } - - bool verifyData(ConsumerSocket &c, const ContentObject &content_object) { - if (content_object.getContentType() == PayloadType::DATA) { - std::cout << "VERIFY CONTENT" << std::endl; - } else if (content_object.getContentType() == PayloadType::MANIFEST) { - std::cout << "VERIFY MANIFEST" << std::endl; - } - - return true; - } - - void processLeavingInterest(ConsumerSocket &c, const Interest &interest) { - // std::cout << "OUTPUT: " << interest.getName() << std::endl; - } - - private: - int seen_manifest_segments_; - int seen_data_segments_; - int byte_counter_; - boost::asio::io_service io_service_; - std::shared_ptr<boost::asio::io_service::work> work_; - std::future<void> handler_; -}; - -/* - * The client signature verification is currently being reworked with the new API. - * The implementation is disabled for the moment. - */ - -class Verificator { - public: - Verificator() { - }; - - ~Verificator() { - // m_keyChain.deleteIdentity(Name(IDENTITY_NAME)); - } - - bool onPacket(ConsumerSocket &c, const ContentObject &contentObject) { - return true; - } -}; - -int main(int argc, char **argv) { - - std::string url = ""; - std::string locator = ""; - std::string path = ""; - std::string name = "ccnx:/locator/get/path"; - size_t found = 0; - size_t path_begin = 0; - - if (argv[optind] == 0) { - std::cerr << "Missing URL" << std::endl; - return 0; - } else { - url = argv[optind]; - std::cout << "Iget " << url << std::endl; - } - - found = url.find("//"); - path_begin = url.find('/', found + 2); - locator = url.substr(found + 2, path_begin - (found + 2)); - path = url.substr(path_begin, std::string::npos); - std::cout << "locator " << locator << std::endl; - std::cout << "path " << path << std::endl; - name = "ccnx:/" + locator + "/get" + path; - std::cout << "Iget ccnx name: " << name << std::endl; - - ConsumerSocket c(Name(name.c_str()), TransportProtocolAlgorithms::RAAQM); - CallbackContainer stubs; - Verificator verificator; - - c.setSocketOption(ConsumerCallbacksOptions::CONTENT_OBJECT_TO_VERIFY, - (ConsumerContentObjectVerificationCallback) std::bind(&Verificator::onPacket, - &verificator, - std::placeholders::_1, - std::placeholders::_2)); - c.setSocketOption(ConsumerCallbacksOptions::CONTENT_RETRIEVED, - (ConsumerContentCallback) std::bind(&CallbackContainer::processPayload, - &stubs, - std::placeholders::_1, - std::placeholders::_2, - std::placeholders::_3)); - c.setSocketOption(ConsumerCallbacksOptions::INTEREST_OUTPUT, - (ConsumerInterestCallback) std::bind(&CallbackContainer::processLeavingInterest, - &stubs, - std::placeholders::_1, - std::placeholders::_2)); - c.consume(Name()); - c.stop(); - return 0; -} - -} // end namespace icnet - -int main(int argc, char **argv) { - return icnet::main(argc, argv); -} diff --git a/apps/general/icnet_general_test.cc b/apps/general/icnet_general_test.cc index 88b0203d..e60cb887 100755 --- a/apps/general/icnet_general_test.cc +++ b/apps/general/icnet_general_test.cc @@ -13,7 +13,7 @@ * limitations under the License. */ -#include "icnet_socket.h" +#include "icnet_transport_socket.h" namespace icnet { diff --git a/apps/http/CMakeLists.txt b/apps/http/CMakeLists.txt new file mode 100644 index 00000000..931fe840 --- /dev/null +++ b/apps/http/CMakeLists.txt @@ -0,0 +1,31 @@ +# 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. + +cmake_minimum_required(VERSION 3.2) + +set(HTTP_CLIENT_SOURCE_FILES icnet_http_dash_client.cc) +set(HTTP_SERVER_SOURCE_FILES icnet_http_echo_server.cc) +set(IGET_SOURCE_FILES icnet_iget.cc) + +add_executable(http-dash-client ${HTTP_CLIENT_SOURCE_FILES}) +add_executable(http-echo-server ${HTTP_SERVER_SOURCE_FILES}) +add_executable(iget ${IGET_SOURCE_FILES}) + + +target_link_libraries(http-dash-client icnet ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES}) +target_link_libraries(http-echo-server icnet ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES}) +target_link_libraries(iget icnet ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES}) + +install(TARGETS http-dash-client DESTINATION bin COMPONENT library) +install(TARGETS iget DESTINATION bin COMPONENT library) +install(TARGETS http-echo-server DESTINATION bin COMPONENT library)
\ No newline at end of file diff --git a/apps/http/icnet_http_dash_client.cc b/apps/http/icnet_http_dash_client.cc new file mode 100644 index 00000000..f34b8503 --- /dev/null +++ b/apps/http/icnet_http_dash_client.cc @@ -0,0 +1,84 @@ +/* + * 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_http_client_connection.h" +#include "icnet_utils_daemonizator.h" + +namespace icnet { + +namespace http { + +void usage(int argc, char **argv) { + std::cout << "Usage:" << std::endl; + std::cout << argv[0] << " [-D] " << "[URL]" << std::endl; + exit(EXIT_FAILURE); +} + +int main(int argc, char **argv) { + + std::string name("http://webserver/sintel/18000"); + uint32_t n_segment = 300; + bool daemon = false; + + int opt; + while ((opt = getopt(argc, argv, "Dh")) != -1) { + switch (opt) { + case 'D': + daemon = true; + break; + case 'n': + n_segment = (uint32_t) atoi(optarg); + break; + case 'h': + default: + usage(argc, argv); + } + } + + if (argv[optind] == 0) { + std::cerr << "Using default name " << name << std::endl; + } else { + name = argv[optind]; + } + + if (daemon) { + utils::Daemonizator::daemonize(); + } + + HTTPClientConnection connection; + HTTPResponse response; + + std::stringstream ss; + for (uint32_t i = 1; i < n_segment; i++) { + ss << name; + ss << "/seg_" << i << ".m4s"; + auto str = ss.str(); + connection.get(str); + response = connection.response(); + std::cout << "SIZE: " << response.size() << std::endl; + std::cout << (char *) response.data() << std::endl; + ss.str(""); + } + + return EXIT_SUCCESS; +} + +} + +} + +int main(int argc, char **argv) { + return icnet::http::main(argc, argv); +}
\ No newline at end of file diff --git a/apps/http/icnet_http_echo_server.cc b/apps/http/icnet_http_echo_server.cc new file mode 100644 index 00000000..17bdb693 --- /dev/null +++ b/apps/http/icnet_http_echo_server.cc @@ -0,0 +1,53 @@ +/* + * 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_http_server_acceptor.h" + +namespace icnet { + +namespace http { + +void onPayload(std::shared_ptr<HTTPServerPublisher> &publisher, const uint8_t *buffer, std::size_t size) { + + char *string = (char *) buffer; + std::cout << "Received this content:" << std::endl; + std::cout << string << std::endl; + + std::stringstream response; + response << "HTTP/1.0 200 OK\r\n" << "Content-Length: " << size << "\r\n\r\n" << string; + std::string response_string = response.str(); + + std::thread t([publisher, response_string]() { + publisher->publishContent((uint8_t *) response_string.data(), response_string.size(), 0, true); + publisher->serveClients(); + }); + + t.detach(); +} + +int main(int argc, char **argv) { + HTTPServerAcceptor connection(std::string("http://webserver"), onPayload); + connection.listen(false); + + return EXIT_SUCCESS; +} + +} + +} + +int main(int argc, char **argv) { + return icnet::http::main(argc, argv); +}
\ No newline at end of file diff --git a/apps/http/icnet_iget.cc b/apps/http/icnet_iget.cc new file mode 100644 index 00000000..d322cc35 --- /dev/null +++ b/apps/http/icnet_iget.cc @@ -0,0 +1,71 @@ +/* + * 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_http_client_connection.h" + +typedef std::chrono::time_point<std::chrono::system_clock> Time; +typedef std::chrono::milliseconds TimeDuration; + +Time t1 = std::chrono::system_clock::now(); + +#define DEFAULT_BETA 0.99 +#define DEFAULT_GAMMA 0.07 + +namespace icnet { + +namespace http { + +void processResponse(std::string &name, HTTPResponse &&response) { + + std::string filename = name.substr(1 + name.find_last_of("/")); + std::cout << "Saving to: " << filename << " " << response.size() / 1024 << "kB" << std::endl; + Time t3 = std::chrono::system_clock::now();; + std::ofstream file(filename.c_str(), std::ofstream::binary); + file.write((char *) response.data(), response.size()); + file.close(); + Time t2 = std::chrono::system_clock::now();; + TimeDuration dt = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1); + TimeDuration dt3 = std::chrono::duration_cast<std::chrono::milliseconds>(t3 - t1); + long msec = dt.count(); + long msec3 = dt3.count(); + std::cout << "Elapsed Time: " << msec / 1000.0 << " seconds -- " << response.size() * 8 / msec / 1000.0 + << "[Mbps] -- " << response.size() * 8 / msec3 / 1000.0 << "[Mbps]" << std::endl; + +} + +int main(int argc, char **argv) { + + std::string name("http://webserver/sintel/mpd"); + + if (argv[optind] == 0) { + std::cerr << "Using default name http://webserver/sintel/mpd" << std::endl; + } else { + name = argv[optind]; + } + + HTTPClientConnection connection; + connection.get(name); + processResponse(name, connection.response()); + + return EXIT_SUCCESS; +} + +} // end namespace http + +} // end namespace hicnet + +int main(int argc, char **argv) { + return icnet::http::main(argc, argv); +}
\ No newline at end of file diff --git a/apps/iping/iPing_Client.c b/apps/iping/iPing_Client.c index 85d92eb0..ea6b0711 100644 --- a/apps/iping/iPing_Client.c +++ b/apps/iping/iPing_Client.c @@ -315,12 +315,11 @@ static bool _ccnxPingClient_ParseCommandline(CCNxPingClient *client, int argc, c static struct option longopts[] = {{"ping", no_argument, NULL, 'p'}, {"flood", no_argument, NULL, 'f'}, {"count", required_argument, NULL, 'c'}, { "size", required_argument, NULL, 's' - }, {"interval", required_argument, NULL, 'i'}, {"locator", required_argument, NULL, 'l'}, {"outstanding" - , required_argument - , NULL, 'o' - }, {"help", no_argument, NULL, 'h'}, {"timeout", required_argument, NULL, 't'}, {"lifetime", required_argument - , NULL, 'e' - }, {NULL, 0, NULL, 0}}; + }, {"interval", required_argument, NULL, 'i'}, {"locator", required_argument, NULL, 'l'}, + {"outstanding", required_argument, NULL, 'o' + }, {"help", no_argument, NULL, 'h'}, {"timeout", required_argument, NULL, 't'}, + {"lifetime", required_argument, NULL, 'e' + }, {NULL, 0, NULL, 0}}; client->payloadSize = ccnxPing_DefaultPayloadSize; diff --git a/apps/iping/iPing_Server.c b/apps/iping/iPing_Server.c index d3e5211c..eb9543a3 100644 --- a/apps/iping/iPing_Server.c +++ b/apps/iping/iPing_Server.c @@ -167,9 +167,9 @@ static void _displayUsage(char *progName) { */ static bool _ccnxPingServer_ParseCommandline(CCNxPingServer *server, int argc, char *argv[argc]) { static struct option longopts[] = - {{"locator", required_argument, NULL, 'l'}, {"size", required_argument, NULL, 's'}, {"help", no_argument, NULL - , 'h' - }, {NULL, 0, NULL, 0}}; + {{"locator", required_argument, NULL, 'l'}, {"size", required_argument, NULL, 's'}, + {"help", no_argument, NULL, 'h' + }, {NULL, 0, NULL, 0}}; // Default value server->payloadSize = ccnxPing_MaxPayloadSize; diff --git a/apps/producers/icnet_producer_test.cc b/apps/producers/icnet_producer_test.cc index c122090a..f35d082a 100755 --- a/apps/producers/icnet_producer_test.cc +++ b/apps/producers/icnet_producer_test.cc @@ -13,19 +13,22 @@ * limitations under the License. */ -#include "icnet_socket_producer.h" +#include "icnet_transport_socket_producer.h" +#include "icnet_utils_daemonizator.h" #define IDENTITY_NAME "cisco" namespace icnet { +namespace transport { + class CallbackContainer { public: CallbackContainer(unsigned long download_size = 0) : buffer_(1400, 'X'), final_chunk_number_(0) { content_object_.setContent((uint8_t *) buffer_.c_str(), 1400); if (download_size > 0) { - final_chunk_number_ = static_cast<uint64_t>(std::ceil(download_size / 1400.0)); + final_chunk_number_ = static_cast<uint64_t > (std::ceil(download_size / 1400.0)); } } @@ -66,48 +69,6 @@ class Signer { Name identity_name_; }; -void becomeDaemon() { - pid_t process_id = 0; - pid_t sid = 0; - - // Create child process - process_id = fork(); - - // Indication of fork() failure - if (process_id < 0) { - printf("fork failed!\n"); - // Return failure in exit status - exit(EXIT_FAILURE); - } - - // PARENT PROCESS. Need to kill it. - if (process_id > 0) { - printf("process_id of child process %d \n", process_id); - // 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. - chdir("/"); - - // Close stdin. stdout and stderr - close(STDIN_FILENO); - close(STDOUT_FILENO); - close(STDERR_FILENO); - - // Really start application -} - int main(int argc, char **argv) { std::string name = "ccnx:/ccnxtest"; unsigned long download_size = 0; @@ -135,7 +96,7 @@ int main(int argc, char **argv) { } if (daemon) { - becomeDaemon(); + utils::Daemonizator::daemonize(); } CallbackContainer stubs(download_size); @@ -168,9 +129,11 @@ int main(int argc, char **argv) { return 0; } +} // end namespace transport + } // end namespace icnet int main(int argc, char **argv) { - return icnet::main(argc, argv); + return icnet::transport::main(argc, argv); } diff --git a/dockerfile.ubuntu.xenial b/dockerfile.ubuntu.xenial new file mode 100644 index 00000000..182d19da --- /dev/null +++ b/dockerfile.ubuntu.xenial @@ -0,0 +1,19 @@ +# +# Ubuntu Dockerfile +# +# https://github.com/dockerfile/ubuntu +# + +# Pull base image. +FROM ubuntu:xenial + +# Install. +RUN \ + sed -i 's/# \(.*multiverse$\)/\1/g' /etc/apt/sources.list && \ + apt-get update && \ + apt-get -y upgrade && \ + apt-get install -y build-essential cmake software-properties-common apt-transport-https && \ + echo "deb [trusted=yes] http://nexus.fd.io/content/repositories/fd.io.master.ubuntu.xenial.main ./" | tee /etc/apt/sources.list.d/99fd.io.master.list && \ + apt-get update && \ + apt-get install -y libevent-dev libssl-dev longbow libparc libccnx-common libccnx-transport-rta libccnx-portal libboost-system-dev && \ + rm -rf /var/lib/apt/lists/* diff --git a/icnet/CMakeLists.txt b/icnet/CMakeLists.txt index fae22d61..427a3d9f 100644 --- a/icnet/CMakeLists.txt +++ b/icnet/CMakeLists.txt @@ -13,65 +13,149 @@ cmake_minimum_required(VERSION 3.2) -file(GLOB HEADER_FILES "ccnx/*.h") -file(GLOB SOURCE_FILES "ccnx/*.cc") +set(CCNX_HEADER_FILES + ccnx/icnet_ccnx_common.h + ccnx/icnet_ccnx_content_object.h + ccnx/icnet_ccnx_facade.h + ccnx/icnet_ccnx_interest.h + ccnx/icnet_ccnx_key_locator.h + ccnx/icnet_ccnx_key_locator_type.h + ccnx/icnet_ccnx_local_connector.h + ccnx/icnet_ccnx_manifest.h + ccnx/icnet_ccnx_name.h + ccnx/icnet_ccnx_network_message.h + ccnx/icnet_ccnx_payload_type.h + ccnx/icnet_ccnx_pending_interest.h + ccnx/icnet_ccnx_portal.h + ccnx/icnet_ccnx_segment.h) -set(ICNET_HEADER_FILES +set(CCNX_SOURCE_FILES + ccnx/icnet_ccnx_segment.cc + ccnx/icnet_ccnx_portal.cc + ccnx/icnet_ccnx_pending_interest.cc + ccnx/icnet_ccnx_network_message.cc + ccnx/icnet_ccnx_name.cc + ccnx/icnet_ccnx_manifest.cc + ccnx/icnet_ccnx_local_connector.cc + ccnx/icnet_ccnx_key_locator.cc + ccnx/icnet_ccnx_interest.cc + ccnx/icnet_ccnx_content_object.cc) + +set(TRANSPORT_HEADER_FILES ${CMAKE_BINARY_DIR}/config.hpp - transport/icnet_rate_estimation.h - transport/icnet_download_observer.h - transport/icnet_socket_consumer.h - transport/icnet_socket.h - transport/icnet_socket_options_default_values.h - transport/icnet_socket_options_keys.h - transport/icnet_common.h - transport/icnet_socket_producer.h - transport/icnet_content_store.h + transport/icnet_transport_rate_estimation.h + transport/icnet_transport_download_observer.h + transport/icnet_transport_socket_consumer.h + transport/icnet_transport_socket.h + transport/icnet_transport_socket_options_default_values.h + transport/icnet_transport_socket_options_keys.h + transport/icnet_transport_common.h + transport/icnet_transport_socket_producer.h + transport/icnet_transport_content_store.h transport/icnet_transport_vegas.h - transport/icnet_transport.h + transport/icnet_transport_protocol.h transport/icnet_transport_raaqm.h transport/icnet_transport_vegas_rto_estimator.h transport/icnet_transport_raaqm_data_path.h) -set(ICNET_SOURCE_FILES - transport/icnet_socket_producer.cc - transport/icnet_socket_consumer.cc +set(TRANSPORT_SOURCE_FILES + transport/icnet_transport_socket_producer.cc + transport/icnet_transport_socket_consumer.cc transport/icnet_transport_vegas.cc - transport/icnet_transport.cc - transport/icnet_content_store.cc + transport/icnet_transport_protocol.cc + transport/icnet_transport_content_store.cc transport/icnet_transport_raaqm.cc transport/icnet_transport_vegas_rto_estimator.cc - transport/icnet_rate_estimation.cc + transport/icnet_transport_rate_estimation.cc transport/icnet_transport_raaqm_data_path.cc) +set(ERRORS_HEADER_FILES + errors/icnet_errors_tokenizer_exception.h + errors/icnet_errors.h + errors/icnet_errors_malformed_name_exception.h + errors/icnet_errors_not_implemented_exception.h + errors/icnet_errors_runtime_exception.h + errors/icnet_errors_malformed_packet_exception.h) + +set(ERRORS_SOURCE_FILES + errors/icnet_errors_tokenizer_exception.cc + errors/icnet_errors_malformed_name_exception.cc + errors/icnet_errors_not_implemented_exception.cc + errors/icnet_errors_runtime_exception.cc + errors/icnet_errors_malformed_packet_exception.cc) + +set(HTTP_HEADER_FILES + http/icnet_http_default_values.h + http/icnet_http_client_connection.h + http/icnet_http_server_acceptor.h + http/icnet_http_server_publisher.h + http/icnet_http_request.h + http/icnet_http_facade.h) + +set(HTTP_SOURCE_FILES + http/icnet_http_client_connection.cc + http/icnet_http_server_acceptor.cc + http/icnet_http_server_publisher.cc + http/icnet_http_request.cc) + +set(UTILS_HEADER_FILES + utils/icnet_utils_array.h + utils/icnet_utils_uri.h + utils/icnet_utils_daemonizator.h + utils/icnet_utils_hash.h + utils/icnet_utils_string_tokenizer.h) + +set(UTILS_SOURCE_FILES + utils/icnet_utils_array.cc + utils/icnet_utils_uri.cc + utils/icnet_utils_daemonizator.cc + utils/icnet_utils_hash.cc + utils/icnet_utils_string_tokenizer.cc) + set(ICNET_CONFIG transport/consumer.conf) set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib") -add_library(icnet STATIC ${SOURCE_FILES} ${ICNET_SOURCE_FILES} ${HEADER_FILES} ${ICNET_HEADER_FILES}) -add_library(icnet.shared SHARED ${SOURCE_FILES} ${ICNET_SOURCE_FILES}) -if(ANDROID_API) - target_link_libraries(icnet.shared ${LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES} ${CCNX_COMMON_LIBRARIES} ${CCNX_TRANSPORT_RTA_LIBRARIES} ${CCNX_PORTAL_LIBRARIES} ${LIBPARC_LIBRARIES} ${LONGBOW_LIBRARIES}) - target_link_libraries(icnet ${LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES} ${CCNX_COMMON_LIBRARIES} ${CCNX_TRANSPORT_RTA_LIBRARIES} ${CCNX_PORTAL_LIBRARIES} ${LIBPARC_LIBRARIES} ${LONGBOW_LIBRARIES}) -else () - target_link_libraries(icnet.shared ${LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES}) +add_library(icnet STATIC + ${CCNX_SOURCE_FILES} + ${TRANSPORT_SOURCE_FILES} + ${ERRORS_SOURCE_FILES} + ${HTTP_SOURCE_FILES} + ${UTILS_SOURCE_FILES}) + +if (COMPILE_FOR_IOS OR ANDROID_API) target_link_libraries(icnet ${LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES}) -endif (ANDROID_API) + set(libicnet_libraries + icnet) +else() -set_target_properties(icnet.shared PROPERTIES - SOVERSION 1 - VERSION 1.0 - OUTPUT_NAME icnet) + add_library(icnet.shared SHARED + ${CCNX_SOURCE_FILES} + ${TRANSPORT_SOURCE_FILES} + ${ERRORS_SOURCE_FILES} + ${HTTP_SOURCE_FILES} + ${UTILS_SOURCE_FILES}) + + target_link_libraries(icnet.shared ${LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES}) + target_link_libraries(icnet ${LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES}) + set_target_properties(icnet.shared PROPERTIES + SOVERSION 1 + VERSION 1.0 + OUTPUT_NAME icnet) -set(libicnet_libraries - icnet - icnet.shared) + set(libicnet_libraries + icnet + icnet.shared) +endif() foreach(lib ${libicnet_libraries}) install(TARGETS ${lib} COMPONENT library LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/lib) endforeach() -install(FILES ${HEADER_FILES} DESTINATION ${CMAKE_INSTALL_PREFIX}/include/icnet COMPONENT headers) -install(FILES ${ICNET_HEADER_FILES} DESTINATION ${CMAKE_INSTALL_PREFIX}/include/icnet COMPONENT headers) +install(FILES ${CCNX_HEADER_FILES} DESTINATION ${CMAKE_INSTALL_PREFIX}/include/icnet COMPONENT headers) +install(FILES ${TRANSPORT_HEADER_FILES} DESTINATION ${CMAKE_INSTALL_PREFIX}/include/icnet COMPONENT headers) +install(FILES ${HTTP_HEADER_FILES} DESTINATION ${CMAKE_INSTALL_PREFIX}/include/icnet COMPONENT headers) +install(FILES ${ERRORS_HEADER_FILES} DESTINATION ${CMAKE_INSTALL_PREFIX}/include/icnet COMPONENT headers) +install(FILES ${UTILS_HEADER_FILES} DESTINATION ${CMAKE_INSTALL_PREFIX}/include/icnet COMPONENT headers) install(FILES ${ICNET_CONFIG} DESTINATION ${CMAKE_INSTALL_PREFIX}/etc/ COMPONENT library) diff --git a/icnet/ccnx/icnet_ccnx_content_object.cc b/icnet/ccnx/icnet_ccnx_content_object.cc index 03875724..e4070194 100644 --- a/icnet/ccnx/icnet_ccnx_content_object.cc +++ b/icnet/ccnx/icnet_ccnx_content_object.cc @@ -63,11 +63,11 @@ bool ContentObject::operator==(const ContentObject &content_object) { return ccnxContentObject_Equals(ccnx_content_object_, content_object.ccnx_content_object_); } -PayloadType ContentObject::getContentType() const { +PayloadType ContentObject::getPayloadType() const { return (PayloadType) ccnxContentObject_GetPayloadType(ccnx_content_object_); } -void ContentObject::setContentType(PayloadType payload_type) { +void ContentObject::setPayloadType(PayloadType payload_type) { content_type_ = payload_type; } diff --git a/icnet/ccnx/icnet_ccnx_content_object.h b/icnet/ccnx/icnet_ccnx_content_object.h index 148587bb..e7e641e6 100644 --- a/icnet/ccnx/icnet_ccnx_content_object.h +++ b/icnet/ccnx/icnet_ccnx_content_object.h @@ -71,13 +71,13 @@ class ContentObject : public std::enable_shared_from_this<ContentObject> { bool operator==(const ContentObject &content_object); - PayloadType getContentType() const; + PayloadType getPayloadType() const; bool setContent(PayloadType content_type, const uint8_t *buffer, size_t buffer_size); bool setContent(const uint8_t *buffer, size_t buffer_size); - void setContentType(PayloadType payload_type); + void setPayloadType(PayloadType payload_type); Array getContent() const; diff --git a/icnet/ccnx/icnet_ccnx_interest.cc b/icnet/ccnx/icnet_ccnx_interest.cc index 6b0f92cb..d578d550 100644 --- a/icnet/ccnx/icnet_ccnx_interest.cc +++ b/icnet/ccnx/icnet_ccnx_interest.cc @@ -117,6 +117,13 @@ bool Interest::setPayload(const PARCBuffer *payload) { return ccnxInterest_SetPayload(interest_, payload); } +bool Interest::setPayload(const uint8_t *buffer, std::size_t size) { + PARCBuffer *pbuffer = parcBuffer_CreateFromArray(buffer, size); + bool ret = setPayload(pbuffer); + parcBuffer_Release(&pbuffer); + return ret; +} + bool Interest::setPayloadAndId(const PARCBuffer *payload) { return ccnxInterest_SetPayloadAndId(interest_, payload); } @@ -125,8 +132,9 @@ bool Interest::setPayloadWithId(const PARCBuffer *payload, const CCNxInterestPay return ccnxInterest_SetPayloadWithId(interest_, payload, payload_id); } -PARCBuffer *Interest::getPayload() { - return ccnxInterest_GetPayload(interest_); +utils::Array Interest::getPayload() const { + PARCBuffer *buffer = ccnxInterest_GetPayload(interest_); + return utils::Array(parcBuffer_Overlay(buffer, 0), parcBuffer_Remaining(buffer)); } void Interest::setHopLimit(uint32_t hop_limit) { diff --git a/icnet/ccnx/icnet_ccnx_interest.h b/icnet/ccnx/icnet_ccnx_interest.h index 2156b56f..47af3f68 100644 --- a/icnet/ccnx/icnet_ccnx_interest.h +++ b/icnet/ccnx/icnet_ccnx_interest.h @@ -17,14 +17,13 @@ #define ICNET_CCNX_INTEREST_H_ #include "icnet_ccnx_common.h" +#include "icnet_utils_array.h" +#include "icnet_ccnx_name.h" extern "C" { #include <ccnx/common/ccnx_Interest.h> }; -//#include "interest.hpp" -#include "icnet_ccnx_name.h" - namespace icnet { namespace ccnx { @@ -67,11 +66,13 @@ class Interest : public std::enable_shared_from_this<Interest> { bool setPayload(const PARCBuffer *payload); + bool setPayload(const uint8_t *buffer, std::size_t size); + bool setPayloadAndId(const PARCBuffer *payload); bool setPayloadWithId(const PARCBuffer *payload, const CCNxInterestPayloadId *payload_id); - PARCBuffer *getPayload(); + utils::Array getPayload() const; void setHopLimit(uint32_t hop_limit); diff --git a/icnet/ccnx/icnet_ccnx_local_connector.cc b/icnet/ccnx/icnet_ccnx_local_connector.cc index cd828b08..75691979 100644 --- a/icnet/ccnx/icnet_ccnx_local_connector.cc +++ b/icnet/ccnx/icnet_ccnx_local_connector.cc @@ -136,7 +136,7 @@ void LocalConnector::doReadHeader() { void LocalConnector::tryReconnect() { if (!is_connecting_) { #ifdef __ANDROID__ - __android_log_print(ANDROID_LOG_DEBUG, "libICNet", "Connection lost. Trying to reconnect...\n"); + __android_log_print(ANDROID_LOG_DEBUG, "libICNet", "Connection lost. Trying to reconnect...\n"); #else std::cerr << "Connection lost. Trying to reconnect..." << std::endl; #endif diff --git a/icnet/errors/icnet_errors.h b/icnet/errors/icnet_errors.h new file mode 100644 index 00000000..34ba11af --- /dev/null +++ b/icnet/errors/icnet_errors.h @@ -0,0 +1,21 @@ +/* + * 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 "icnet_errors_malformed_name_exception.h" +#include "icnet_errors_not_implemented_exception.h" +#include "icnet_errors_malformed_packet_exception.h" +#include "icnet_errors_runtime_exception.h" diff --git a/icnet/errors/icnet_errors_malformed_name_exception.cc b/icnet/errors/icnet_errors_malformed_name_exception.cc new file mode 100644 index 00000000..361c211a --- /dev/null +++ b/icnet/errors/icnet_errors_malformed_name_exception.cc @@ -0,0 +1,32 @@ +/* + * 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.h" + +namespace icnet { + +namespace errors { + +MalformedNameException::MalformedNameException() + : std::runtime_error("") { +} + +char const *MalformedNameException::what() const noexcept { + return "Malformed IP address."; +} + +} // end namespace errors + +} // end namespace icnet
\ No newline at end of file diff --git a/icnet/errors/icnet_errors_malformed_name_exception.h b/icnet/errors/icnet_errors_malformed_name_exception.h new file mode 100644 index 00000000..f714d2b0 --- /dev/null +++ b/icnet/errors/icnet_errors_malformed_name_exception.h @@ -0,0 +1,32 @@ +/* + * 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 <stdexcept> + +namespace icnet { + +namespace errors { + +class MalformedNameException : public std::runtime_error { + public: + MalformedNameException(); + virtual char const *what() const noexcept override; +}; + +} // end namespace errors + +} // end namespace icnet
\ No newline at end of file diff --git a/icnet/errors/icnet_errors_malformed_packet_exception.cc b/icnet/errors/icnet_errors_malformed_packet_exception.cc new file mode 100644 index 00000000..962a6582 --- /dev/null +++ b/icnet/errors/icnet_errors_malformed_packet_exception.cc @@ -0,0 +1,32 @@ +/* + * 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_malformed_packet_exception.h" + +namespace icnet { + +namespace errors { + +MalformedPacketException::MalformedPacketException() + : std::runtime_error("") { +} + +char const *MalformedPacketException::what() const noexcept { + return "Malformed IP packet."; +} + +} // end namespace errors + +} // end namespace icnet
\ No newline at end of file diff --git a/icnet/errors/icnet_errors_malformed_packet_exception.h b/icnet/errors/icnet_errors_malformed_packet_exception.h new file mode 100644 index 00000000..0420d5c1 --- /dev/null +++ b/icnet/errors/icnet_errors_malformed_packet_exception.h @@ -0,0 +1,32 @@ +/* + * 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 <stdexcept> + +namespace icnet { + +namespace errors { + +class MalformedPacketException : public std::runtime_error { + public: + MalformedPacketException(); + virtual char const *what() const noexcept override; +}; + +} // end namespace errors + +} // end namespace icnet
\ No newline at end of file diff --git a/icnet/errors/icnet_errors_not_implemented_exception.cc b/icnet/errors/icnet_errors_not_implemented_exception.cc new file mode 100644 index 00000000..c58039c9 --- /dev/null +++ b/icnet/errors/icnet_errors_not_implemented_exception.cc @@ -0,0 +1,33 @@ +/* + * 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_not_implemented_exception.h" + +namespace icnet { + +namespace errors { + +NotImplementedException::NotImplementedException() + : std::logic_error("") { + +} + +char const *NotImplementedException::what() const noexcept { + return "Function not yet implemented."; +} + +} // end namespace errors + +} // end namespace icnet
\ No newline at end of file diff --git a/icnet/errors/icnet_errors_not_implemented_exception.h b/icnet/errors/icnet_errors_not_implemented_exception.h new file mode 100644 index 00000000..980ae99f --- /dev/null +++ b/icnet/errors/icnet_errors_not_implemented_exception.h @@ -0,0 +1,32 @@ +/* + * 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 <stdexcept> + +namespace icnet { + +namespace errors { + +class NotImplementedException : public std::logic_error { + public: + NotImplementedException(); + virtual char const *what() const noexcept override; +}; + +} // end namespace errors + +} // end namespace icnet
\ No newline at end of file diff --git a/icnet/errors/icnet_errors_runtime_exception.cc b/icnet/errors/icnet_errors_runtime_exception.cc new file mode 100644 index 00000000..4853f25c --- /dev/null +++ b/icnet/errors/icnet_errors_runtime_exception.cc @@ -0,0 +1,32 @@ +/* + * 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_runtime_exception.h" + +namespace icnet { + +namespace errors { + +RuntimeException::RuntimeException() + : std::runtime_error("") { +} + +//char const *Runtime::what() const { +// return "Function not yet implemented."; +//} + +} // end namespace errors + +} // end namespace icnet
\ No newline at end of file diff --git a/icnet/errors/icnet_errors_runtime_exception.h b/icnet/errors/icnet_errors_runtime_exception.h new file mode 100644 index 00000000..37c6bcb6 --- /dev/null +++ b/icnet/errors/icnet_errors_runtime_exception.h @@ -0,0 +1,34 @@ +/* + * 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 <stdexcept> +#include <string> + +namespace icnet { + +namespace errors { + +class RuntimeException : public std::runtime_error { + public: + RuntimeException(); + RuntimeException(std::string what) + : runtime_error(what) {}; +}; + +} // end namespace errors + +} // end namespace icnet
\ No newline at end of file diff --git a/icnet/errors/icnet_errors_tokenizer_exception.cc b/icnet/errors/icnet_errors_tokenizer_exception.cc new file mode 100644 index 00000000..1b5e8438 --- /dev/null +++ b/icnet/errors/icnet_errors_tokenizer_exception.cc @@ -0,0 +1,33 @@ +/* + * 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" + +namespace icnet { + +namespace errors { + +TokenizerException::TokenizerException() + : std::logic_error("") { + +} + +char const *TokenizerException::what() const noexcept { + return "No more tokens available."; +} + +} // end namespace errors + +} // end namespace icnet
\ No newline at end of file diff --git a/icnet/errors/icnet_errors_tokenizer_exception.h b/icnet/errors/icnet_errors_tokenizer_exception.h new file mode 100644 index 00000000..bae9d82a --- /dev/null +++ b/icnet/errors/icnet_errors_tokenizer_exception.h @@ -0,0 +1,32 @@ +/* + * 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 <stdexcept> + +namespace icnet { + +namespace errors { + +class TokenizerException : public std::logic_error { + public: + TokenizerException(); + virtual char const *what() const noexcept override; +}; + +} // end namespace errors + +} // end namespace icnet
\ No newline at end of file diff --git a/icnet/http/icnet_http_client_connection.cc b/icnet/http/icnet_http_client_connection.cc new file mode 100644 index 00000000..1f0d8fd8 --- /dev/null +++ b/icnet/http/icnet_http_client_connection.cc @@ -0,0 +1,119 @@ +/* + * 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_http_client_connection.h" + +#define DEFAULT_BETA 0.99 +#define DEFAULT_GAMMA 0.07 + +namespace icnet { + +namespace http { + +using namespace transport; + +HTTPClientConnection::HTTPClientConnection() + : consumer_(Name("ccnx:"), transport::TransportProtocolAlgorithms::RAAQM) { + + consumer_.setSocketOption(GeneralTransportOptions::INTEREST_LIFETIME, 1001); + consumer_.setSocketOption(RaaqmTransportOptions::BETA_VALUE, DEFAULT_BETA); + consumer_.setSocketOption(RaaqmTransportOptions::DROP_FACTOR, DEFAULT_GAMMA); + consumer_.setSocketOption(GeneralTransportOptions::MAX_INTEREST_RETX, 200); + + consumer_.setSocketOption(ConsumerCallbacksOptions::CONTENT_OBJECT_TO_VERIFY, + (ConsumerContentObjectVerificationCallback) std::bind(&HTTPClientConnection::verifyData, + this, + std::placeholders::_1, + std::placeholders::_2)); + + consumer_.setSocketOption(ConsumerCallbacksOptions::CONTENT_RETRIEVED, + (ConsumerContentCallback) std::bind(&HTTPClientConnection::processPayload, + this, + std::placeholders::_1, + std::placeholders::_2)); +} + +HTTPClientConnection &HTTPClientConnection::get(std::string &url, HTTPHeaders headers, HTTPPayload payload) { + + HTTPRequest request(GET, url, headers, payload); + + std::string &request_string = request.getRequestString(); + std::string &locator = request.getLocator(); + std::string &path = request.getPath(); + + consumer_.setSocketOption(ConsumerCallbacksOptions::INTEREST_OUTPUT, + (ConsumerInterestCallback) std::bind(&HTTPClientConnection::processLeavingInterest, + this, + std::placeholders::_1, + std::placeholders::_2, + request_string)); + + // Send content to producer piggybacking it through first interest (to fix) + + response_.clear(); + + // Factor icn name + + std::stringstream stream; + + stream << "ccnx:/"; + stream << locator << "/get"; + stream << path; + + consumer_.consume(Name(stream.str())); + + consumer_.stop(); + + return *this; +} + +HTTPResponse &&HTTPClientConnection::response() { + return std::move(response_); +} + +void HTTPClientConnection::processPayload(transport::ConsumerSocket &c, + std::vector<uint8_t> &&payload) { + response_ = std::move(payload); +} + +bool HTTPClientConnection::verifyData(transport::ConsumerSocket &c, const ContentObject &contentObject) { + if (contentObject.getPayloadType() == PayloadType::DATA) { + std::cout << "VERIFY CONTENT" << std::endl; + } else if (contentObject.getPayloadType() == PayloadType::MANIFEST) { + std::cout << "VERIFY MANIFEST" << std::endl; + } + + return true; +} + +void HTTPClientConnection::processLeavingInterest(transport::ConsumerSocket &c, + const Interest &interest, + std::string &payload) { + if (interest.getName().get(-1).toSegment() == 0) { + Interest &int2 = const_cast<Interest &>(interest); + int2.setPayload((const uint8_t *) payload.data(), payload.size()); + } +} + +HTTPClientConnection& HTTPClientConnection::stop() { + // This is thread safe and can be called from another thread + consumer_.stop(); + + return *this; +} + +} + +}
\ No newline at end of file diff --git a/icnet/http/icnet_http_client_connection.h b/icnet/http/icnet_http_client_connection.h new file mode 100644 index 00000000..5a009d88 --- /dev/null +++ b/icnet/http/icnet_http_client_connection.h @@ -0,0 +1,56 @@ +/* + * 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 "icnet_transport_socket_consumer.h" +#include "icnet_transport_socket_producer.h" +#include "icnet_utils_uri.h" +#include "icnet_http_request.h" +#include "icnet_http_default_values.h" + +#include <vector> + +#define HTTP_VERSION "1.0" + +namespace icnet { + +namespace http { + +class HTTPClientConnection { + public: + HTTPClientConnection(); + + HTTPClientConnection &get(std::string &url, HTTPHeaders headers = {}, HTTPPayload payload = {}); + + HTTPResponse &&response(); + + HTTPClientConnection &stop(); + + private: + + void processPayload(transport::ConsumerSocket &c, std::vector<uint8_t> &&payload); + + bool verifyData(transport::ConsumerSocket &c, const transport::ContentObject &contentObject); + + void processLeavingInterest(transport::ConsumerSocket &c, const transport::Interest &interest, std::string &payload); + + HTTPResponse response_; + transport::ConsumerSocket consumer_; +}; + +} // end namespace http + +} // end namespace icnet
\ No newline at end of file diff --git a/icnet/http/icnet_http_default_values.h b/icnet/http/icnet_http_default_values.h new file mode 100644 index 00000000..5aaf60d8 --- /dev/null +++ b/icnet/http/icnet_http_default_values.h @@ -0,0 +1,30 @@ +/* + * 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 hicnet { + +namespace http { + +namespace default_values { + +const uint16_t ipv6_first_word = 0xb001; // Network byte order + +} // end namespace transport + +} // end namespace default_values + +} // end namespace hicnet diff --git a/icnet/http/icnet_http_facade.h b/icnet/http/icnet_http_facade.h new file mode 100644 index 00000000..f32eff73 --- /dev/null +++ b/icnet/http/icnet_http_facade.h @@ -0,0 +1,22 @@ +/* + * 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 "icnet_http_server_acceptor.h" +#include "icnet_http_server_publisher.h" +#include "icnet_http_client_connection.h" + +namespace libl4 = icnet;
\ No newline at end of file diff --git a/icnet/http/icnet_http_request.cc b/icnet/http/icnet_http_request.cc new file mode 100644 index 00000000..cd0c512f --- /dev/null +++ b/icnet/http/icnet_http_request.cc @@ -0,0 +1,101 @@ +/* + * 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_http_request.h" +#include "icnet_utils_uri.h" + +namespace icnet { + +namespace http { + +static std::map<HTTPMethod, std::string> method_map = { + {GET, "GET"}, + {POST, "POST"}, + {PUT, "PUT"}, + {PATCH, "PATCH"}, + {DELETE, "DELETE"}, +}; + +//std::map<HTTPMethod, std::string> method_map + +HTTPRequest::HTTPRequest(HTTPMethod method, std::string &url, HTTPHeaders &headers, HTTPPayload &payload) { + utils::Uri uri; + uri.parse(url); + + path_ = uri.getPath(); + query_string_ = uri.getQueryString(); + protocol_ = uri.getProtocol(); + locator_ = uri.getLocator(); + port_ = uri.getPort(); + + headers_ = headers; + payload_ = payload; + + std::transform(locator_.begin(), + locator_.end(), + locator_.begin(), + ::tolower); + + std::transform(protocol_.begin(), + protocol_.end(), + protocol_.begin(), + ::tolower); + + std::stringstream stream; + stream << method_map[method] << " " << uri.getPath() << " HTTP/" << HTTP_VERSION << "\r\n"; + for (auto &item : headers) { + stream << item.first << ": " << item.second << "\r\n"; + } + stream << "\r\n"; + stream << payload.data(); + + request_string_ = stream.str(); +} + +std::string &HTTPRequest::getPort() { + return port_; +} + +std::string &HTTPRequest::getLocator() { + return locator_; +} + +std::string &HTTPRequest::getProtocol() { + return protocol_; +} + +std::string &HTTPRequest::getPath() { + return path_; +} + +std::string &HTTPRequest::getQueryString() { + return query_string_; +} + +HTTPHeaders &HTTPRequest::getHeaders() { + return headers_; +} + +HTTPPayload &HTTPRequest::getPayload() { + return payload_; +} + +std::string &HTTPRequest::getRequestString() { + return request_string_; +} + +} + +}
\ No newline at end of file diff --git a/icnet/http/icnet_http_request.h b/icnet/http/icnet_http_request.h new file mode 100644 index 00000000..985d7628 --- /dev/null +++ b/icnet/http/icnet_http_request.h @@ -0,0 +1,71 @@ +/* + * 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 <sstream> +#include <vector> +#include <map> + +#define HTTP_VERSION "1.0" + +namespace icnet { + +namespace http { + +typedef enum { + GET, + POST, + PUT, + PATCH, + DELETE +} HTTPMethod; + +typedef std::map<std::string, std::string> HTTPHeaders; +typedef std::vector<uint8_t> HTTPPayload; +typedef std::vector<uint8_t> HTTPResponse; + +class HTTPRequest { + public: + + HTTPRequest(HTTPMethod method, + std::string &url, HTTPHeaders &headers, HTTPPayload &payload); + + std::string &getQueryString(); + + std::string &getPath(); + + std::string &getProtocol(); + + std::string &getLocator(); + + std::string &getPort(); + + std::string &getRequestString(); + + HTTPHeaders &getHeaders(); + + HTTPPayload &getPayload(); + + private: + std::string query_string_, path_, protocol_, locator_, port_; + std::string request_string_; + HTTPHeaders headers_; + HTTPPayload payload_; +}; + +} // end namespace http + +} // end namespace icnet
\ No newline at end of file diff --git a/icnet/http/icnet_http_server_acceptor.cc b/icnet/http/icnet_http_server_acceptor.cc new file mode 100644 index 00000000..9406955d --- /dev/null +++ b/icnet/http/icnet_http_server_acceptor.cc @@ -0,0 +1,171 @@ +/* + * 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_http_server_acceptor.h" +#include "icnet_http_request.h" +#include "icnet_errors.h" +#include "icnet_utils_uri.h" +#include "icnet_utils_hash.h" + +namespace icnet { + +namespace http { + +HTTPServerAcceptor::HTTPServerAcceptor(std::string &&server_locator, OnHttpRequest callback) + : HTTPServerAcceptor(server_locator, callback) { +} + +HTTPServerAcceptor::HTTPServerAcceptor(std::string &server_locator, OnHttpRequest callback) + : callback_(callback) { + utils::Uri uri; + + uri.parseProtocolAndLocator(server_locator); + std::string protocol = uri.getProtocol(); + std::string locator = uri.getLocator(); + + std::transform(locator.begin(), + locator.end(), + locator.begin(), + ::tolower); + + std::transform(protocol.begin(), + protocol.end(), + protocol.begin(), + ::tolower); + + if (protocol != "http") { + throw errors::RuntimeException("Malformed server_locator. The locator format should be in the form http://locator"); + } + + std::stringstream ss; + + ss << "ccnx:/"; + ss << locator; + + acceptor_producer_ = std::make_shared<transport::ProducerSocket>(ss.str()); +} + +void HTTPServerAcceptor::listen(bool async) { + acceptor_producer_->setSocketOption(icnet::transport::ProducerCallbacksOptions::INTEREST_INPUT, + (icnet::transport::ProducerInterestCallback) bind(&HTTPServerAcceptor::processIncomingInterest, + this, + std::placeholders::_1, + std::placeholders::_2)); + acceptor_producer_->dispatch(); + + if (!async) { + acceptor_producer_->serveForever(); + } +} + +HttpRequest &&HTTPServerAcceptor::request() { + return std::move(request_); +} + +void HTTPServerAcceptor::processIncomingInterest(transport::ProducerSocket &p, const transport::Interest &interest) { + // Temporary solution + + transport::Name complete_name = interest.getName(); + + transport::Name request_name = complete_name.get(-1).isSegment() ? complete_name.getPrefix(-1) : complete_name; + transport::Name prefix; + acceptor_producer_->getSocketOption(transport::GeneralTransportOptions::NAME_PREFIX, prefix); + + // Get the name of the HTTP method to compute + std::string method = request_name.get(prefix.getSegmentCount()).toString(); + std::transform(method.begin(), method.end(), method.begin(), ::toupper); + std::string path; + std::string url_begin; + + // This is done for getting rid of useless name components such as ccnx: or ndn: + if (request_name.getSegmentCount() > 2) { + std::string raw_path = request_name.getSubName(prefix.getSegmentCount() + 1).toString(); + std::size_t pos = raw_path.find("/"); + path = raw_path.substr(pos); + url_begin = prefix.getSubName(0).toString().substr(pos); + } + + std::stringstream ss; + ss << "http:/" << url_begin << path; + + std::string url = ss.str(); + HTTPHeaders headers = {}; + HTTPPayload payload = {}; + + if (method == "GET") { + HTTPRequest request(GET, url, headers, payload); + auto publisher = std::make_shared<HTTPServerPublisher>(request_name); + callback_(publisher, (uint8_t *) request.getRequestString().data(), request.getRequestString().size()); + } +} + +//void HTTPServerConnection::sendResponse() { +// +// std::thread t([]() { +// +// // Get the name of the HTTP method to compute +// std::string method = request_name.get(1).toString(); +// std::transform(method.begin(), method.end(), method.begin(), ::toupper); +// std::string path; +// +// // This is done for getting rid of useless name components such as ccnx: or ndn: +// if (request_name.getSegmentCount() > 2) { +// std::string rawPath = request_name.getSubName(2).toString(); +// std::size_t pos = rawPath.find("/"); +// path = rawPath.substr(pos); +// } +// +// // Create new producer +// +// // Create timer for response availability +// std::shared_ptr<core::Portal> portal; +// po->getSocketOption(icnet::GeneralTransportOptions::PORTAL, portal); +// boost::asio::io_service &ioService = portal->getIoService(); +// +// boost::asio::deadline_timer t(ioService, boost::posix_time::seconds(5)); +// +// std::function<void(const boost::system::error_code e)> +// wait_callback = [&ioService](const boost::system::error_code e) { +// if (!e) { +// // Be sure to delete the timer before the io_service, otherwise we'll get some strange behavior! +// ioService.stop(); +// } +// }; +// +// std::function<void(transport::ProducerSocket, const core::Interest &interest)> +// interest_enter_callback = [this, &wait_callback, &t] +// (transport::ProducerSocket &p, const core::Interest &interest) { +// t.cancel(); +// t.expires_from_now(boost::posix_time::seconds(5)); +// t.async_wait(wait_callback); +// }; +// +// p->setSocketOption(transport::ProducerCallbacksOptions::INTEREST_INPUT, +// (transport::ProducerInterestCallback) interest_enter_callback); +// +// t.async_wait(wait_callback); +// +// p->serveForever(); +// +// std::unique_lock<std::mutex> lock(thread_list_mtx_); +// icn_producers_.erase(request_name); +// }); +// +// t.detach(); +//} + +} + +}
\ No newline at end of file diff --git a/icnet/http/icnet_http_server_acceptor.h b/icnet/http/icnet_http_server_acceptor.h new file mode 100644 index 00000000..2d0b7f25 --- /dev/null +++ b/icnet/http/icnet_http_server_acceptor.h @@ -0,0 +1,62 @@ +/* + * 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 "icnet_transport_socket_consumer.h" +#include "icnet_transport_socket_producer.h" +#include "icnet_http_default_values.h" +#include "icnet_http_server_publisher.h" + +#include <vector> +#include <functional> + +#define HTTP_VERSION "1.0" + +namespace icnet { + +namespace http { + +//typedef std::vector<uint8_t> HTTPResponse; +typedef std::vector<uint8_t> HttpRequest; +typedef std::function<void(std::shared_ptr<HTTPServerPublisher> &, const uint8_t *, std::size_t)> OnHttpRequest; + +class HTTPServerAcceptor { + public: + HTTPServerAcceptor(std::string &&server_locator, OnHttpRequest callback); + HTTPServerAcceptor(std::string &server_locator, OnHttpRequest callback); + + void listen(bool async); + + HttpRequest &&request(); + +// void asyncSendResponse(); + +// HTTPClientConnection& get(std::string &url, HTTPHeaders headers = {}, HTTPPayload payload = {}); +// +// HTTPResponse&& response(); + + private: + + void processIncomingInterest(transport::ProducerSocket &p, const transport::Interest &interest); + + OnHttpRequest callback_; + HttpRequest request_; + std::shared_ptr<transport::ProducerSocket> acceptor_producer_; +}; + +} // end namespace http + +} // end namespace icnet
\ No newline at end of file diff --git a/icnet/http/icnet_http_server_publisher.cc b/icnet/http/icnet_http_server_publisher.cc new file mode 100644 index 00000000..8ff86459 --- /dev/null +++ b/icnet/http/icnet_http_server_publisher.cc @@ -0,0 +1,79 @@ +/* + * 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_http_server_publisher.h" + +namespace icnet { + +namespace http { + +HTTPServerPublisher::HTTPServerPublisher(const transport::Name &content_name) + : content_name_(content_name) { + // Create a new publisher + producer_ = std::unique_ptr<transport::ProducerSocket>(new transport::ProducerSocket(content_name)); + producer_->attach(); +} + +HTTPServerPublisher::~HTTPServerPublisher() { + this->timer_->cancel(); +} + +HTTPServerPublisher &HTTPServerPublisher::setTimeout(uint32_t timeout) { + std::shared_ptr<transport::Portal> portal; + producer_->getSocketOption(transport::GeneralTransportOptions::PORTAL, portal); + timer_ = std::unique_ptr<boost::asio::deadline_timer>(new boost::asio::deadline_timer(portal->getIoService(), + boost::posix_time::seconds( + timeout))); + + wait_callback_ = [this](const boost::system::error_code e) { + if (!e) { + producer_->stop(); + } + }; + + interest_enter_callback_ = [this, timeout](transport::ProducerSocket &p, const transport::Interest &interest) { + this->timer_->cancel(); + this->timer_->expires_from_now(boost::posix_time::seconds(timeout)); + this->timer_->async_wait(wait_callback_); + }; + + producer_->setSocketOption(transport::ProducerCallbacksOptions::INTEREST_INPUT, + (transport::ProducerInterestCallback) interest_enter_callback_); + + timer_->async_wait(wait_callback_); + + return *this; +} + +void HTTPServerPublisher::publishContent(const uint8_t *buf, size_t buffer_size, const int response_id, bool is_last) { + if (producer_) { + std::cout << "Replying to " << content_name_ << std::endl; + producer_->produce(content_name_, buf, buffer_size, response_id, is_last); + } +} + +void HTTPServerPublisher::serveClients() { + producer_->serveForever(); +} + +void HTTPServerPublisher::stop() { + std::shared_ptr<transport::Portal> portal_ptr; + producer_->getSocketOption(transport::GeneralTransportOptions::PORTAL, portal_ptr); + portal_ptr->getIoService().stop(); +} + +} + +}
\ No newline at end of file diff --git a/icnet/http/icnet_http_server_publisher.h b/icnet/http/icnet_http_server_publisher.h new file mode 100644 index 00000000..933d20c8 --- /dev/null +++ b/icnet/http/icnet_http_server_publisher.h @@ -0,0 +1,72 @@ +/* + * 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 "icnet_transport_socket_consumer.h" +#include "icnet_transport_socket_producer.h" +#include "icnet_http_default_values.h" + +#include <vector> +#include <functional> + +#define HTTP_VERSION "1.0" + +namespace icnet { + +namespace http { + +typedef std::vector<uint8_t> HttpRequest; +typedef std::function<void(const boost::system::error_code e)> DeadlineTimerCallback; + +class HTTPServerPublisher { + public: + HTTPServerPublisher(const transport::Name &content_name); + + ~HTTPServerPublisher(); + + void publishContent(const uint8_t *buf, + size_t buffer_size, + const int response_id, + bool is_last); + + void serveClients(); + + void stop(); + + HTTPServerPublisher &setTimeout(uint32_t timeout); + +// HttpRequest&& request(); + +// void sendResponse(); + +// HTTPClientConnection& get(std::string &url, HTTPHeaders headers = {}, HTTPPayload payload = {}); +// +// HTTPResponse&& response(); + + private: + + void processIncomingInterest(transport::ProducerSocket &p, const transport::Interest &interest); + + transport::Name content_name_; + std::unique_ptr<boost::asio::deadline_timer> timer_; + std::unique_ptr<transport::ProducerSocket> producer_; + transport::ProducerInterestCallback interest_enter_callback_; + DeadlineTimerCallback wait_callback_; +}; + +} // end namespace http + +} // end namespace icnet
\ No newline at end of file diff --git a/icnet/transport/icnet_common.h b/icnet/transport/icnet_transport_common.h index d507b324..d507b324 100644 --- a/icnet/transport/icnet_common.h +++ b/icnet/transport/icnet_transport_common.h diff --git a/icnet/transport/icnet_content_store.cc b/icnet/transport/icnet_transport_content_store.cc index 64694b20..a9f05658 100644 --- a/icnet/transport/icnet_content_store.cc +++ b/icnet/transport/icnet_transport_content_store.cc @@ -13,10 +13,12 @@ * limitations under the License. */ -#include "icnet_content_store.h" +#include "icnet_transport_content_store.h" namespace icnet { +namespace transport { + ContentStore::ContentStore(std::size_t max_packets) : max_content_store_size_(max_packets) { } @@ -73,4 +75,6 @@ std::size_t ContentStore::size() const { return content_store_hash_table_.size(); } +} + } // end namespace icnet
\ No newline at end of file diff --git a/icnet/transport/icnet_content_store.h b/icnet/transport/icnet_transport_content_store.h index a626055d..8a2ed4c6 100644 --- a/icnet/transport/icnet_content_store.h +++ b/icnet/transport/icnet_transport_content_store.h @@ -16,12 +16,14 @@ #ifndef ICNET_CONTENT_STORE_H_ #define ICNET_CONTENT_STORE_H_ -#include "icnet_socket.h" +#include "icnet_transport_socket.h" #include <mutex> namespace icnet { +namespace transport { + typedef std::pair<std::shared_ptr<ContentObject>, std::list<std::reference_wrapper<const Name>>::iterator> CcnxContentStoreEntry; typedef std::list<std::reference_wrapper<const Name>> LRUList; @@ -54,6 +56,8 @@ class ContentStore { std::mutex cs_mutex_; }; +} // end namespace transport + } // end namespace icnet diff --git a/icnet/transport/icnet_download_observer.h b/icnet/transport/icnet_transport_download_observer.h index 7b640a1c..15ed5436 100644 --- a/icnet/transport/icnet_download_observer.h +++ b/icnet/transport/icnet_transport_download_observer.h @@ -18,6 +18,8 @@ namespace icnet { +namespace transport { + class IcnObserver { public: virtual ~IcnObserver() { @@ -26,6 +28,8 @@ class IcnObserver { virtual void notifyStats(double throughput) = 0; }; +} // end namespace transport + } // end namespace icnet #endif // ICNET_DOWNLOAD_OBSERVER_H_ diff --git a/icnet/transport/icnet_transport.cc b/icnet/transport/icnet_transport_protocol.cc index 632d03c4..71b855eb 100644 --- a/icnet/transport/icnet_transport.cc +++ b/icnet/transport/icnet_transport_protocol.cc @@ -13,10 +13,12 @@ * limitations under the License. */ -#include "icnet_transport.h" +#include "icnet_transport_protocol.h" namespace icnet { +namespace transport { + TransportProtocol::TransportProtocol(Socket *icn_socket) : socket_(icn_socket), is_running_(false) { } @@ -29,4 +31,6 @@ bool TransportProtocol::isRunning() { return is_running_; } +} // end namespace transport + } // end namespace icnet diff --git a/icnet/transport/icnet_transport.h b/icnet/transport/icnet_transport_protocol.h index 738634fb..5ad43fb5 100644 --- a/icnet/transport/icnet_transport.h +++ b/icnet/transport/icnet_transport_protocol.h @@ -16,10 +16,12 @@ #ifndef ICNET_TRANSPORT_PROTOCOL_H_ #define ICNET_TRANSPORT_PROTOCOL_H_ -#include "icnet_socket.h" +#include "icnet_transport_socket.h" namespace icnet { +namespace transport { + class TransportProtocol { public: TransportProtocol(Socket *icn_socket); @@ -38,6 +40,8 @@ class TransportProtocol { bool is_running_; }; -} +} // end namespace transport + +} // end namespace icnet #endif // ICNET_TRANSPORT_PROTOCOL_H_ diff --git a/icnet/transport/icnet_transport_raaqm.cc b/icnet/transport/icnet_transport_raaqm.cc index 7216d29e..787631f4 100644 --- a/icnet/transport/icnet_transport_raaqm.cc +++ b/icnet/transport/icnet_transport_raaqm.cc @@ -21,6 +21,8 @@ namespace icnet { +namespace transport { + RaaqmTransportProtocol::RaaqmTransportProtocol(Socket *icnet_socket) : VegasTransportProtocol(icnet_socket), rate_estimator_(NULL) { init(); @@ -560,4 +562,6 @@ RaaqmTransportProtocol::onInterest(const Interest &interest) } #endif +} // end namespace transport + } // end namespace icnet diff --git a/icnet/transport/icnet_transport_raaqm.h b/icnet/transport/icnet_transport_raaqm.h index dc5e72bd..adf3d439 100644 --- a/icnet/transport/icnet_transport_raaqm.h +++ b/icnet/transport/icnet_transport_raaqm.h @@ -19,10 +19,12 @@ #include "icnet_transport_vegas.h" #include "icnet_transport_vegas_rto_estimator.h" #include "icnet_transport_raaqm_data_path.h" -#include "icnet_rate_estimation.h" +#include "icnet_transport_rate_estimation.h" namespace icnet { +namespace transport { + class RaaqmTransportProtocol : public VegasTransportProtocol { public: RaaqmTransportProtocol(Socket *icnet_socket); @@ -95,6 +97,8 @@ class RaaqmTransportProtocol : public VegasTransportProtocol { double avg_rtt_; }; +} // end namespace transport + } // end namespace icnet #endif // ICNET_RAAQM_TRANSPORT_PROTOCOL_H_ diff --git a/icnet/transport/icnet_transport_raaqm_data_path.cc b/icnet/transport/icnet_transport_raaqm_data_path.cc index 1ff3f667..abe8db6c 100644 --- a/icnet/transport/icnet_transport_raaqm_data_path.cc +++ b/icnet/transport/icnet_transport_raaqm_data_path.cc @@ -20,6 +20,8 @@ namespace icnet { +namespace transport { + RaaqmDataPath::RaaqmDataPath(double drop_factor, double minimum_drop_probability, unsigned new_timer, @@ -209,4 +211,6 @@ bool RaaqmDataPath::isStale() { return false; } +} // end namespace transport + } // end namespace icnet diff --git a/icnet/transport/icnet_transport_raaqm_data_path.h b/icnet/transport/icnet_transport_raaqm_data_path.h index 0093f84b..526c0231 100644 --- a/icnet/transport/icnet_transport_raaqm_data_path.h +++ b/icnet/transport/icnet_transport_raaqm_data_path.h @@ -31,6 +31,8 @@ namespace icnet { +namespace transport { + class RaaqmDataPath { public: @@ -237,6 +239,8 @@ class RaaqmDataPath { double alpha_; }; +} // end namespace transport + } // end namespace icnet #endif // ICNET_RAAQM_DATA_PATH_H_ diff --git a/icnet/transport/icnet_rate_estimation.cc b/icnet/transport/icnet_transport_rate_estimation.cc index b378da06..79b5b3e2 100644 --- a/icnet/transport/icnet_rate_estimation.cc +++ b/icnet/transport/icnet_transport_rate_estimation.cc @@ -14,10 +14,12 @@ */ -#include "icnet_rate_estimation.h" +#include "icnet_transport_rate_estimation.h" namespace icnet { +namespace transport { + void *Timer(void *data) { InterRttEstimator *estimator = (InterRttEstimator *) data; @@ -108,7 +110,7 @@ void InterRttEstimator::onRttUpdate(double rtt) { std::cerr << "Error allocating thread." << std::endl; my_th_ = NULL; } - if (/*int err = */pthread_create(my_th_, NULL, icnet::Timer, (void *) this)) { + if (/*int err = */pthread_create(my_th_, NULL, Timer, (void *) this)) { std::cerr << "Error creating the thread" << std::endl; my_th_ = NULL; } @@ -320,5 +322,7 @@ void BatchingPacketsEstimator::onWindowDecrease(double win_current) { gettimeofday(&(this->begin_), 0); } +} // end namespace transport + } // end namespace icnet diff --git a/icnet/transport/icnet_rate_estimation.h b/icnet/transport/icnet_transport_rate_estimation.h index 86b879c2..7916ccfa 100644 --- a/icnet/transport/icnet_rate_estimation.h +++ b/icnet/transport/icnet_transport_rate_estimation.h @@ -19,7 +19,7 @@ #include <unistd.h> #include "icnet_transport_raaqm_data_path.h" -#include "icnet_download_observer.h" +#include "icnet_transport_download_observer.h" #define BATCH 50 #define KV 20 @@ -28,6 +28,8 @@ namespace icnet { +namespace transport { + class IcnRateEstimator { public: IcnRateEstimator() { @@ -181,7 +183,9 @@ class SimpleEstimator : public IcnRateEstimator { void *Timer(void *data); -}// end namespace icnet +} // end namespace transport + +} // end namespace icnet #endif // ICNET_RATE_ESTIMATION_H_ diff --git a/icnet/transport/icnet_socket.h b/icnet/transport/icnet_transport_socket.h index f1ce8da0..edfdf1e8 100644 --- a/icnet/transport/icnet_socket.h +++ b/icnet/transport/icnet_transport_socket.h @@ -16,10 +16,10 @@ #ifndef ICNET_SOCKET_H_ #define ICNET_SOCKET_H_ -#include "icnet_common.h" -#include "icnet_socket_options_keys.h" -#include "icnet_socket_options_default_values.h" -#include "icnet_download_observer.h" +#include "icnet_transport_common.h" +#include "icnet_transport_socket_options_keys.h" +#include "icnet_transport_socket_options_default_values.h" +#include "icnet_transport_download_observer.h" #define SOCKET_OPTION_GET 0 #define SOCKET_OPTION_NOT_GET 1 @@ -31,6 +31,8 @@ namespace icnet { +namespace transport { + class ConsumerSocket; class ProducerSocket; @@ -45,7 +47,7 @@ typedef ccnx::PayloadType PayloadType; typedef ccnx::Array Array; typedef std::function<void(ConsumerSocket &, const Interest &)> ConsumerInterestCallback; -typedef std::function<void(ConsumerSocket &, const uint8_t *, size_t)> ConsumerContentCallback; +typedef std::function<void(ConsumerSocket &, std::vector<uint8_t> &&)> ConsumerContentCallback; typedef std::function<void(ConsumerSocket &, const ContentObject &)> ConsumerContentObjectCallback; typedef std::function<bool(ConsumerSocket &, const ContentObject &)> ConsumerContentObjectVerificationCallback; typedef std::function<void(ConsumerSocket &, const Manifest &)> ConsumerManifestCallback; @@ -119,6 +121,8 @@ class Socket { }; }; +} // end namespace transport + } // namespace icnet #endif // ICNET_SOCKET_H_ diff --git a/icnet/transport/icnet_socket_consumer.cc b/icnet/transport/icnet_transport_socket_consumer.cc index 2aec571b..21fa7327 100644 --- a/icnet/transport/icnet_socket_consumer.cc +++ b/icnet/transport/icnet_transport_socket_consumer.cc @@ -13,10 +13,12 @@ * limitations under the License. */ -#include "icnet_socket_consumer.h" +#include "icnet_transport_socket_consumer.h" namespace icnet { +namespace transport { + ConsumerSocket::ConsumerSocket(Name prefix, int protocol) : is_running_(false), name_prefix_(prefix), @@ -611,4 +613,6 @@ int ConsumerSocket::getSocketOption(int socket_option_key, IcnObserver **socket_ } } +} // end namespace transport + } // end namespace icnet diff --git a/icnet/transport/icnet_socket_consumer.h b/icnet/transport/icnet_transport_socket_consumer.h index 6b9ec811..59a3a8cb 100644 --- a/icnet/transport/icnet_socket_consumer.h +++ b/icnet/transport/icnet_transport_socket_consumer.h @@ -16,9 +16,9 @@ #ifndef ICNET_CONSUMER_SOCKET_H_ #define ICNET_CONSUMER_SOCKET_H_ -#include "icnet_common.h" -#include "icnet_socket.h" -#include "icnet_transport.h" +#include "icnet_transport_common.h" +#include "icnet_transport_socket.h" +#include "icnet_transport_protocol.h" #include "icnet_transport_raaqm.h" #include "icnet_transport_vegas.h" @@ -27,6 +27,8 @@ namespace icnet { +namespace transport { + class ConsumerSocket : public Socket { public: explicit ConsumerSocket(const Name prefix, int protocol); @@ -156,6 +158,8 @@ class ConsumerSocket : public Socket { bool rtt_stats_; }; +} // end namespace transport + } // end namespace icnet #endif // ICNET_CONSUMER_SOCKET_H_ diff --git a/icnet/transport/icnet_socket_options_default_values.h b/icnet/transport/icnet_transport_socket_options_default_values.h index 4f6d68e9..6214fc96 100644 --- a/icnet/transport/icnet_socket_options_default_values.h +++ b/icnet/transport/icnet_transport_socket_options_default_values.h @@ -18,6 +18,8 @@ namespace icnet { +namespace transport { + namespace default_values { const int interest_lifetime = 1000; // milliseconds @@ -56,6 +58,8 @@ const int max_content_object_size = 8096; } // end namespace default_values +} // end namespace transport + } // end namespace icnet #endif // ICNET_SOCKET_OPTIONS_DEFAULT_VALUES_H_ diff --git a/icnet/transport/icnet_socket_options_keys.h b/icnet/transport/icnet_transport_socket_options_keys.h index 4b82f67a..460e8383 100644 --- a/icnet/transport/icnet_socket_options_keys.h +++ b/icnet/transport/icnet_transport_socket_options_keys.h @@ -18,6 +18,8 @@ namespace icnet { +namespace transport { + typedef enum { RAAQM = 0, VEGAS = 1 } TransportProtocolAlgorithms; @@ -90,6 +92,8 @@ typedef enum { SHA_256 = 701, RSA_256 = 702, } SignatureType; +} // end namespace transport + } // end namespace icnet #endif // ICNET_TRANSPORT_OPTIONS_KEYS_H_ diff --git a/icnet/transport/icnet_socket_producer.cc b/icnet/transport/icnet_transport_socket_producer.cc index 994488a0..190580f8 100644 --- a/icnet/transport/icnet_socket_producer.cc +++ b/icnet/transport/icnet_transport_socket_producer.cc @@ -13,10 +13,12 @@ * limitations under the License. */ -#include "icnet_socket_producer.h" +#include "icnet_transport_socket_producer.h" namespace icnet { +namespace transport { + ProducerSocket::ProducerSocket(Name prefix) : portal_(new Portal()), name_prefix_(prefix), @@ -69,6 +71,10 @@ void ProducerSocket::serveForever() { } } +void ProducerSocket::stop() { + portal_->stopEventsLoop(); +} + void ProducerSocket::dispatch() { // Check that the INTEREST_INPUT callback is set. if (on_interest_input_ == VOID_HANDLER) { @@ -98,7 +104,7 @@ void ProducerSocket::passContentObjectToCallbacks(const std::shared_ptr<ContentO if (!making_manifest_) { on_content_object_to_sign_(*this, *content_object); } else { - if (content_object->getContentType() == PayloadType::MANIFEST) { + if (content_object->getPayloadType() == PayloadType::MANIFEST) { on_content_object_to_sign_(*this, *content_object); } else { content_object->signWithSha256(key_locator_); @@ -140,19 +146,19 @@ void ProducerSocket::produce(ContentObject &content_object) { portal_->sendContentObject(content_object); } -void ProducerSocket::produce(Name suffix, const uint8_t *buf, size_t buffer_size, const int response_id, bool is_last) { +void ProducerSocket::produce(Name name, const uint8_t *buf, size_t buffer_size, const int response_id, bool is_last) { if (buffer_size == 0) { return; } - int bytes_segmented = 0; + if (name.empty() || !name_prefix_.isPrefixOf(name)) { + return; + } - Name name(name_prefix_); + int bytes_segmented = 0; - if (!suffix.empty()) { - name.append(suffix); - } + std::cout << name.toString() << std::endl; size_t bytes_occupied_by_name = name.size(); @@ -192,8 +198,8 @@ void ProducerSocket::produce(Name suffix, const uint8_t *buf, size_t buffer_size Name manifest_name(name_prefix_); - if (!suffix.empty()) { - manifest_name.append(suffix); + if (!name.empty()) { + manifest_name.append(name); } manifest_name.appendSegment(current_segment); @@ -717,4 +723,6 @@ int ProducerSocket::setSocketOption(int socket_option_key, IcnObserver *socket_o return SOCKET_OPTION_NOT_SET; } +} // end namespace transport + } // end namespace icnet diff --git a/icnet/transport/icnet_socket_producer.h b/icnet/transport/icnet_transport_socket_producer.h index d709e305..4f98f9e2 100644 --- a/icnet/transport/icnet_socket_producer.h +++ b/icnet/transport/icnet_transport_socket_producer.h @@ -16,8 +16,8 @@ #ifndef ICNET_PRODUCER_SOCKET_H_ #define ICNET_PRODUCER_SOCKET_H_ -#include "icnet_socket.h" -#include "icnet_content_store.h" +#include "icnet_transport_socket.h" +#include "icnet_transport_content_store.h" #include <queue> #include <mutex> @@ -31,6 +31,8 @@ namespace icnet { +namespace transport { + class ProducerSocket : public Socket { public: @@ -42,7 +44,7 @@ class ProducerSocket : public Socket { void dispatch(); - void produce(Name suffix, const uint8_t *buffer, size_t buffer_size, const int request_id = 0, bool is_last = false); + void produce(Name name, const uint8_t *buffer, size_t buffer_size, const int request_id = 0, bool is_last = false); void produce(ContentObject &content_object); @@ -52,6 +54,8 @@ class ProducerSocket : public Socket { void serveForever(); + void stop(); + void onInterest(const Name &name, const Interest &interest); int setSocketOption(int socket_option_key, int socket_option_value); @@ -167,6 +171,8 @@ class ProducerSocket : public Socket { }; -} +} // end namespace transport + +} // end namespace icnet #endif // ICNET_PRODUCER_SOCKET_H_ diff --git a/icnet/transport/icnet_transport_vegas.cc b/icnet/transport/icnet_transport_vegas.cc index d5a3c500..dd5c7c90 100644 --- a/icnet/transport/icnet_transport_vegas.cc +++ b/icnet/transport/icnet_transport_vegas.cc @@ -14,10 +14,12 @@ */ #include "icnet_transport_vegas.h" -#include "icnet_socket_consumer.h" +#include "icnet_transport_socket_consumer.h" namespace icnet { +namespace transport { + VegasTransportProtocol::VegasTransportProtocol(Socket *icnet_socket) : TransportProtocol(icnet_socket), is_final_block_number_discovered_(false), @@ -139,9 +141,9 @@ void VegasTransportProtocol::onContentSegment(const Interest &interest, ContentO on_interest_satisfied(*dynamic_cast<ConsumerSocket *>(socket_), const_cast<Interest &>(interest)); } - if (content_object.getContentType() == PayloadType::MANIFEST) { + if (content_object.getPayloadType() == PayloadType::MANIFEST) { onManifest(interest, content_object); - } else if (content_object.getContentType() == PayloadType::DATA) { + } else if (content_object.getPayloadType() == PayloadType::DATA) { onContentObject(interest, content_object); } // TODO InterestReturn @@ -381,8 +383,7 @@ void VegasTransportProtocol::copyContent(ContentObject &content_object) { socket_->getSocketOption(CONTENT_RETRIEVED, on_payload); if (on_payload != VOID_HANDLER) { on_payload(*dynamic_cast<ConsumerSocket *>(socket_), - (uint8_t *) (content_buffer_.data()), - content_buffer_.size()); + std::move(content_buffer_)); } //reduce window size to prevent its speculative growth in case when consume() is called in loop @@ -401,7 +402,7 @@ void VegasTransportProtocol::reassemble() { uint64_t index = last_reassembled_segment_ % default_values::default_buffer_size; while (receive_buffer_[index % default_values::default_buffer_size]) { - if (receive_buffer_[index % default_values::default_buffer_size]->getContentType() == PayloadType::DATA) { + if (receive_buffer_[index % default_values::default_buffer_size]->getPayloadType() == PayloadType::DATA) { copyContent(*receive_buffer_[index % default_values::default_buffer_size]); } @@ -484,4 +485,6 @@ void VegasTransportProtocol::removeAllPendingInterests() { portal_->clear(); } -} // namespace icn-interface +} // end namespace transport + +} // end namespace icnet diff --git a/icnet/transport/icnet_transport_vegas.h b/icnet/transport/icnet_transport_vegas.h index a47050d8..cec2aa5a 100644 --- a/icnet/transport/icnet_transport_vegas.h +++ b/icnet/transport/icnet_transport_vegas.h @@ -16,11 +16,13 @@ #ifndef ICNET_VEGAS_TRANSPORT_PROTOCOL_H_ #define ICNET_VEGAS_TRANSPORT_PROTOCOL_H_ -#include "icnet_transport.h" +#include "icnet_transport_protocol.h" #include "icnet_transport_vegas_rto_estimator.h" namespace icnet { +namespace transport { + class VegasTransportProtocol : public TransportProtocol { public: @@ -103,6 +105,8 @@ class VegasTransportProtocol : public TransportProtocol { std::unordered_map<uint64_t, bool> fast_retransmitted_segments; }; +} // end namespace transport + } // end namespace icnet diff --git a/icnet/transport/icnet_transport_vegas_rto_estimator.cc b/icnet/transport/icnet_transport_vegas_rto_estimator.cc index 6653518e..f1c10637 100644 --- a/icnet/transport/icnet_transport_vegas_rto_estimator.cc +++ b/icnet/transport/icnet_transport_vegas_rto_estimator.cc @@ -14,10 +14,12 @@ */ #include "icnet_transport_vegas_rto_estimator.h" -#include "icnet_socket_options_default_values.h" +#include "icnet_transport_socket_options_default_values.h" namespace icnet { +namespace transport { + RtoEstimator::RtoEstimator(Duration min_rto) : smoothed_rtt_(RtoEstimator::getInitialRtt().count()), rtt_variation_(0), @@ -44,4 +46,6 @@ RtoEstimator::Duration RtoEstimator::computeRto() const { return Duration(static_cast<Duration::rep>(rto)); } +} // end namespace transport + } // end namespace icnet
\ No newline at end of file diff --git a/icnet/transport/icnet_transport_vegas_rto_estimator.h b/icnet/transport/icnet_transport_vegas_rto_estimator.h index 7b18533c..799d6fbc 100644 --- a/icnet/transport/icnet_transport_vegas_rto_estimator.h +++ b/icnet/transport/icnet_transport_vegas_rto_estimator.h @@ -16,12 +16,14 @@ #ifndef ICNET_VEGAS_TRANSPORT_PROTOCOL_RTT_ESTIMATOR_H_ #define ICNET_VEGAS_TRANSPORT_PROTOCOL_RTT_ESTIMATOR_H_ -#include "icnet_common.h" +#include "icnet_transport_common.h" // Implementation inspired from RFC6298 (https://tools.ietf.org/search/rfc6298#ref-JK88) namespace icnet { +namespace transport { + class RtoEstimator { public: typedef std::chrono::microseconds Duration; @@ -43,6 +45,8 @@ class RtoEstimator { double last_rto_; }; +} // end namespace transport + } // end namespace icnet 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 |