Skip to content

Commit

Permalink
Merge branch 'master' of github.com:ANLAB-KAIST/KENSv3
Browse files Browse the repository at this point in the history
  • Loading branch information
Pusnow committed Mar 14, 2022
2 parents c273914 + 00a03e7 commit 3098c76
Show file tree
Hide file tree
Showing 6 changed files with 833 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# KENSv3 (KAIST Educational Network System)

[![Test Linux Environment](https://github.com/ANLAB-KAIST/KENSv3/actions/workflows/test-linux.yml/badge.svg)](https://github.com/ANLAB-KAIST/KENSv3/actions/workflows/test-linux.yml) [![Test Linux Extra Environments](https://github.com/ANLAB-KAIST/KENSv3/actions/workflows/test-linux-extra.yml/badge.svg)](https://github.com/ANLAB-KAIST/KENSv3/actions/workflows/test-linux-extra.yml) [![Test WSL Environment](https://github.com/ANLAB-KAIST/KENSv3/actions/workflows/test-wsl.yml/badge.svg)](https://github.com/ANLAB-KAIST/KENSv3/actions/workflows/test-wsl.yml) [![Test macOS Environment](https://github.com/ANLAB-KAIST/KENSv3/actions/workflows/test-macos.yml/badge.svg)](https://github.com/ANLAB-KAIST/KENSv3/actions/workflows/test-macos.yml)

KENS series have been used for programming assignment in CS341: Introduction to Computer Network in KAIST.
First version of KENS (v1) had been developed by Network Computing Lab, 2005.
This version had been used until 2013.
Expand Down
11 changes: 11 additions & 0 deletions app/echo/CMakeLists.txt
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)
84 changes: 84 additions & 0 deletions app/echo/EchoAssignment.cpp
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;
}
}
59 changes: 59 additions & 0 deletions app/echo/EchoAssignment.hpp
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
15 changes: 15 additions & 0 deletions app/echo/EchoNonKens.cpp
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;
}

0 comments on commit 3098c76

Please sign in to comment.