-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducer_consumer.c
executable file
·127 lines (95 loc) · 4.16 KB
/
producer_consumer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "producer_consumer.h"
int get_port(int argc, char **argv) {
Require(argc > 1, Exception, "get_port", "ip:port not founded");
int port = 0;
int i = find_char(argv[argc - 1], ':');
Require(i != -1, Exception, "get_port", "port not found");
for (i++; i < strlen(argv[argc - 1]); i++) {
port *= 10;
port += argv[argc - 1][i] - '0';
}
return port;
}
char *get_hostname(int argc, char **argv) {
char *hostname = malloc(sizeof(char) * 18 + 4);
bzero(hostname, sizeof(char) * 18);
for (int i = 0; i < strlen(argv[argc - 1]) && argv[argc - 1][i] != ':'; i++)
hostname[i] = argv[argc - 1][i];
return hostname;
}
void generate_default(type_amount_t *type_amount) {
NotNullRequired(type_amount, "generate_default", "type_amount its null");
type_amount[0].type[0] = (char) 65;
type_amount[0].count = 1;
}
void get_product_type(char *argv, char *let_type) {
for (int i = 0; i < strlen(argv) && argv[i] != ':'; i++)
let_type[i] = argv[i];
}
int get_number(char *argv) {
int number = 0;
int pos = find_char(argv, ':');
Require(pos != -1, Exception, "get_number", "type count not found");
for (int i = pos + 1; i < strlen(argv); i++) {
number *= 10;
number += argv[i] - '0';
}
return number;
}
void PrintStates(product_t *product, type_amount_t *type_amount, int count_types) {
fprintf(stdout,
"\n"COLOR_BLUE"<"COLOR_CLEAR COLOR_NEG COLOR_WHITE_BLUE "STATUS" COLOR_CLEAR COLOR_BLUE">" COLOR_CLEAR":\n");
printf("(Type, Amount) = \n");
for (int i = 0; i < count_types; ++i)
printf("\t\t(%s/%d)\n", type_amount[i].type, type_amount[i].count);
fprintf(stdout,
"\n"COLOR_BLUE"<"COLOR_CLEAR COLOR_NEG COLOR_WHITE_BLUE "END-STATUS" COLOR_CLEAR COLOR_BLUE">" COLOR_CLEAR"\n");
}
void ParseTypesAmount(type_amount_t *type_amount, int count_product, int *len_products,
char **argv, int is_random) {
NotNullRequired(type_amount, "ParseTypesAmount", "product its null");
Require(count_product > 0, Exception, "ParseTypesAmount", "count_product its less or equals than 0");
if (is_random)
generate_default(type_amount);
else {
for (int i = 1, c = 0; c < count_product; i++, c++) {
get_product_type(argv[i], type_amount[c].type);
type_amount[c].count = get_number(argv[i]);
*len_products += type_amount[c].count;
}
}
}
int OpenClientFD(char *hostname, int port) {
int clientfd;
struct hostent *hp;
struct sockaddr_in serveraddr;
Require((clientfd = socket(AF_INET, SOCK_STREAM, 0)) >= 0, "Socket"Exception, "OpenClientFD", "");
Require((hp = gethostbyname(hostname)) != NULL, "GetHostByName"Exception, "OpenClientFD", "");
bzero((char *) &serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
bcopy(hp->h_addr_list[0], (char *) &serveraddr.sin_addr.s_addr, hp->h_length);
serveraddr.sin_port = htons((uint16_t) port);
Require(connect(clientfd, (SA *) &serveraddr, sizeof(serveraddr)) >= 0 || close(clientfd), "Connect"Exception,
"OpenClientFD", "");
return clientfd;
}
int EstablishConnection(char *hostname, int port) {
NotNullRequired(hostname, "EstablishConnection", "Hostname its null");
int clientfd;
Require((clientfd = OpenClientFD(hostname, port)) > 0, Exception, "EstablishConnection", "OpenClientFD error.");
return clientfd;
}
void FillProducts(product_t *products, type_amount_t *amounts, int len_products, int len_types) {
NotNullRequired(products, "FillProducts", "products its null");
NotNullRequired(amounts, "FillProducts", "amounts its null");
Require(len_products > 0, Exception, "FillProducts", "len_products its less or equals than 0");
Require(len_types > 0, Exception, "FillProducts", "len_types its less or equals than 0");
int id = 1;
int pos = 0;
for (int type_index = 0; type_index < len_types; type_index++) {
for (int i = 0; i < amounts[type_index].count; ++i,++pos,++id) {
products[pos].product_id = id;
strncpy(products[pos].product_type, amounts[type_index].type, 3);
}
}
}