Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/client/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.1)
project(TCPServerClient LANGUAGES C)

add_executable(client client.c extract_ip_port_from_string.c opt_arg.c)
add_executable(client client.c utils/extract_ip_port_from_string.c utils/opt_arg.c ../lib/check_port.c ../lib/set_sockaddr_in.c)

target_include_directories(client PUBLIC ./)
Comment on lines +4 to 6
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a library directory we have libraries so, why don't you create a directory with check_callbacks and place here check_callbacks.h and build it as a library?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, you didn't fix last problem, you've included ./ but not ../lib it's unnecessary if you'll make a library, you can just target_link_libraries(client STATIC CheckCallbacks)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to explicitly set utils folder and not forcing CMake looking for it!
target_include_directories(client PUBLIC ./ ./utils)


Expand Down
14 changes: 5 additions & 9 deletions src/client/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
#include <netinet/ip.h>
#include <sys/socket.h>
#include <sys/types.h>
#include "opt_arg.h"
#include "utils/opt_arg.h"
#include "../lib/set_sockaddr_in.h"


int main(int argc, char** argv) {
Expand All @@ -21,11 +22,9 @@ int main(int argc, char** argv) {
static struct option options[] = {{"factorial", required_argument, 0, 0},
{"server", required_argument, 0, 0},
{0, 0, 0, 0}};

if (handle_options(argc, argv, options, 2, &factorial, &ip_port)) {
return 1;
}

if (factorial == -1 || !strlen(ip_port.ip)) {
fprintf(stderr, "Using: %s --factorial n --server 127.0.0.1:20001\n",
argv[0]);
Expand All @@ -39,10 +38,8 @@ int main(int argc, char** argv) {
}

struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(ip_port.port);
server.sin_addr.s_addr = *((unsigned long*)hostname->h_addr);

set_sockaddr_in(&server, AF_INET, ip_port.port,
*((unsigned long*)hostname->h_addr));
int sck = socket(AF_INET, SOCK_STREAM, 0);
if (sck < 0) {
fprintf(stderr, "Socket creation failed!\n");
Expand All @@ -53,7 +50,6 @@ int main(int argc, char** argv) {
fprintf(stderr, "Connection failed\n");
exit(1);
}

char task[sizeof(uint64_t)];
memcpy(task, &factorial, sizeof(uint64_t));

Expand All @@ -70,7 +66,7 @@ int main(int argc, char** argv) {

uint64_t answer = 0;
memcpy(&answer, response, sizeof(uint64_t));
printf("answer: %llu\n", answer);
printf("answer: %lu\n", answer);

close(sck);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "extract_ip_port_from_string.h"
#include "opt_arg.h"
#include "utils/extract_ip_port_from_string.h"
#include "utils/opt_arg.h"


unsigned int convert(char* st) {
Expand Down
12 changes: 3 additions & 9 deletions src/client/opt_arg.c → src/client/utils/opt_arg.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,14 @@
// Created by sergeyampo on 06.06.2020.
//
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include <stdlib.h>
#include "opt_arg.h"
#include "extract_ip_port_from_string.h"
#include "../../lib/check_port.h"


int check_port(int port) {
if (port < 1024) {
printf("port lower than 1024 may not be free\n");
return 1;
}
return 0;
}

int handle_options(int argc,
char** argv,
struct option options[],
Expand Down Expand Up @@ -43,7 +37,7 @@ int handle_options(int argc,
break;
case 1: memcpy(server, optarg, strlen(optarg));
extract_ip_port_from_string(server, ':', ip_port);
if (check_port(ip_port->port))
if (check_port(&ip_port->port))
return 1;
break;
default:printf("Index %d is out of options\n", option_index);
Expand Down
File renamed without changes.
14 changes: 14 additions & 0 deletions src/lib/check_port.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// Created by khrushv on 13.06.2020.
//
#include "check_port.h"
#include <stdio.h>


int check_port(unsigned int* port) {
if (*port < 1024) {
printf("port lower than 1024 may not be free\n");
return 1;
}
return 0;
}
12 changes: 12 additions & 0 deletions src/lib/check_port.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// Created by khrushv on 13.06.2020.
//



#ifndef TCPSERVERCLIENT_SRC_LIB_CHECK_PORT_H_
#define TCPSERVERCLIENT_SRC_LIB_CHECK_PORT_H_

int check_port(unsigned int* port);

#endif //TCPSERVERCLIENT_SRC_LIB_CHECK_PORT_H_
13 changes: 13 additions & 0 deletions src/lib/set_sockaddr_in.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// Created by khrushv on 14.06.2020.
//
#include <netinet/in.h>
#include "set_sockaddr_in.h"


void set_sockaddr_in(struct sockaddr_in* server,
short s_port, unsigned short port, unsigned long s_addr) {
server->sin_family = s_port;
server->sin_port = htons(port);
server->sin_addr.s_addr = s_addr;
}
9 changes: 9 additions & 0 deletions src/lib/set_sockaddr_in.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//
// Created by khrushv on 14.06.2020.
//

#ifndef TCPSERVERCLIENT_SRC_LIB_SET_SOCKADDR_IN_H_
#define TCPSERVERCLIENT_SRC_LIB_SET_SOCKADDR_IN_H_
void set_sockaddr_in(struct sockaddr_in* server,
short s_port, unsigned short port, unsigned long s_addr);
#endif //TCPSERVERCLIENT_SRC_LIB_SET_SOCKADDR_IN_H_
3 changes: 2 additions & 1 deletion src/server/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
cmake_minimum_required(VERSION 3.1)
project(TCPServerClient LANGUAGES C)

add_executable(server server.c opt_arg.c)
add_executable(server server.c utils/opt_arg.c utils/factorial.c ../lib/check_port.c ../lib/set_sockaddr_in.c)

target_include_directories(server PUBLIC ./)
find_package(Threads REQUIRED)
target_link_libraries(server ${CMAKE_THREAD_LIBS_INIT})

Expand Down
47 changes: 10 additions & 37 deletions src/server/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,17 @@

#include "pthread.h"
#include "../lib/thread_pool/thpool.h"
#include "opt_arg.h"
#include "../lib/check_port.h"
#include "utils/opt_arg.h"
#include "utils/factorial.h"
#include "../lib/set_sockaddr_in.h"

int check_tnum(unsigned int* tnum) {
if (*tnum <= 0) {
printf("tnum must be a positive number\n");
return 1;
}
return 0;
}

int check_port(unsigned int* port) {
if (*port < 1024) {
printf("port lower than 1024 may not be free\n");
return 1;
}
return 0;
}

uint64_t factorial(const uint64_t* n) {
uint64_t factorial = 1;
uint64_t r = *n;
for (uint64_t i = 2; i <= r; ++i)
factorial *= i;
return factorial;
}

void* socket_thread(void* arg) {

printf("Into thread %d\n", (int)pthread_self());
int newSocket = *((int*)arg);
while (true) {
//TODO: make variable for count of parameters
unsigned int buffer_size = sizeof(uint64_t);
char from_client[buffer_size];
int read = recv(newSocket, from_client, buffer_size, 0);
Expand All @@ -59,9 +39,9 @@ void* socket_thread(void* arg) {
}
uint64_t factorial_number = 0;
memcpy(&factorial_number, from_client, sizeof(uint64_t));
fprintf(stdout, "Receive: %llu \n", factorial_number);
fprintf(stdout, "Receive: %lu \n", factorial_number);
uint64_t total = factorial(&factorial_number);
printf("Total: %llu\n", total);
printf("Total: %lu\n", total);

char buffer[sizeof(total)];
memcpy(buffer, &total, sizeof(total));
Expand All @@ -78,7 +58,6 @@ void* socket_thread(void* arg) {

int main(int argc, char** argv) {
size_t options_amount = 2;
//TODO: remove "tnum" parameter
static struct option options[] = {{"port", required_argument, 0, 0},
{"tnum", required_argument, 0, 0},
{0, 0, 0, 0}};
Expand All @@ -87,13 +66,10 @@ int main(int argc, char** argv) {
int (* callbacks[options_amount])(unsigned int*); //Array of callbacks
callbacks[0] = check_port;
callbacks[1] = check_tnum;

unsigned int* handled_options = handle_options(argc, argv, options, options_amount, callbacks);

threadpool thpool = thpool_init(4);

unsigned port = handled_options[0];
unsigned tnum = handled_options[1];
threadpool thpool = thpool_init(tnum);

if (port == -1 || tnum == -1) {
fprintf(stderr, "Using: %s --port 20001 --tnum 4\n", argv[0]);
Expand All @@ -107,10 +83,7 @@ int main(int argc, char** argv) {
}

struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = htonl(INADDR_ANY);

set_sockaddr_in(&server, AF_INET, port, INADDR_ANY);
int opt_val = 1;
setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt_val, sizeof(opt_val));

Expand All @@ -137,7 +110,7 @@ int main(int argc, char** argv) {
fprintf(stderr, "Could not establish new connection\n");
continue;
}
printf("Num of active threads worker %d\n", thpool_num_threads_working(thpool));
thpool_add_work(thpool, (void*)socket_thread, &client_fd);
}
thpool_destroy(thpool);
Expand Down
14 changes: 14 additions & 0 deletions src/server/utils/factorial.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// Created by khrushv on 13.06.2020.
//

#include "factorial.h"


uint64_t factorial(const uint64_t* n) {
uint64_t factorial = 1;
uint64_t r = *n;
for (uint64_t i = 2; i <= r; ++i)
factorial *= i;
return factorial;
}
12 changes: 12 additions & 0 deletions src/server/utils/factorial.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// Created by khrushv on 13.06.2020.
//

#include <stdint.h>

#ifndef TCPSERVERCLIENT_SRC_SERVER_FACTORIAL_H_
#define TCPSERVERCLIENT_SRC_SERVER_FACTORIAL_H_

uint64_t factorial(const uint64_t* n);

#endif //TCPSERVERCLIENT_SRC_SERVER_FACTORIAL_H_
8 changes: 8 additions & 0 deletions src/server/opt_arg.c → src/server/utils/opt_arg.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
#include "opt_arg.h"


int check_tnum(unsigned int* tnum) {
if (*tnum <= 0) {
printf("tnum must be a positive number\n");
return 1;
}
return 0;
}

unsigned int* handle_options(int argc,
char** argv,
struct option options[],
Expand Down
2 changes: 2 additions & 0 deletions src/server/opt_arg.h → src/server/utils/opt_arg.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@

unsigned int* handle_options(int, char**, struct option[], const int, int (* [])(unsigned int*));

int check_tnum(unsigned int* tnum);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In our codestyle we don't write a name of parameter in a header file


#endif //TCPSERVERCLIENT_SRC_SERVER_OPT_ARG_H_