-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserprogram_multipaxos_testing.cpp
More file actions
executable file
·127 lines (102 loc) · 3.11 KB
/
userprogram_multipaxos_testing.cpp
File metadata and controls
executable file
·127 lines (102 loc) · 3.11 KB
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
//
// Created for CSE513 course, Project 1
// EECS Department
// Pennsylvania State University
//
/* This program will create 3 clients and do a read and a write operation with each of them cuncurrently.
* Please set the varaiable servers before use
*/
#include "client.h"
#include <cstdio>
#include <string>
#include <vector>
#include <ctime>
#include <thread>
typedef unsigned int uint;
#define NUMBER_OF_CLIENTS 3
#define SIZE_OF_VALUE 3 //bytes
// Define your server information here
static struct Server_info servers[] = {
{"127.0.0.1", 10000},
{"127.0.0.1", 10001},
{"127.0.0.1", 10002}};
static char key[] = "123456"; // We only have one key in this userprogram
namespace Thread_helper{
void _put(const struct Client* c, const char* key, uint32_t key_size, const char* value, uint32_t value_size){
int status = put(c, key, key_size, value, value_size);
if(status == 0){ // Success
return;
}
else{
exit(-1);
}
return;
}
void _get(const struct Client* c, const char* key, uint32_t key_size, char** value, uint32_t *value_size){
int status = get(c, key, key_size, value, value_size);
if(status == 0){ // Success
return;
}
else{
exit(-1);
}
return;
}
}
int main(int argc, char* argv[]){
if(argc != 1){
fprintf(stderr, "%s%s%s\n", "Error\n"
"Usage: ", argv[0], "\n\n"
"Please note to run the servers first\n"
"This application is just for your testing purposes, "
"and the evaluation of your code will be done with another initiation of your Client libraries.");
return -1;
}
// Running mp Client (Multi-Paxos)
// Create clients
struct Client* mp_clt[NUMBER_OF_CLIENTS];
for(uint i = 0; i < NUMBER_OF_CLIENTS; i++){
mp_clt[i] = client_instance(i, "MP", servers, sizeof(servers) / sizeof(struct Server_info));
if(mp_clt[i] == NULL){
fprintf(stderr, "%s\n", "Error occured in creating clients");
return -1;
}
}
// Do write operations concurrently
std::vector<std::thread*> threads;
srand(time(0));
for(uint i = 0; i < NUMBER_OF_CLIENTS; i++){
// build a random value
char value[SIZE_OF_VALUE]={0};
for(int i = 0; i < SIZE_OF_VALUE; i++){
value[i] = '0' + rand() % 10 ;
}
// run the thread
threads.push_back(new std::thread(Thread_helper::_put, mp_clt[i], key, sizeof(key), value, sizeof(value)));
}
// Wait for all threads to join
for(uint i = 0; i < NUMBER_OF_CLIENTS; i++){
threads[i]->join();
}
// Do get operations concurrently
threads.clear();
char* values[NUMBER_OF_CLIENTS];
uint32_t value_sizes[NUMBER_OF_CLIENTS];
for(uint i = 0; i < NUMBER_OF_CLIENTS; i++){
// run the thread
threads.push_back(new std::thread(Thread_helper::_get, mp_clt[i], key, sizeof(key), &values[i], &value_sizes[i]));
}
// Wait for all threads to join
for(uint i = 0; i < NUMBER_OF_CLIENTS; i++){
threads[i]->join();
}
// remmeber after using values, delete them to avoid memory leak
// Clean up allocated memory in struct Client
for(uint i = 0; i < NUMBER_OF_CLIENTS; i++){
if(client_delete(mp_clt[i]) == -1){
fprintf(stderr, "%s\n", "Error occured in deleting clients");
return -1;
}
}
return 0;
}