-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
chkang0912
committed
Jan 20, 2020
1 parent
e5b2634
commit 70abf3a
Showing
22 changed files
with
493 additions
and
109 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
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,38 @@ | ||
CC=gcc | ||
CPP=g++ | ||
OPT=-O0 | ||
DEBUG=-g -ggdb #-DWITHOUT_LOG #-DONLY_FLASH #-DWITHOUT_LOG #-DONLY_FLASH | ||
DEPENDENCY_TARGETS= | ||
CFLAGS+= $(DEBUG) $(OPT) -std=c11 | ||
CXXFLAGS+= $(DEBUG) $(OPT) -std=c++11 | ||
LDFLAGS=-Llib -Iinclude | ||
LIBS=-pthread | ||
OBJ_FOLDER = obj | ||
uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not') | ||
# ifeq ($(uname_S),Darwin) | ||
# CFLAGS+=-fPIC | ||
# OBJ_FOLDER=obj_mac | ||
# BIN_FOLDER=bin_mac | ||
# else | ||
# OBJ_FOLDER=obj_linux | ||
# BIN_FOLDER=bin_linux | ||
# endif | ||
TARGET=client | ||
CLIENT_OBJ=net.o client.o util.o | ||
|
||
all: $(TARGET) | ||
|
||
dep: | ||
-(cd ../deps && $(MAKE) $(DEPENDENCY_TARGETS)) | ||
|
||
client: $(CLIENT_OBJ) | ||
$(CPP) $(CXXFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS) | ||
# mv *.o $(OBJ_FOLDER)/ | ||
|
||
%.o: %.c | ||
$(CC) $(CFLAGS) $(LDFLAGS) -c $< | ||
%.o: %.cc | ||
$(CPP) $(CXXFLAGS) $(LDFLAGS) -c $< | ||
|
||
clean: | ||
rm -rf $(TARGET) $(BIN_FOLDER)/*.o $(OBJ_FOLDER)/*.o *.o |
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,29 @@ | ||
#include <cmath> | ||
#include <iomanip> | ||
#include <iostream> | ||
#include <map> | ||
#include <random> | ||
#include <string> | ||
|
||
using namespace std; | ||
|
||
int main() { | ||
default_random_engine generator; // generator 생성 | ||
normal_distribution<double> distribution(5.0, 4.0); | ||
|
||
map<int, int> hist; // hostogram 을 그리기 위한 버퍼 | ||
for (int n = 0; n < 5000000; ++n) { | ||
double number = distribution(generator); | ||
if ((number >= 0.0) && (number < 10.0)) ++hist[int(number)]; | ||
// ++hist[std::round(distribution(generator))]; // histogram buffering | ||
} | ||
int total = 0; | ||
for (auto p : hist) { // * 로 나타내기 | ||
std::cout << std::fixed << std::setprecision(1) << std::setw(2) << p.first | ||
<< ' ' << std::string(p.second / 25000, '*') << '\n'; | ||
total += p.second; | ||
} | ||
std::cout << total << std::endl; | ||
getchar(); // 결과 창이 없어지지 않게 하기 위함 | ||
return 0; | ||
} |
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,63 @@ | ||
#include "client.h" | ||
struct kvClient client; | ||
static void sigShutdownHandler(int sig) { | ||
std::string msg; | ||
switch (sig) { | ||
case SIGINT: | ||
msg = "Received SIGINT scheduling shutdown..."; | ||
break; | ||
case SIGTERM: | ||
msg = "Received SIGTERM scheduling shutdown..."; | ||
break; | ||
default: | ||
msg = "Received shutdown signal, scheduling shutdown..."; | ||
}; | ||
std::cout << msg << std::endl; | ||
if (sig == SIGINT) { | ||
std::cout << "You insist... exiting now." << std::endl; | ||
exit(1); | ||
} | ||
} | ||
int setupSignalHandlers(void) { | ||
sighandler_t sig_ret; | ||
sig_ret = signal(SIGTERM, sigShutdownHandler); | ||
if (sig_ret == SIG_ERR) { | ||
std::cout << "can't set SIGTERM handler" << std::endl; | ||
return EXIT_FAILURE; | ||
} | ||
signal(SIGINT, sigShutdownHandler); | ||
if (sig_ret == SIG_ERR) { | ||
std::cout << "can't set SIGINT handler" << std::endl; | ||
return EXIT_FAILURE; | ||
} | ||
return 0; | ||
} | ||
|
||
static void cliRefreshPrompt(void) { | ||
int len; | ||
client.prompt.clear(); | ||
client.prompt += | ||
client.ip.append(":") + std::to_string(client.port).append("> "); | ||
} | ||
|
||
void initClient(void) { | ||
Net_util net(client.port, client.ip); | ||
net.tcpConnect(); | ||
client.cli = static_cast<void*>(&net); | ||
cliRefreshPrompt(); | ||
} | ||
int main(int argc, char** argv) { | ||
if (argc >= 2) { | ||
char* configfile = NULL; | ||
configfile = argv[1]; | ||
loadServerConfig(configfile); | ||
} else { | ||
std::cout << "ex: " << argv[0] << " conf/client.conf" << std::endl; | ||
exit(-1); | ||
} | ||
initClient(); | ||
std::cout << client.prompt; | ||
int data; | ||
std::cin >> data; | ||
return 0; | ||
} |
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,26 @@ | ||
#ifndef __SERVER_H | ||
#define __SERVER_H | ||
#include <signal.h> | ||
#include <stddef.h> | ||
#include <sys/syscall.h> | ||
#include "net.h" | ||
#include "util.h" | ||
#define KV_IOBUF_LEN 1024 * 40 | ||
|
||
typedef void (*sighandler_t)(int); | ||
|
||
struct msg { | ||
unsigned int len; | ||
char buf[]; | ||
}; | ||
|
||
struct kvClient { | ||
/* Networking */ | ||
int port; | ||
std::string ip; | ||
void *cli; | ||
std::string prompt; | ||
}; | ||
|
||
extern struct kvClient client; | ||
#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,4 @@ | ||
|
||
#client config | ||
port 19502 | ||
host 127.0.0.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,32 @@ | ||
#include "net.h" | ||
void Net_util::tcpConnect() { | ||
struct sockaddr_in server_addr, client_addr; | ||
if ((cfd_ = socket(AF_INET, SOCK_STREAM, 0)) == -1) { // 소켓 생성 | ||
printf("[NET] Can't open stream socket."); | ||
exit(-1); | ||
} | ||
memset(&server_addr, 0x00, sizeof(server_addr)); | ||
server_addr.sin_family = AF_INET; | ||
server_addr.sin_addr.s_addr = inet_addr(ip_.c_str()); | ||
server_addr.sin_port = htons(port_); | ||
int ret = connect(cfd_, (struct sockaddr *)&server_addr, sizeof(server_addr)); | ||
if (ret < 0) { | ||
printf("[NET] Can't connect port #%d.", port_); | ||
exit(-1); | ||
} | ||
querybuf_ = (struct msg *)malloc(sizeof(struct msg) + KV_IOBUF_LEN); | ||
std::cout << "[NET] Connect " << ip_ << ":" << port_ << std::endl; | ||
} | ||
|
||
void Net_util::tcpRecv() { | ||
int rst; | ||
rst = read(cfd_, querybuf_->buf, KV_IOBUF_LEN); | ||
if (rst < 0) printf("recv error"); | ||
printf("[NET] MSG Recived\n%s", querybuf_->buf); | ||
} | ||
void Net_util::tcpSend() { | ||
int rst; | ||
rst = write(cfd_, querybuf_->buf, querybuf_->len); | ||
if (rst < 0) printf("recv error"); | ||
printf("[NET] MSG Recived\n%s", querybuf_->buf); | ||
} |
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,30 @@ | ||
#ifndef __NET_H | ||
#define __NET_H | ||
|
||
#include <arpa/inet.h> | ||
#include <fcntl.h> | ||
#include <netinet/in.h> | ||
#include <netinet/tcp.h> | ||
#include <stdio.h> | ||
#include <sys/socket.h> | ||
#include <unistd.h> | ||
#include <iostream> | ||
#include <string> | ||
#include "client.h" | ||
|
||
#define IP_STR_LEN 46 | ||
class Net_util { | ||
public: | ||
Net_util(int port, std::string ip) : port_(port), ip_(ip) {} | ||
~Net_util() {} | ||
void tcpConnect(); | ||
void tcpRecv(); | ||
void tcpSend(); | ||
struct msg *querybuf_; | ||
|
||
private: | ||
int port_; | ||
std::string ip_; | ||
int cfd_; | ||
}; | ||
#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,29 @@ | ||
#include "util.h" | ||
|
||
void loadServerConfig(char *filename) { | ||
/* Load the file content */ | ||
char buf[1024]; | ||
char *argv[10]; | ||
int argc = 0; | ||
if (filename) { | ||
FILE *fp; | ||
if ((fp = fopen(filename, "r")) == NULL) { | ||
printf("Fatal error, can't open config file '%s'", filename); | ||
exit(1); | ||
} | ||
while (fgets(buf, 1024, fp) != NULL) { | ||
if (buf[0] == '#' || buf[0] == '\0' || buf[0] == '\n') continue; | ||
argv[argc] = strtok(buf, " "); | ||
while (argv[argc] != NULL) { | ||
argv[++argc] = strtok(NULL, " "); // 다음 문자열을 잘라서 포인터를 반환 | ||
} | ||
if (!strcasecmp(argv[0], "port") && argc == 2) { | ||
client.port = atoi(argv[1]); | ||
} else if (!strcasecmp(argv[0], "host") && argc == 2) { | ||
client.ip = std::string(argv[1]); | ||
} | ||
argc = 0; | ||
} | ||
if (fp != stdin) fclose(fp); | ||
} | ||
} |
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 @@ | ||
#ifndef __UTIL_H | ||
#define __UTIL_H | ||
|
||
#include <stdio.h> | ||
#include "client.h" | ||
|
||
// LOG_DEBUG 0 | ||
// LOG_NOTICE 1 | ||
// LOG_ERROR 2 | ||
|
||
int string2ll(const char *s, size_t slen, long long *value); | ||
void loadServerConfig(char *filename); | ||
void makingMessage(struct kvClient *c); | ||
|
||
#endif |
Oops, something went wrong.