/* * 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_producer.h" #define IDENTITY_NAME "ciao" namespace icnet { class CallbackContainer { public: CallbackContainer() : buffer_(1400, 'X') { content_object_.setContent((uint8_t *) buffer_.c_str(), 1400); } void processInterest(ProducerSocket &p, const Interest &interest) { // std::cout << "Sending response to " << interest.getName() << std::endl; } void processIncomingInterest(ProducerSocket &p, const Interest &interest) { content_object_.setName(Name(interest.getName().getWrappedStructure())); p.produce(content_object_); } private: ContentObject content_object_; std::string buffer_; }; class Signer { public: Signer() : counter_(0), identity_name_(IDENTITY_NAME) { }; ~Signer() { }; void onPacket(ProducerSocket &p, ContentObject &contentObject) { counter_++; KeyLocator kl; contentObject.signWithSha256(kl); } private: int counter_; 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"; bool daemon = false; int opt; while ((opt = getopt(argc, argv, "D")) != -1) { switch (opt) { case 'D': daemon = true; break; default: exit(EXIT_FAILURE); } } if (argv[optind] == 0) { std::cerr << "Using default name ccnx:/ccnxtest" << std::endl; } else { name = argv[optind]; } if (daemon) { becomeDaemon(); } CallbackContainer stubs; // Signer signer; std::cout << "Setting name.. " << name << std::endl; ProducerSocket p(Name(name.c_str())); p.setSocketOption(GeneralTransportOptions::MAKE_MANIFEST, false); // setting callbacks p.setSocketOption(ProducerCallbacksOptions::INTEREST_INPUT, (ProducerInterestCallback) bind(&CallbackContainer::processIncomingInterest, &stubs, std::placeholders::_1, std::placeholders::_2)); p.setSocketOption(ProducerCallbacksOptions::CACHE_MISS, (ProducerInterestCallback) bind(&CallbackContainer::processInterest, &stubs, std::placeholders::_1, std::placeholders::_2)); p.attach(); p.serveForever(); return 0; } } // end namespace icnet int main(int argc, char **argv) { return icnet::main(argc, argv); }