Skip to content

Commit e0992b3

Browse files
committed
io baseline
1 parent 610a75b commit e0992b3

File tree

7 files changed

+268
-0
lines changed

7 files changed

+268
-0
lines changed

src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ set(LIB_SRC
77
${SOURCE_DIR}/kad_network.cpp
88
${SOURCE_DIR}/kad_node.cpp
99
${SOURCE_DIR}/kad_routable.cpp
10+
${SOURCE_DIR}/io_client.cpp
11+
${SOURCE_DIR}/io_server.cpp
1012
${SOURCE_DIR}/shell.cpp
1113

1214
CACHE

src/cmds.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "kad_network.h"
99
#include "kad_node.h"
1010
#include "kad_routable.h"
11+
#include "io_client.h"
1112
#include "shell.h"
1213
#include "utils.h"
1314

@@ -337,6 +338,19 @@ static int cmd_get_bytes(Shell* shell, int argc, char** argv)
337338
return SHELL_CONT;
338339
}
339340

341+
static int cmd_put(Shell * /* shell */, int argc, char **argv)
342+
{
343+
if (argc != 4)
344+
{
345+
fprintf(stderr, "usage: put file n_data n_parities\n");
346+
return SHELL_CONT;
347+
}
348+
349+
do_put(argv[1], atoi(argv[2]), atoi(argv[3]));
350+
351+
return SHELL_CONT;
352+
}
353+
340354
struct cmd_def quit_cmd = {"quit", "quit program", cmd_quit};
341355
struct cmd_def help_cmd = {"help", "help", cmd_help};
342356
struct cmd_def jump_cmd = {"jump", "jump to a node", cmd_jump};
@@ -353,6 +367,7 @@ struct cmd_def find_nearest_cmd = {"find_nearest",
353367
"find nearest nodes to",
354368
cmd_find_nearest};
355369
struct cmd_def verbose_cmd = {"verbose", "set verbosity level", cmd_verbose};
370+
struct cmd_def put_cmd = {"put", "put a file", cmd_put};
356371
struct cmd_def save_cmd = {"save", "save the network to file", cmd_save};
357372
struct cmd_def xor_cmd = {"xor", "xor between 2 bignums", cmd_xor};
358373
struct cmd_def bit_length_cmd = {"bit_length",
@@ -376,6 +391,7 @@ struct cmd_def* cmd_defs[] = {
376391
&bit_length_cmd,
377392
&buy_storage_cmd,
378393
&cheat_lookup_cmd,
394+
&put_cmd,
379395
&find_nearest_cmd,
380396
&get_bytes_cmd,
381397
&graphviz_cmd,

src/io_client.cpp

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
2+
#include <cstdio>
3+
#include <cstdlib>
4+
#include <streambuf>
5+
6+
#include <sys/mman.h>
7+
#include <sys/stat.h>
8+
#include <fcntl.h>
9+
#include <unistd.h>
10+
#include <nttec/nttec.h>
11+
12+
#include "io_client.h"
13+
14+
template class nttec::fec::RsFnt<uint32_t>;
15+
16+
int vflag = 1;
17+
18+
struct membuf : std::streambuf {
19+
membuf(char const *base, size_t size) {
20+
char *p(const_cast<char *>(base));
21+
this->setg(p, p, p + size);
22+
}
23+
};
24+
25+
struct imemstream : virtual membuf, std::istream {
26+
imemstream(char const *base, size_t size)
27+
: membuf(base, size), std::istream(static_cast<std::streambuf *>(this)) {}
28+
};
29+
30+
/**
31+
* Create coding files for the specified file
32+
*
33+
* This function:
34+
* 1) compute the ideal mmap size
35+
* 2) mmaps the file
36+
* 3) create proper input and output streams
37+
*
38+
* @param filename source filename
39+
* @param fec encoder to use
40+
*/
41+
int create_coding_files(const char *filename,
42+
nttec::fec::FecCode<uint32_t> *fec) {
43+
void *addr;
44+
char *tmp_addr;
45+
int fd;
46+
struct stat sb;
47+
size_t length, data_size;
48+
off_t offset;
49+
char cfilename[1024];
50+
std::vector<std::istream *> d_files(fec->n_data, nullptr);
51+
std::vector<std::ostream *> c_files(fec->n_outputs, nullptr);
52+
std::vector<std::ostream *> c_props_files(fec->n_outputs, nullptr);
53+
std::vector<nttec::Properties> c_props(fec->n_outputs);
54+
55+
fd = open(filename, O_RDONLY);
56+
if (fd == -1) {
57+
std::cerr << "opening " << filename << " failed\n";
58+
return -1;
59+
}
60+
61+
if (fstat(fd, &sb) == -1) {
62+
std::cerr << "fstat " << filename << " failed\n";
63+
return -1;
64+
}
65+
66+
length = sb.st_size;
67+
68+
data_size = length / fec->n_data;
69+
if (length % fec->n_data != 0) {
70+
// FIXME
71+
std::cerr << "file size is not multiple of n_data\n";
72+
return -1;
73+
}
74+
std::cerr << "data_size " << data_size << "\n";
75+
76+
addr = mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd, 0);
77+
if (addr == MAP_FAILED) {
78+
std::cerr << "mmap failed\n";
79+
exit(1);
80+
}
81+
std::cerr << "length " << length << " addr " << addr << "\n";
82+
83+
// do it
84+
offset = 0;
85+
for (u_int32_t i = 0; i < fec->n_data; i++) {
86+
if (vflag)
87+
std::cerr << "create: opening data " << i << " offset " << offset << "\n";
88+
tmp_addr = static_cast<char *>(addr) + offset;
89+
d_files[i] = new imemstream(tmp_addr, data_size);
90+
offset += data_size;
91+
}
92+
93+
for (u_int32_t i = 0; i < fec->n_outputs; i++) {
94+
snprintf(cfilename, sizeof(cfilename), "%s.c%d", filename, i);
95+
if (vflag)
96+
std::cerr << "create: opening coding for writing " << filename << "\n";
97+
c_files[i] = new std::ofstream(cfilename);
98+
snprintf(cfilename, sizeof(cfilename), "%s.c%d.props", filename, i);
99+
if (vflag)
100+
std::cerr << "create: opening coding props for writing " << filename
101+
<< "\n";
102+
c_props_files[i] = new std::ofstream(cfilename);
103+
}
104+
105+
fec->encode_bufs(d_files, c_files, c_props);
106+
107+
if (munmap(addr, length) != 0) {
108+
std::cerr << "munmap " << filename << " failed\n";
109+
exit(1);
110+
}
111+
112+
close(fd);
113+
114+
return 0;
115+
}
116+
117+
void do_put(const char *filename, int n_data, int n_parities) {
118+
nttec::fec::RsFnt<uint32_t> *fec;
119+
120+
fec = new nttec::fec::RsFnt<uint32_t>(2, n_data, n_parities);
121+
if (create_coding_files(filename, fec) != 0) {
122+
return;
123+
}
124+
}

src/io_client.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2017-2018 the QuadIron authors
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are met:
6+
*
7+
* 1. Redistributions of source code must retain the above copyright notice,
8+
* this list of conditions and the following disclaimer.
9+
*
10+
* 2. Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation
12+
* and/or other materials provided with the distribution.
13+
*
14+
* 3. Neither the name of the copyright holder nor the names of its contributors
15+
* may be used to endorse or promote products derived from this software
16+
* without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
#ifndef __IO_CLIENT_H__
31+
#define __IO_CLIENT_H__
32+
33+
#include <nttec/nttec.h>
34+
35+
extern int create_coding_files(const char *filename,
36+
nttec::fec::FecCode<uint32_t> *fec);
37+
extern void do_put(const char *filename, int n_data, int n_parities);
38+
39+
#endif

src/io_server.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
#include <cstdio>
3+
#include <cstdlib>
4+
5+
#include <nttec/nttec.h>
6+
7+
#include "io_server.h"
8+
9+
namespace io {
10+
11+
Server::Server() {
12+
this->daemon = nullptr;
13+
}
14+
15+
int Server::callback(void *cls, MHD_Connection *connection, const char *url,
16+
const char *method, const char *version,
17+
const char *upload_data, size_t *upload_data_size,
18+
void **con_cls) {
19+
std::cerr << "callback\n";
20+
return 0;
21+
}
22+
23+
void Server::start_daemon() {
24+
unsigned int mhd_flags = MHD_NO_FLAG;
25+
26+
this->daemon = MHD_start_daemon(mhd_flags, 0, NULL, NULL,
27+
Server::callback, this,
28+
MHD_OPTION_END);
29+
}
30+
}

src/io_server.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2017-2018 the QuadIron authors
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are met:
6+
*
7+
* 1. Redistributions of source code must retain the above copyright notice,
8+
* this list of conditions and the following disclaimer.
9+
*
10+
* 2. Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation
12+
* and/or other materials provided with the distribution.
13+
*
14+
* 3. Neither the name of the copyright holder nor the names of its contributors
15+
* may be used to endorse or promote products derived from this software
16+
* without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
#ifndef __IO_SERVER_H__
31+
#define __IO_SERVER_H__
32+
33+
#include <jsonrpccpp/server.h>
34+
#include <jsonrpccpp/server/connectors/httpserver.h>
35+
#include <nttec/nttec.h>
36+
37+
namespace io {
38+
39+
class Server {
40+
public:
41+
Server();
42+
~Server();
43+
private:
44+
struct MHD_Daemon *daemon;
45+
46+
int callback(void *cls, MHD_Connection *connection, const char *url,
47+
const char *method, const char *version,
48+
const char *upload_data, size_t *upload_data_size,
49+
void **con_cls);
50+
void start_daemon();
51+
};
52+
53+
} // namespace io
54+
55+
#endif

src/quadiron.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include "kad_network.h"
1111
#include "kad_node.h"
1212
#include "kad_routable.h"
13+
#include "io_server.h"
14+
#include "io_client.h"
1315
#include "shell.h"
1416
#include "utils.h"
1517

0 commit comments

Comments
 (0)