-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:ANLAB-KAIST/KENSv3
- Loading branch information
Showing
6 changed files
with
833 additions
and
0 deletions.
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
project(echo) | ||
|
||
set(echo_SOURCES EchoAssignment.cpp EchoAssignment.hpp) | ||
|
||
add_executable(echo ${echo_SOURCES} testecho.cpp) | ||
add_executable(echo-non-kens ${echo_SOURCES} EchoNonKens.cpp) | ||
|
||
get_solution(kens) | ||
|
||
target_link_libraries(echo kens kens_solution gtest_main) | ||
target_compile_definitions(echo-non-kens PRIVATE NON_KENS) |
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,84 @@ | ||
#include "EchoAssignment.hpp" | ||
|
||
#include <cstdio> | ||
#include <cstdlib> | ||
#include <cstring> | ||
|
||
#include <arpa/inet.h> | ||
|
||
// !IMPORTANT: allowed system calls. | ||
// !DO NOT USE OTHER NETWORK SYSCALLS (send, recv, select, poll, epoll, fork | ||
// etc.) | ||
// * socket | ||
// * bind | ||
// * listen | ||
// * accept | ||
// * read | ||
// * write | ||
// * close | ||
// * getsockname | ||
// * getpeername | ||
// See below for their usage. | ||
// https://github.com/ANLAB-KAIST/KENSv3/wiki/Misc:-External-Resources#linux-manuals | ||
|
||
int EchoAssignment::serverMain(const char *bind_ip, int port, | ||
const char *server_hello) { | ||
// Your server code | ||
// !IMPORTANT: do not use global variables and do not define/use functions | ||
// !IMPORTANT: for all system calls, when an error happens, your program must | ||
// return. e.g., if an read() call return -1, return -1 for serverMain. | ||
|
||
return 0; | ||
} | ||
|
||
int EchoAssignment::clientMain(const char *server_ip, int port, | ||
const char *command) { | ||
// Your client code | ||
// !IMPORTANT: do not use global variables and do not define/use functions | ||
// !IMPORTANT: for all system calls, when an error happens, your program must | ||
// return. e.g., if an read() call return -1, return -1 for clientMain. | ||
|
||
return 0; | ||
} | ||
|
||
static void print_usage(const char *program) { | ||
printf("Usage: %s <mode> <ip-address> <port-number> <command/server-hello>\n" | ||
"Modes:\n c: client\n s: server\n" | ||
"Client commands:\n" | ||
" hello : server returns <server-hello>\n" | ||
" whoami: server returns <client-ip>\n" | ||
" whoru : server returns <server-ip>\n" | ||
" others: server echos\n" | ||
"Note: each command is terminated by newline character (\\n)\n" | ||
"Examples:\n" | ||
" server: %s s 0.0.0.0 9000 hello-client\n" | ||
" client: %s c 127.0.0.1 9000 whoami\n", | ||
program, program, program); | ||
} | ||
|
||
int EchoAssignment::Main(int argc, char *argv[]) { | ||
|
||
if (argc == 0) | ||
return 1; | ||
|
||
if (argc != 5) { | ||
print_usage(argv[0]); | ||
return 1; | ||
} | ||
|
||
int port = atoi(argv[3]); | ||
if (port == 0) { | ||
printf("Wrong port number\n"); | ||
print_usage(argv[0]); | ||
} | ||
|
||
switch (*argv[1]) { | ||
case 'c': | ||
return clientMain(argv[2], port, argv[4]); | ||
case 's': | ||
return serverMain(argv[2], port, argv[4]); | ||
default: | ||
print_usage(argv[0]); | ||
return 1; | ||
} | ||
} |
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,59 @@ | ||
#ifndef E_ECHOASSIGNMENT_HPP_ | ||
#define E_ECHOASSIGNMENT_HPP_ | ||
|
||
#ifndef NON_KENS | ||
|
||
#include <E/E_Common.hpp> | ||
#include <E/E_Module.hpp> | ||
#include <E/Networking/E_Host.hpp> | ||
|
||
#include <E/Networking/TCP/E_TCPApplication.hpp> | ||
|
||
#else | ||
|
||
#include <sys/socket.h> | ||
#include <unistd.h> | ||
|
||
namespace E { | ||
class Host {}; | ||
class TCPApplication { | ||
public: | ||
TCPApplication(Host &host) {} | ||
}; | ||
} // namespace E | ||
|
||
#endif | ||
|
||
#include <map> | ||
#include <string> | ||
#include <vector> | ||
|
||
using namespace E; | ||
|
||
class EchoTesting; | ||
class EchoAssignment : public TCPApplication { | ||
public: | ||
EchoAssignment(Host &host, std::map<std::string, std::string> &answers, | ||
int argc, char *argv[]) | ||
: TCPApplication(host), ___answers(answers), argc(argc), argv(argv) {} | ||
|
||
int serverMain(const char *bind_ip, int port, const char *server_hello); | ||
int clientMain(const char *server_ip, int port, const char *msg); | ||
int Main(int argc, char *argv[]); | ||
|
||
int E_Main() { return Main(argc, argv); } | ||
|
||
private: | ||
int argc; | ||
char **argv; | ||
std::map<std::string, std::string> &___answers; | ||
|
||
// ip: peer(client or server)'s IP address | ||
// answer: peer's request/response | ||
void submitAnswer(const char *ip, const char *answer) { | ||
___answers[ip] = answer; | ||
} | ||
friend class EchoTesting; | ||
}; | ||
|
||
#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,15 @@ | ||
|
||
#include "EchoAssignment.hpp" | ||
|
||
int main(int argc, char *argv[]) { | ||
Host host; | ||
std::map<std::string, std::string> answers; | ||
EchoAssignment assignment(host, answers, argc, argv); | ||
|
||
int ret = assignment.E_Main(); | ||
printf("Submitted answers:\n"); | ||
for (auto &answer : answers) { | ||
printf(" %s -> %s\n", answer.first.c_str(), answer.second.c_str()); | ||
} | ||
return ret; | ||
} |
Oops, something went wrong.