Skip to content

Commit 54d36a9

Browse files
committed
fixed linting and do format-fix on all source files
1 parent fe84eaa commit 54d36a9

10 files changed

+511
-559
lines changed

CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ target_compile_features(clt-svr_lib PUBLIC cxx_std_20)
4444

4545
# ---- Declare executables ----
4646

47-
add_executable(svr source/server.cc)
47+
add_executable(svr source/server.cpp)
4848
add_executable(clt-svr::svr ALIAS svr)
4949

5050
set_target_properties(
@@ -60,7 +60,7 @@ target_link_libraries(svr PRIVATE clt-svr_lib
6060
fmt::fmt
6161
Threads::Threads)
6262

63-
add_executable(clt source/client.cc)
63+
add_executable(clt source/client.cpp)
6464
add_executable(clt-svr::clt ALIAS clt)
6565

6666
set_target_properties(

cmake/lint-targets.cmake

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
set(
22
FORMAT_PATTERNS
33
source/*.cpp source/*.hpp
4-
include/*.hpp
4+
source/*.cc source/*.h
5+
include/*.hpp include/*.h
56
test/*.cpp test/*.hpp
7+
test/*.h test/*.cc
68
CACHE STRING
79
"; separated patterns relative to the project source dir to format"
810
)

cmake/lint.cmake

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ default(FORMAT_COMMAND clang-format)
1010
default(
1111
PATTERNS
1212
source/*.cpp source/*.hpp
13-
include/*.hpp
13+
source/*.h source/*.cc
14+
include/*.hpp include/*.h
1415
test/*.cpp test/*.hpp
16+
test/*.cc test/*.h
1517
)
1618
default(FIX NO)
1719

source/callback.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// TODO:
22
// 1. decode the request (deserialize using protobufs)
33
// 2. execute the request
4-
void func1(size_t size, char* buffer) {
5-
}
4+
void func1(size_t size, char * buffer) {}

source/client.cpp

+126-155
Original file line numberDiff line numberDiff line change
@@ -1,186 +1,157 @@
1-
#include <iostream>
1+
#include <algorithm>
2+
#include <atomic>
3+
#include <condition_variable>
24
#include <cstring>
3-
#include <netinet/in.h>
4-
#include <sys/socket.h>
5-
#include <arpa/inet.h>
6-
#include <netdb.h>
7-
#include <stdio.h>
8-
#include <stdlib.h>
9-
#include <unistd.h>
10-
11-
5+
#include <iostream>
126
#include <memory>
7+
#include <mutex>
138
#include <thread>
149
#include <vector>
15-
#include <fcntl.h>
16-
1710

11+
#include <arpa/inet.h>
1812
#include <errno.h>
19-
#include <sys/types.h>
20-
#include <time.h>
2113
#include <fcntl.h>
22-
#include <sys/wait.h>
23-
#include <signal.h>
24-
#include <thread>
25-
#include <vector>
26-
#include <mutex>
27-
#include <condition_variable>
28-
#include <algorithm>
29-
#include <memory>
30-
#include <atomic>
31-
#include "client_message.pb.h"
32-
#include "client_thread.h"
33-
34-
3514
#include <google/protobuf/io/coded_stream.h>
3615
#include <google/protobuf/io/zero_copy_stream_impl.h>
3716
#include <google/protobuf/text_format.h>
17+
#include <netdb.h>
18+
#include <netinet/in.h>
19+
#include <signal.h>
20+
#include <stdio.h>
21+
#include <stdlib.h>
22+
#include <sys/socket.h>
23+
#include <sys/types.h>
24+
#include <sys/wait.h>
25+
#include <time.h>
26+
#include <unistd.h>
3827

28+
#include "client_message.pb.h"
29+
#include "client_thread.h"
3930
#include "shared.h"
4031

41-
int nb_clients = -1;
42-
int port = -1;
32+
int nb_clients = -1;
33+
int port = -1;
4334
int nb_messages = -1;
4435

4536
uint64_t last_result = 0;
4637

47-
std::string _string = "llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll";
48-
std::atomic<uint32_t> global_number{0};
49-
50-
51-
52-
std::unique_ptr<char[]> get_operation(size_t& size) {
53-
static int i = 0;
54-
static int j = 1;
55-
if (j > 100) {
56-
j = 0;
57-
58-
}
59-
60-
sockets::client_msg _msg;
61-
if (i % 3 == 0) {
62-
i++;
63-
for (int k = 0; k < j; k++) {
64-
sockets::client_msg::OperationData* op = _msg.add_ops();
65-
op->set_argument(1);
66-
global_number.fetch_add(1);
67-
op->set_type(sockets::client_msg::ADD);
68-
69-
}
70-
j++;
71-
72-
std::string msg_str;
73-
_msg.SerializeToString(&msg_str);
74-
char number[4];
75-
size_t sz = msg_str.size();
76-
convertIntToByteArray(number, sz);
77-
std::unique_ptr<char[]> buf = std::make_unique<char[]>(sz+4);
78-
::memcpy(buf.get(), number, 4);
79-
80-
::memcpy(buf.get()+4, msg_str.c_str(), sz);
81-
size = sz + 4;
82-
83-
return std::move(buf);
84-
85-
38+
std::string _string =
39+
"llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll"
40+
"llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll"
41+
"llllllllllllllllllll";
42+
std::atomic<uint32_t> global_number {0};
43+
44+
std::unique_ptr<char[]> get_operation(size_t & size) {
45+
static int i = 0;
46+
static int j = 1;
47+
if (j > 100) {
48+
j = 0;
49+
}
50+
51+
sockets::client_msg _msg;
52+
if (i % 3 == 0) {
53+
i++;
54+
for (int k = 0; k < j; k++) {
55+
sockets::client_msg::OperationData * op = _msg.add_ops();
56+
op->set_argument(1);
57+
global_number.fetch_add(1);
58+
op->set_type(sockets::client_msg::ADD);
8659
}
87-
else if (i % 3 == 1){
88-
i++;
89-
for (int k = 0; k < j; k++) {
90-
sockets::client_msg::OperationData* op = _msg.add_ops();
91-
op->set_argument(1);
92-
global_number.fetch_sub(1);
93-
op->set_type(sockets::client_msg::SUB);
94-
95-
}
96-
j++;
97-
98-
std::string msg_str;
99-
_msg.SerializeToString(&msg_str);
100-
101-
char number[4];
102-
size_t sz = msg_str.size();
103-
convertIntToByteArray(number, sz);
104-
std::unique_ptr<char[]> buf = std::make_unique<char[]>(sz+4);
105-
::memcpy(buf.get(), number, 4);
106-
::memcpy(buf.get()+4, msg_str.c_str(), sz);
107-
size = sz + 4;
108-
return std::move(buf);
109-
110-
60+
j++;
61+
62+
std::string msg_str;
63+
_msg.SerializeToString(&msg_str);
64+
char number[4];
65+
size_t sz = msg_str.size();
66+
convertIntToByteArray(number, sz);
67+
std::unique_ptr<char[]> buf = std::make_unique<char[]>(sz + 4);
68+
::memcpy(buf.get(), number, 4);
69+
70+
::memcpy(buf.get() + 4, msg_str.c_str(), sz);
71+
size = sz + 4;
72+
73+
return std::move(buf);
74+
75+
} else if (i % 3 == 1) {
76+
i++;
77+
for (int k = 0; k < j; k++) {
78+
sockets::client_msg::OperationData * op = _msg.add_ops();
79+
op->set_argument(1);
80+
global_number.fetch_sub(1);
81+
op->set_type(sockets::client_msg::SUB);
11182
}
112-
else {
113-
i++;
114-
for (int k = 0; k < j; k++) {
115-
sockets::client_msg::OperationData* op = _msg.add_ops();
116-
op->set_random_data(_string);
117-
op->set_type(sockets::client_msg::RANDOM_DATA);
118-
119-
}
120-
j++;
121-
122-
std::string msg_str;
123-
_msg.SerializeToString(&msg_str);
124-
125-
char number[4];
126-
size_t sz = msg_str.size();
127-
convertIntToByteArray(number, sz);
128-
std::unique_ptr<char[]> buf = std::make_unique<char[]>(sz+4);
129-
::memcpy(buf.get(), number, 4);
130-
::memcpy(buf.get()+4, msg_str.c_str(), sz);
131-
size = sz + 4;
132-
return std::move(buf);
133-
134-
135-
83+
j++;
84+
85+
std::string msg_str;
86+
_msg.SerializeToString(&msg_str);
87+
88+
char number[4];
89+
size_t sz = msg_str.size();
90+
convertIntToByteArray(number, sz);
91+
std::unique_ptr<char[]> buf = std::make_unique<char[]>(sz + 4);
92+
::memcpy(buf.get(), number, 4);
93+
::memcpy(buf.get() + 4, msg_str.c_str(), sz);
94+
size = sz + 4;
95+
return std::move(buf);
96+
97+
} else {
98+
i++;
99+
for (int k = 0; k < j; k++) {
100+
sockets::client_msg::OperationData * op = _msg.add_ops();
101+
op->set_random_data(_string);
102+
op->set_type(sockets::client_msg::RANDOM_DATA);
136103
}
137-
138-
104+
j++;
105+
106+
std::string msg_str;
107+
_msg.SerializeToString(&msg_str);
108+
109+
char number[4];
110+
size_t sz = msg_str.size();
111+
convertIntToByteArray(number, sz);
112+
std::unique_ptr<char[]> buf = std::make_unique<char[]>(sz + 4);
113+
::memcpy(buf.get(), number, 4);
114+
::memcpy(buf.get() + 4, msg_str.c_str(), sz);
115+
size = sz + 4;
116+
return std::move(buf);
117+
}
139118
}
140119

141-
142-
143-
144-
145-
146120
void client() {
147-
class client_thread c_thread;
148-
149-
c_thread.connect_to_the_server(port, "localhost");
150-
uint64_t bytes_sent = 0;
151-
int iterations = nb_messages;
152-
while (iterations > 0) {
153-
size_t size = 0;
154-
std::unique_ptr<char[]> buf = get_operation(size);
155-
c_thread.sent_request(buf.get(), size);
156-
iterations--;
157-
}
121+
class client_thread c_thread;
122+
123+
c_thread.connect_to_the_server(port, "localhost");
124+
uint64_t bytes_sent = 0;
125+
int iterations = nb_messages;
126+
while (iterations > 0) {
127+
size_t size = 0;
128+
std::unique_ptr<char[]> buf = get_operation(size);
129+
c_thread.sent_request(buf.get(), size);
130+
iterations--;
131+
}
158132
}
159133

134+
int main(int args, char * argv[]) {
135+
if (args < 5) {
136+
std::cerr
137+
<< "usage: ./client <nb_threads> <hostname> <port> <nb_messages>\n";
138+
exit(-1);
139+
}
160140

161-
int main (int args, char* argv[]) {
162-
163-
if (args < 5) {
164-
std::cerr << "usage: ./client <nb_threads> <hostname> <port> <nb_messages>\n";
165-
exit(-1);
166-
}
167-
168-
nb_clients = std::atoi(argv[1]);
169-
port = std::atoi(argv[3]);
170-
nb_messages = std::atoi(argv[4]);
171-
141+
nb_clients = std::atoi(argv[1]);
142+
port = std::atoi(argv[3]);
143+
nb_messages = std::atoi(argv[4]);
172144

173-
// creating the client threads
174-
std::vector<std::thread> threads;
145+
// creating the client threads
146+
std::vector<std::thread> threads;
175147

176-
for (size_t i = 0; i < nb_clients; i++) {
148+
for (size_t i = 0; i < nb_clients; i++) {
149+
threads.emplace_back(std::thread(client));
150+
}
177151

178-
threads.emplace_back(std::thread(client));
179-
}
180-
181-
for (auto& thread : threads) {
182-
thread.join();
183-
}
152+
for (auto & thread : threads) {
153+
thread.join();
154+
}
184155

185-
std::cout << "** all threads joined **\n";
156+
std::cout << "** all threads joined **\n";
186157
}

0 commit comments

Comments
 (0)