-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
io baseline #43
Open
vrancurel
wants to merge
2
commits into
master
Choose a base branch
from
ft/io2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
io baseline #43
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
#include "kad_network.h" | ||
#include "kad_node.h" | ||
#include "kad_routable.h" | ||
#include "io_client.h" | ||
#include "shell.h" | ||
#include "utils.h" | ||
|
||
|
@@ -337,6 +338,33 @@ static int cmd_get_bytes(Shell* shell, int argc, char** argv) | |
return SHELL_CONT; | ||
} | ||
|
||
static int cmd_put(Shell * /* shell */, int argc, char **argv) | ||
{ | ||
if (argc != 4) | ||
{ | ||
fprintf(stderr, "usage: put file n_data n_parities\n"); | ||
return SHELL_CONT; | ||
} | ||
|
||
do_put(argv[1], atoi(argv[2]), atoi(argv[3])); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
return SHELL_CONT; | ||
} | ||
|
||
static int cmd_io_server_info(Shell *shell, int, char **) | ||
{ | ||
auto* node = static_cast<Node*>(shell->get_handle2()); | ||
|
||
if (nullptr == node) { | ||
std::cerr << "shall jump to a node first\n"; | ||
return SHELL_CONT; | ||
} | ||
|
||
std::cout << "port: " << node->io_server->get_port() << "\n"; | ||
|
||
return SHELL_CONT; | ||
} | ||
|
||
struct cmd_def quit_cmd = {"quit", "quit program", cmd_quit}; | ||
struct cmd_def help_cmd = {"help", "help", cmd_help}; | ||
struct cmd_def jump_cmd = {"jump", "jump to a node", cmd_jump}; | ||
|
@@ -353,6 +381,7 @@ struct cmd_def find_nearest_cmd = {"find_nearest", | |
"find nearest nodes to", | ||
cmd_find_nearest}; | ||
struct cmd_def verbose_cmd = {"verbose", "set verbosity level", cmd_verbose}; | ||
struct cmd_def put_cmd = {"put", "put a file", cmd_put}; | ||
struct cmd_def save_cmd = {"save", "save the network to file", cmd_save}; | ||
struct cmd_def xor_cmd = {"xor", "xor between 2 bignums", cmd_xor}; | ||
struct cmd_def bit_length_cmd = {"bit_length", | ||
|
@@ -371,11 +400,15 @@ struct cmd_def put_bytes_cmd = {"put_bytes", | |
struct cmd_def get_bytes_cmd = {"get_bytes", | ||
"get N bytes from storage", | ||
cmd_get_bytes}; | ||
|
||
struct cmd_def io_server_info_cmd = {"io_server_info", | ||
"get IO server info", | ||
cmd_io_server_info}; | ||
|
||
struct cmd_def* cmd_defs[] = { | ||
&bit_length_cmd, | ||
&buy_storage_cmd, | ||
&cheat_lookup_cmd, | ||
&put_cmd, | ||
&find_nearest_cmd, | ||
&get_bytes_cmd, | ||
&graphviz_cmd, | ||
|
@@ -390,6 +423,7 @@ struct cmd_def* cmd_defs[] = { | |
&show_cmd, | ||
&verbose_cmd, | ||
&xor_cmd, | ||
&io_server_info_cmd, | ||
nullptr, | ||
}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
|
||
#include <cstdio> | ||
#include <cstdlib> | ||
#include <streambuf> | ||
|
||
#include <sys/mman.h> | ||
#include <sys/stat.h> | ||
#include <fcntl.h> | ||
#include <unistd.h> | ||
#include <nttec/nttec.h> | ||
|
||
#include "io_client.h" | ||
|
||
template class nttec::fec::RsFnt<uint32_t>; | ||
|
||
int vflag = 1; | ||
|
||
struct membuf : std::streambuf { | ||
membuf(char const *base, size_t size) { | ||
char *p(const_cast<char *>(base)); | ||
this->setg(p, p, p + size); | ||
} | ||
}; | ||
|
||
struct imemstream : virtual membuf, std::istream { | ||
imemstream(char const *base, size_t size) | ||
: membuf(base, size), std::istream(static_cast<std::streambuf *>(this)) {} | ||
}; | ||
|
||
/** | ||
* Create coding files for the specified file | ||
* | ||
* This function: | ||
* 1) compute the ideal mmap size | ||
* 2) mmaps the file | ||
* 3) create proper input and output streams | ||
* | ||
* @param filename source filename | ||
* @param fec encoder to use | ||
*/ | ||
int create_coding_files(const char *filename, | ||
nttec::fec::FecCode<uint32_t> *fec) { | ||
void *addr; | ||
char *tmp_addr; | ||
int fd; | ||
struct stat sb; | ||
size_t length, data_size; | ||
off_t offset; | ||
char cfilename[1024]; | ||
std::vector<std::istream *> d_files(fec->n_data, nullptr); | ||
std::vector<std::ostream *> c_files(fec->n_outputs, nullptr); | ||
std::vector<std::ostream *> c_props_files(fec->n_outputs, nullptr); | ||
std::vector<nttec::Properties> c_props(fec->n_outputs); | ||
|
||
fd = open(filename, O_RDONLY); | ||
if (fd == -1) { | ||
std::cerr << "opening " << filename << " failed\n"; | ||
return -1; | ||
} | ||
|
||
if (fstat(fd, &sb) == -1) { | ||
std::cerr << "fstat " << filename << " failed\n"; | ||
return -1; | ||
} | ||
|
||
length = sb.st_size; | ||
|
||
data_size = length / fec->n_data; | ||
if (length % fec->n_data != 0) { | ||
// FIXME | ||
std::cerr << "file size is not multiple of n_data\n"; | ||
return -1; | ||
} | ||
std::cerr << "data_size " << data_size << "\n"; | ||
|
||
addr = mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd, 0); | ||
if (addr == MAP_FAILED) { | ||
std::cerr << "mmap failed\n"; | ||
exit(1); | ||
} | ||
std::cerr << "length " << length << " addr " << addr << "\n"; | ||
|
||
// do it | ||
offset = 0; | ||
for (u_int32_t i = 0; i < fec->n_data; i++) { | ||
if (vflag) | ||
std::cerr << "create: opening data " << i << " offset " << offset << "\n"; | ||
tmp_addr = static_cast<char *>(addr) + offset; | ||
d_files[i] = new imemstream(tmp_addr, data_size); | ||
offset += data_size; | ||
} | ||
|
||
for (u_int32_t i = 0; i < fec->n_outputs; i++) { | ||
snprintf(cfilename, sizeof(cfilename), "%s.c%d", filename, i); | ||
if (vflag) | ||
std::cerr << "create: opening coding for writing " << filename << "\n"; | ||
c_files[i] = new std::ofstream(cfilename); | ||
snprintf(cfilename, sizeof(cfilename), "%s.c%d.props", filename, i); | ||
if (vflag) | ||
std::cerr << "create: opening coding props for writing " << filename | ||
<< "\n"; | ||
c_props_files[i] = new std::ofstream(cfilename); | ||
} | ||
|
||
fec->encode_bufs(d_files, c_files, c_props); | ||
|
||
if (munmap(addr, length) != 0) { | ||
std::cerr << "munmap " << filename << " failed\n"; | ||
exit(1); | ||
} | ||
|
||
close(fd); | ||
|
||
return 0; | ||
} | ||
|
||
void do_put(const char *filename, int n_data, int n_parities) { | ||
nttec::fec::RsFnt<uint32_t> *fec; | ||
|
||
fec = new nttec::fec::RsFnt<uint32_t>(2, n_data, n_parities); | ||
if (create_coding_files(filename, fec) != 0) { | ||
return; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2017-2018 the QuadIron authors | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* | ||
* 2. Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* | ||
* 3. Neither the name of the copyright holder nor the names of its contributors | ||
* may be used to endorse or promote products derived from this software | ||
* without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
#ifndef __IO_CLIENT_H__ | ||
#define __IO_CLIENT_H__ | ||
|
||
#include <nttec/nttec.h> | ||
|
||
extern int create_coding_files(const char *filename, | ||
nttec::fec::FecCode<uint32_t> *fec); | ||
extern void do_put(const char *filename, int n_data, int n_parities); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
|
||
#include <cstdio> | ||
#include <cstdlib> | ||
|
||
#include <nttec/nttec.h> | ||
#include <sys/socket.h> | ||
#include <netinet/in.h> | ||
#include <netinet/ip.h> | ||
|
||
#include "io_server.h" | ||
|
||
namespace io { | ||
|
||
Server::Server() { | ||
this->daemon = nullptr; | ||
} | ||
|
||
/** | ||
* Get the I/O Server port | ||
* | ||
* return the port number or 0 on failure | ||
*/ | ||
uint16_t Server::get_port() | ||
{ | ||
const union MHD_DaemonInfo *dinfo; | ||
|
||
if (this->daemon == nullptr) { | ||
std::cerr << "start daemon first\n"; | ||
return 0; | ||
} | ||
|
||
dinfo = MHD_get_daemon_info(this->daemon, MHD_DAEMON_INFO_LISTEN_FD); | ||
|
||
struct sockaddr_in sin; | ||
socklen_t len = sizeof(sin); | ||
if (getsockname(dinfo->listen_fd, reinterpret_cast<struct sockaddr *>(&sin), &len) == -1) { | ||
std::cerr << "getsockname error\n"; | ||
return 0; | ||
} | ||
|
||
return ntohs(sin.sin_port); | ||
} | ||
|
||
int Server::callback(void *, MHD_Connection *, const char *, | ||
const char *, const char *, | ||
const char *, size_t *, | ||
void **) { | ||
std::cerr << "callback\n"; | ||
return 0; | ||
} | ||
|
||
void Server::start_daemon() { | ||
unsigned int mhd_flags = MHD_NO_FLAG; | ||
uint16_t port = 0; | ||
|
||
this->daemon = MHD_start_daemon(mhd_flags, port, NULL, NULL, | ||
Server::callback, this, | ||
MHD_OPTION_END); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 2017-2018 the QuadIron authors | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* | ||
* 2. Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* | ||
* 3. Neither the name of the copyright holder nor the names of its contributors | ||
* may be used to endorse or promote products derived from this software | ||
* without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
#ifndef __IO_SERVER_H__ | ||
#define __IO_SERVER_H__ | ||
|
||
#include <jsonrpccpp/server.h> | ||
#include <jsonrpccpp/server/connectors/httpserver.h> | ||
#include <nttec/nttec.h> | ||
|
||
namespace io { | ||
|
||
class Server { | ||
public: | ||
Server(); | ||
~Server(); | ||
|
||
void start_daemon(); | ||
uint16_t get_port(); | ||
|
||
private: | ||
struct MHD_Daemon *daemon; | ||
|
||
static int callback(void *cls, MHD_Connection *connection, const char *url, | ||
const char *method, const char *version, | ||
const char *upload_data, size_t *upload_data_size, | ||
void **con_cls); | ||
}; | ||
|
||
} // namespace io | ||
|
||
#endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use
std::cerr
instead offprintf