-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
194 lines (137 loc) · 4 KB
/
client.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <ObjStore.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define EQUALS(a,b) strncmp(a,b,strlen(b)) == 0
#define STRING "0123456789"
int strOp = 0; //numero store effettuate
int retOp = 0; //numero retrieve effettuate
int delOp = 0; //numero delete effettuate
int strFail = 0; //numero store fallite
int retFail = 0; //numero retrieve fallite
int delFail = 0; //numero delete fallite
int strData = 0; //byte memorizzati con successo
int retData = 0; //byte recuperati con successo
int con = 1; //esito connect
int dis = 0; //esito disconnect
/*
DESCRIPTION: Richiede al server di memorizzare un blocco di dati contenente la stringa STRING ripetuta size/10 volte
PARAMETERS: name -> memoria per nome, size -> dimensione dati (in byte)
*/
void store(char* name, size_t size){
static int i=0;
char* data = (char*)malloc((size+1)*sizeof(char));
if(data == NULL){perror("malloc"); return;}
strcpy(data,STRING);
for(int j=1; j<size/10; j++){
strcat(data,STRING);
}
if(snprintf(name,6*sizeof(char),"obj%d",i++) < 0){fprintf(stderr,"ERROR: snprintf\n"); return;}
strOp++;
if(!os_store(name,data, size*sizeof(char))){strFail++;}
else strData+=size*sizeof(char);
free(data);
}
/*
DESCRIPTION: Effettua 20 store con dimensioni crescenti
*/
void stores(){
char *name;
size_t size = 100;
name = malloc(6*sizeof(char));
if(name == NULL){perror("malloc"); return;}
store(name,size);
for(int i=1; i<19; i++){
size += 5300;
store(name,size);
}
size = 100000;
store(name,size);
free(name);
}
/*
DESCRIPTION: Richiede al server di recuperare un blocco di dati che dovrebbe contenere la stringa STRING ripetuta size/10 volte
PARAMETERS: name -> memoria per nome, size -> dimensione dati (in byte)
*/
void retrieve(char* name, size_t size){
char *controlData, *data;
static int i=0;
controlData = (char*)malloc((size+1)*sizeof(char));
if(controlData == NULL){perror("malloc"); return;}
strcpy(controlData,STRING);
for(int j=1; j<10; j++){
controlData = strcat(controlData,STRING);
}
if(snprintf(name,6*sizeof(char),"obj%d",i++) < 0){fprintf(stderr,"ERROR: snprintf\n"); return;}
retOp++;
data = os_retrieve(name);
if(data == NULL){retFail++; free(controlData); return;}
if(!EQUALS(data,controlData)){retFail++;}
else retData+=size*sizeof(char);
free(data);
free(controlData);
}
/*
DESCRIPTION: Effettua 20 retrieve con dimensioni crescenti
*/
void retrieves(){
char* name;
int size = 100;
name = malloc(6*sizeof(char));
if(name == NULL){perror("malloc"); return;}
retrieve(name,size);
for(int i=1; i<19; i++){
size += 5300;
retrieve(name,size);
}
size = 100000;
retrieve(name,size);
free(name);
}
/*
DESCRIPTION: Effettua 20 cancellazioni con nomi obj$Indice
*/
void deletes(){
char *name;
name = malloc(6*sizeof(char));
if(name == NULL){perror("malloc"); return;}
for(int i=0; i<20; i++){
if(snprintf(name,6*sizeof(char),"obj%d",i) < 0){fprintf(stderr,"ERROR: snprintf\n"); return;}
delOp++;
if(!os_delete(name)){delFail++;}
}
free(name);
}
/*
DESCRIPTION: Stampa il report del test
*/
void debug(char* arg){
if(!con){
printf("REPORT 1 1\n");
printf("CONNECT 0\n");
return;
}
int totOp = strOp+delOp+retOp+2;
int totFail = strFail+delFail+retFail;
if(!dis)totFail++;
printf("REPORT %d %d\n",totOp, totFail);
printf("CONNECT %d\n",con);
if(EQUALS(arg,"1") || EQUALS(arg,"0"))printf("STORE %d %d %d\n",strOp, strFail, strData);
if(EQUALS(arg,"2") || EQUALS(arg,"0"))printf("RETRIEVE %d %d %d\n",retOp, retFail, retData);
if(EQUALS(arg,"3") || EQUALS(arg,"0"))printf("DELETE %d %d\n",delOp, delFail);
printf("LEAVE %d\n",dis);
}
int main(int argc, char* argv[]){
if(os_connect(argv[1])){
if(EQUALS(argv[2],"1")){stores();}
else if(EQUALS(argv[2],"2")){retrieves();}
else if(EQUALS(argv[2],"3")){deletes();}
else if(EQUALS(argv[2],"0")){stores(); retrieves(); deletes();}
else{printf("ERROR: %s do not recognized\n",argv[2]);}
dis = os_disconnect();
}
else con = 0;
debug(argv[2]);
return 0;
}