Skip to content

Commit

Permalink
add demo
Browse files Browse the repository at this point in the history
  • Loading branch information
Dueplay committed Mar 16, 2024
1 parent aa669e0 commit 0a89528
Show file tree
Hide file tree
Showing 12 changed files with 310 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ cmake-build-debug/
*.xml
.idea/
*/.ipynb_checkpoints/
.vscode/
.vscode/
16 changes: 16 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,22 @@ TARGET_INCLUDE_DIRECTORIES(
PUBLIC ${NEXT_SERVER_SRC_INCLUDE_DIR}
)

# Build the fileserver
ADD_EXECUTABLE(file_server ${NEXT_SERVER_DEMO_DIR}/file_server/file_server.cpp)
TARGET_LINK_LIBRARIES(file_server next_core next_http)
TARGET_INCLUDE_DIRECTORIES(
file_server
PUBLIC ${NEXT_SERVER_SRC_INCLUDE_DIR}
)

# Build the file client
ADD_EXECUTABLE(file_client ${NEXT_SERVER_DEMO_DIR}/file_server/file_client.cpp)
TARGET_LINK_LIBRARIES(file_client next_core next_http)
TARGET_INCLUDE_DIRECTORIES(
file_client
PUBLIC ${NEXT_SERVER_SRC_INCLUDE_DIR}
)

# Build the http server
ADD_EXECUTABLE(http_server ${NEXT_SERVER_SRC_DIR}/http/http_server.cpp)
TARGET_LINK_LIBRARIES(http_server next_core next_http)
Expand Down
2 changes: 2 additions & 0 deletions build/CMakeFiles/Makefile.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,7 @@ set(CMAKE_DEPEND_INFO_FILES
"CMakeFiles/echo_client.dir/DependInfo.cmake"
"CMakeFiles/dict_server.dir/DependInfo.cmake"
"CMakeFiles/dict_client.dir/DependInfo.cmake"
"CMakeFiles/file_server.dir/DependInfo.cmake"
"CMakeFiles/file_client.dir/DependInfo.cmake"
"CMakeFiles/http_server.dir/DependInfo.cmake"
)
68 changes: 64 additions & 4 deletions build/CMakeFiles/Makefile2
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ all: CMakeFiles/echo_server.dir/all
all: CMakeFiles/echo_client.dir/all
all: CMakeFiles/dict_server.dir/all
all: CMakeFiles/dict_client.dir/all
all: CMakeFiles/file_server.dir/all
all: CMakeFiles/file_client.dir/all
all: CMakeFiles/http_server.dir/all
.PHONY : all

Expand All @@ -85,6 +87,8 @@ clean: CMakeFiles/echo_server.dir/clean
clean: CMakeFiles/echo_client.dir/clean
clean: CMakeFiles/dict_server.dir/clean
clean: CMakeFiles/dict_client.dir/clean
clean: CMakeFiles/file_server.dir/clean
clean: CMakeFiles/file_client.dir/clean
clean: CMakeFiles/http_server.dir/clean
.PHONY : clean

Expand All @@ -95,7 +99,7 @@ clean: CMakeFiles/http_server.dir/clean
CMakeFiles/next_core.dir/all: CMakeFiles/next_log.dir/all
$(MAKE) $(MAKESILENT) -f CMakeFiles/next_core.dir/build.make CMakeFiles/next_core.dir/depend
$(MAKE) $(MAKESILENT) -f CMakeFiles/next_core.dir/build.make CMakeFiles/next_core.dir/build
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --progress-dir=/home/gxj/Next/build/CMakeFiles --progress-num=11,12,13,14,15,16,17,18,19,20,21 "Built target next_core"
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --progress-dir=/home/gxj/Next/build/CMakeFiles --progress-num=15,16,17,18,19,20,21,22,23,24,25 "Built target next_core"
.PHONY : CMakeFiles/next_core.dir/all

# Build rule for subdir invocation for target.
Expand All @@ -121,7 +125,7 @@ CMakeFiles/next_core.dir/clean:
CMakeFiles/next_http.dir/all: CMakeFiles/next_log.dir/all
$(MAKE) $(MAKESILENT) -f CMakeFiles/next_http.dir/build.make CMakeFiles/next_http.dir/depend
$(MAKE) $(MAKESILENT) -f CMakeFiles/next_http.dir/build.make CMakeFiles/next_http.dir/build
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --progress-dir=/home/gxj/Next/build/CMakeFiles --progress-num=22,23,24,25,26,27,28 "Built target next_http"
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --progress-dir=/home/gxj/Next/build/CMakeFiles --progress-num=26,27,28,29,30,31,32 "Built target next_http"
.PHONY : CMakeFiles/next_http.dir/all

# Build rule for subdir invocation for target.
Expand All @@ -147,7 +151,7 @@ CMakeFiles/next_http.dir/clean:
CMakeFiles/next_log.dir/all:
$(MAKE) $(MAKESILENT) -f CMakeFiles/next_log.dir/build.make CMakeFiles/next_log.dir/depend
$(MAKE) $(MAKESILENT) -f CMakeFiles/next_log.dir/build.make CMakeFiles/next_log.dir/build
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --progress-dir=/home/gxj/Next/build/CMakeFiles --progress-num=29,30 "Built target next_log"
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --progress-dir=/home/gxj/Next/build/CMakeFiles --progress-num=33,34 "Built target next_log"
.PHONY : CMakeFiles/next_log.dir/all

# Build rule for subdir invocation for target.
Expand Down Expand Up @@ -276,6 +280,62 @@ CMakeFiles/dict_client.dir/clean:
$(MAKE) $(MAKESILENT) -f CMakeFiles/dict_client.dir/build.make CMakeFiles/dict_client.dir/clean
.PHONY : CMakeFiles/dict_client.dir/clean

#=============================================================================
# Target rules for target CMakeFiles/file_server.dir

# All Build rule for target.
CMakeFiles/file_server.dir/all: CMakeFiles/next_core.dir/all
CMakeFiles/file_server.dir/all: CMakeFiles/next_http.dir/all
CMakeFiles/file_server.dir/all: CMakeFiles/next_log.dir/all
$(MAKE) $(MAKESILENT) -f CMakeFiles/file_server.dir/build.make CMakeFiles/file_server.dir/depend
$(MAKE) $(MAKESILENT) -f CMakeFiles/file_server.dir/build.make CMakeFiles/file_server.dir/build
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --progress-dir=/home/gxj/Next/build/CMakeFiles --progress-num=11,12 "Built target file_server"
.PHONY : CMakeFiles/file_server.dir/all

# Build rule for subdir invocation for target.
CMakeFiles/file_server.dir/rule: cmake_check_build_system
$(CMAKE_COMMAND) -E cmake_progress_start /home/gxj/Next/build/CMakeFiles 22
$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 CMakeFiles/file_server.dir/all
$(CMAKE_COMMAND) -E cmake_progress_start /home/gxj/Next/build/CMakeFiles 0
.PHONY : CMakeFiles/file_server.dir/rule

# Convenience name for target.
file_server: CMakeFiles/file_server.dir/rule
.PHONY : file_server

# clean rule for target.
CMakeFiles/file_server.dir/clean:
$(MAKE) $(MAKESILENT) -f CMakeFiles/file_server.dir/build.make CMakeFiles/file_server.dir/clean
.PHONY : CMakeFiles/file_server.dir/clean

#=============================================================================
# Target rules for target CMakeFiles/file_client.dir

# All Build rule for target.
CMakeFiles/file_client.dir/all: CMakeFiles/next_core.dir/all
CMakeFiles/file_client.dir/all: CMakeFiles/next_http.dir/all
CMakeFiles/file_client.dir/all: CMakeFiles/next_log.dir/all
$(MAKE) $(MAKESILENT) -f CMakeFiles/file_client.dir/build.make CMakeFiles/file_client.dir/depend
$(MAKE) $(MAKESILENT) -f CMakeFiles/file_client.dir/build.make CMakeFiles/file_client.dir/build
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --progress-dir=/home/gxj/Next/build/CMakeFiles --progress-num=9,10 "Built target file_client"
.PHONY : CMakeFiles/file_client.dir/all

# Build rule for subdir invocation for target.
CMakeFiles/file_client.dir/rule: cmake_check_build_system
$(CMAKE_COMMAND) -E cmake_progress_start /home/gxj/Next/build/CMakeFiles 22
$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 CMakeFiles/file_client.dir/all
$(CMAKE_COMMAND) -E cmake_progress_start /home/gxj/Next/build/CMakeFiles 0
.PHONY : CMakeFiles/file_client.dir/rule

# Convenience name for target.
file_client: CMakeFiles/file_client.dir/rule
.PHONY : file_client

# clean rule for target.
CMakeFiles/file_client.dir/clean:
$(MAKE) $(MAKESILENT) -f CMakeFiles/file_client.dir/build.make CMakeFiles/file_client.dir/clean
.PHONY : CMakeFiles/file_client.dir/clean

#=============================================================================
# Target rules for target CMakeFiles/http_server.dir

Expand All @@ -285,7 +345,7 @@ CMakeFiles/http_server.dir/all: CMakeFiles/next_http.dir/all
CMakeFiles/http_server.dir/all: CMakeFiles/next_log.dir/all
$(MAKE) $(MAKESILENT) -f CMakeFiles/http_server.dir/build.make CMakeFiles/http_server.dir/depend
$(MAKE) $(MAKESILENT) -f CMakeFiles/http_server.dir/build.make CMakeFiles/http_server.dir/build
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --progress-dir=/home/gxj/Next/build/CMakeFiles --progress-num=9,10 "Built target http_server"
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --progress-dir=/home/gxj/Next/build/CMakeFiles --progress-num=13,14 "Built target http_server"
.PHONY : CMakeFiles/http_server.dir/all

# Build rule for subdir invocation for target.
Expand Down
2 changes: 2 additions & 0 deletions build/CMakeFiles/TargetDirectories.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
/home/gxj/Next/build/CMakeFiles/echo_client.dir
/home/gxj/Next/build/CMakeFiles/dict_server.dir
/home/gxj/Next/build/CMakeFiles/dict_client.dir
/home/gxj/Next/build/CMakeFiles/file_server.dir
/home/gxj/Next/build/CMakeFiles/file_client.dir
/home/gxj/Next/build/CMakeFiles/http_server.dir
/home/gxj/Next/build/CMakeFiles/edit_cache.dir
/home/gxj/Next/build/CMakeFiles/rebuild_cache.dir
22 changes: 11 additions & 11 deletions build/CMakeFiles/next_core.dir/progress.make
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
CMAKE_PROGRESS_1 = 11
CMAKE_PROGRESS_2 = 12
CMAKE_PROGRESS_3 = 13
CMAKE_PROGRESS_4 = 14
CMAKE_PROGRESS_5 = 15
CMAKE_PROGRESS_6 = 16
CMAKE_PROGRESS_7 = 17
CMAKE_PROGRESS_8 = 18
CMAKE_PROGRESS_9 = 19
CMAKE_PROGRESS_10 = 20
CMAKE_PROGRESS_11 = 21
CMAKE_PROGRESS_1 = 15
CMAKE_PROGRESS_2 = 16
CMAKE_PROGRESS_3 = 17
CMAKE_PROGRESS_4 = 18
CMAKE_PROGRESS_5 = 19
CMAKE_PROGRESS_6 = 20
CMAKE_PROGRESS_7 = 21
CMAKE_PROGRESS_8 = 22
CMAKE_PROGRESS_9 = 23
CMAKE_PROGRESS_10 = 24
CMAKE_PROGRESS_11 = 25

4 changes: 2 additions & 2 deletions build/CMakeFiles/next_log.dir/progress.make
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
CMAKE_PROGRESS_1 = 29
CMAKE_PROGRESS_2 = 30
CMAKE_PROGRESS_1 = 33
CMAKE_PROGRESS_2 = 34

2 changes: 1 addition & 1 deletion build/CMakeFiles/progress.marks
Original file line number Diff line number Diff line change
@@ -1 +1 @@
30
34
82 changes: 82 additions & 0 deletions build/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,32 @@ dict_client/fast:
$(MAKE) $(MAKESILENT) -f CMakeFiles/dict_client.dir/build.make CMakeFiles/dict_client.dir/build
.PHONY : dict_client/fast

#=============================================================================
# Target rules for targets named file_server

# Build rule for target.
file_server: cmake_check_build_system
$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 file_server
.PHONY : file_server

# fast build rule for target.
file_server/fast:
$(MAKE) $(MAKESILENT) -f CMakeFiles/file_server.dir/build.make CMakeFiles/file_server.dir/build
.PHONY : file_server/fast

#=============================================================================
# Target rules for targets named file_client

# Build rule for target.
file_client: cmake_check_build_system
$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 file_client
.PHONY : file_client

# fast build rule for target.
file_client/fast:
$(MAKE) $(MAKESILENT) -f CMakeFiles/file_client.dir/build.make CMakeFiles/file_client.dir/build
.PHONY : file_client/fast

#=============================================================================
# Target rules for targets named http_server

Expand Down Expand Up @@ -268,6 +294,54 @@ demo/echo/echo_server.cpp.s:
$(MAKE) $(MAKESILENT) -f CMakeFiles/echo_server.dir/build.make CMakeFiles/echo_server.dir/demo/echo/echo_server.cpp.s
.PHONY : demo/echo/echo_server.cpp.s

demo/file_server/file_client.o: demo/file_server/file_client.cpp.o
.PHONY : demo/file_server/file_client.o

# target to build an object file
demo/file_server/file_client.cpp.o:
$(MAKE) $(MAKESILENT) -f CMakeFiles/file_client.dir/build.make CMakeFiles/file_client.dir/demo/file_server/file_client.cpp.o
.PHONY : demo/file_server/file_client.cpp.o

demo/file_server/file_client.i: demo/file_server/file_client.cpp.i
.PHONY : demo/file_server/file_client.i

# target to preprocess a source file
demo/file_server/file_client.cpp.i:
$(MAKE) $(MAKESILENT) -f CMakeFiles/file_client.dir/build.make CMakeFiles/file_client.dir/demo/file_server/file_client.cpp.i
.PHONY : demo/file_server/file_client.cpp.i

demo/file_server/file_client.s: demo/file_server/file_client.cpp.s
.PHONY : demo/file_server/file_client.s

# target to generate assembly for a file
demo/file_server/file_client.cpp.s:
$(MAKE) $(MAKESILENT) -f CMakeFiles/file_client.dir/build.make CMakeFiles/file_client.dir/demo/file_server/file_client.cpp.s
.PHONY : demo/file_server/file_client.cpp.s

demo/file_server/file_server.o: demo/file_server/file_server.cpp.o
.PHONY : demo/file_server/file_server.o

# target to build an object file
demo/file_server/file_server.cpp.o:
$(MAKE) $(MAKESILENT) -f CMakeFiles/file_server.dir/build.make CMakeFiles/file_server.dir/demo/file_server/file_server.cpp.o
.PHONY : demo/file_server/file_server.cpp.o

demo/file_server/file_server.i: demo/file_server/file_server.cpp.i
.PHONY : demo/file_server/file_server.i

# target to preprocess a source file
demo/file_server/file_server.cpp.i:
$(MAKE) $(MAKESILENT) -f CMakeFiles/file_server.dir/build.make CMakeFiles/file_server.dir/demo/file_server/file_server.cpp.i
.PHONY : demo/file_server/file_server.cpp.i

demo/file_server/file_server.s: demo/file_server/file_server.cpp.s
.PHONY : demo/file_server/file_server.s

# target to generate assembly for a file
demo/file_server/file_server.cpp.s:
$(MAKE) $(MAKESILENT) -f CMakeFiles/file_server.dir/build.make CMakeFiles/file_server.dir/demo/file_server/file_server.cpp.s
.PHONY : demo/file_server/file_server.cpp.s

demo/kvstore/dict_client.o: demo/kvstore/dict_client.cpp.o
.PHONY : demo/kvstore/dict_client.o

Expand Down Expand Up @@ -739,6 +813,8 @@ help:
@echo "... dict_server"
@echo "... echo_client"
@echo "... echo_server"
@echo "... file_client"
@echo "... file_server"
@echo "... http_server"
@echo "... next_core"
@echo "... next_http"
Expand All @@ -749,6 +825,12 @@ help:
@echo "... demo/echo/echo_server.o"
@echo "... demo/echo/echo_server.i"
@echo "... demo/echo/echo_server.s"
@echo "... demo/file_server/file_client.o"
@echo "... demo/file_server/file_client.i"
@echo "... demo/file_server/file_client.s"
@echo "... demo/file_server/file_server.o"
@echo "... demo/file_server/file_server.i"
@echo "... demo/file_server/file_server.s"
@echo "... demo/kvstore/dict_client.o"
@echo "... demo/kvstore/dict_client.i"
@echo "... demo/kvstore/dict_client.s"
Expand Down
84 changes: 84 additions & 0 deletions demo/file_server/file_client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@

#include <unistd.h>
#include <cstring>
#include <fstream>
#include <iostream>
#include <memory>
#include "core/connection.h"
#include "core/net_addr.h"
#include "core/socket.h"
#include "core/thread_pool.h"
#include "http/http_utils.h"

#define BUF_SIZE 2048

namespace Next {
class File_Client {
public:
explicit File_Client(NetAddress &server_address) {
auto client_socket = std::make_unique<Socket>();
client_socket->Connect(server_address);
client_conn = std::make_unique<Connection>(std::move(client_socket));
std::cout << "File_Client: Connected with " << server_address.ToString() << std::endl;
}
void Begin() {
Usage();
char buf[BUF_SIZE + 1];
memset(buf, 0, sizeof(buf));
int fd = client_conn->GetFd();
while (true) {
// scan form user keyboard
auto actual_read = read(STDIN_FILENO, buf, BUF_SIZE);
if (strncmp(buf, "quit", strlen("quit")) == 0 || strncmp(buf, "q", strlen("q")) == 0) {
// client indicates to exit
exit(0);
}
auto tokens = Next::Http::Spilt(buf, "\n");
if (tokens.empty()) {
std::cout << "input error, try again." << std::endl;
continue;
}
std::string filename = tokens[0];
std::ifstream file(filename, std::ios::binary | std::ios::ate);
if (!file) {
std::cout << "can't open file, try again." << std::endl;
continue;
}
int file_size = file.tellg();
file.seekg(0, std::ios::beg);

int send_size = 0;
// std::cout << "file :" << filename << ", size : " << file_size << std::endl;
while (send_size < file_size) {
memset(buf, 0, sizeof(buf));
int bufferSize = (file_size - send_size) < BUFSIZ + 1 ? (file_size - send_size) : BUFSIZ + 1;
file.read(buf, bufferSize);
send(fd, buf, bufferSize, 0);
send_size += bufferSize;
}
memset(buf, 0, sizeof(buf));
// blocking wait for server's response
auto actual_recv = recv(fd, buf, BUF_SIZE, 0);
write(STDOUT_FILENO, buf, actual_recv);
// scan next user input
memset(buf, 0, sizeof(buf));
}
}
void Usage() {
std::cout << "Usage:\n"
<< "\tfilename\n"
<< "\tquit(q)\n\n";
}

private:
std::unique_ptr<Connection> client_conn;
};

} // namespace Next

int main(int argc, char *argv[]) {
Next::NetAddress server_addr{"0.0.0.0", 10080};
Next::File_Client dict_client{server_addr};
dict_client.Begin();
return 0;
}
Loading

0 comments on commit 0a89528

Please sign in to comment.