aboutsummaryrefslogtreecommitdiffstats
path: root/apps/consumers/icnet_iget.cc
blob: db5ef173a504d8ae834f6fc10ae6521e3d6d848a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * 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);
}