-
Notifications
You must be signed in to change notification settings - Fork 0
/
synclient.c
176 lines (138 loc) · 5.09 KB
/
synclient.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
/* A simple synchronous XML-RPC client written in C, as an example of an
Xmlrpc-c client. This invokes the sample.add procedure that the Xmlrpc-c
example xmlrpc_sample_add_server.c server provides. I.e. it adds two
numbers together, the hard way.
This sends the RPC to the server running on the local system ("localhost"),
HTTP Port 8080.
*/
#include <stdlib.h>
#include <stdio.h>
#include "queue.h"
#include "prime.h"
#include <openssl/ssl.h>
#include <xmlrpc-c/base.h>
#include <xmlrpc-c/client.h>
#include <xmlrpc-c/config.h> /* information about this build environment */
#define NAME "Xmlrpc-c Test Client"
#define VERSION "1.0"
static void
dieIfFaultOccurred (xmlrpc_env * const envP) {
if (envP->fault_occurred) {
fprintf(stderr, "ERROR: %s (%d)\n",
envP->fault_string, envP->fault_code);
exit(1);
}
}
xmlrpc_value* generate_request_array ( );
void difference();
const char* extract_result ( );
int
main(int const argc,
const char ** const argv) {
if(argc < 2 )
{
fprintf(stderr,"Usage : ./client <Semantics> <No of Servers>\n");
exit(EXIT_FAILURE);
}
const char const **serverUrl;
int i = 0, port = 8080 ;
xmlrpc_env env;
xmlrpc_value * resultP;
int nUrl = atoi(argv[2]);
struct timeval start,end,diff;
if(nUrl == 0)
{
fprintf(stderr,"Please enter non-zero number of servers\n");
exit(EXIT_FAILURE);
}
#if 0
serverUrl = (char **) malloc (nUrl * sizeof(char*));
for(i = 0 ; i < nUrl ; i++){
serverUrl[i] = (char *) malloc(50 * sizeof(char));
sprintf(serverUrl[i],"http://localhost:%d/RPC2",port++);
}
#endif
#if 1
serverUrl = malloc(sizeof(char*) * 5);
serverUrl[0] = "http://localhost:8080/RPC2";
serverUrl[1] = "http://localhost:8081/RPC2";
serverUrl[2] = "http://localhost:8082/RPC2";
serverUrl[3] = "http://localhost:8083/RPC2";
serverUrl[4] = "http://localhost:8084/RPC2";
#endif
const char * const methodName = "calculate_modexp";
//const char * const serverUrl = "http://localhost:8080/RPC2";
/* Initialize our error-handling environment. */
xmlrpc_env_init(&env);
/* Start up our XML-RPC client library. */
xmlrpc_client_init2(&env, XMLRPC_CLIENT_NO_FLAGS, NAME, VERSION, NULL, 0);
dieIfFaultOccurred(&env);
// printf("Making XMLRPC call to server url '%s' method '%s' " "to request the sum " "of 5 and 7...\n", serverUrl, methodName);
/* Make the remote procedure call */
// printf("Result is : %d\n",xmlrpc_validate_utf8("Hello World"));
xmlrpc_value *arr = xmlrpc_array_new(&env);
//resultP = xmlrpc_client_call(&env, serverUrl, methodName,"(A)", generate_request_array(&env , arr) );
gettimeofday(&start,NULL);
/*Call the server*/
resultP = xmlrpc_client_call_multi_sync(atoi(argv[1]),nUrl, &env, serverUrl, methodName,"(A)", generate_request_array(&env , arr));
gettimeofday(&end,NULL);
difference(&diff,end,start);
printf("Time Taken With %d Server = %ld:%ld\n",nUrl,diff.tv_usec/1000000,diff.tv_usec%1000000);
// printf("Check to see if killed\n");
dieIfFaultOccurred(&env);
/* Get our sum and print it out. */
//xmlrpc_read_int(&env, resultP, &primeResult);
// xmlrpc_env * const dummy;
// printf("Extracting Results\n");
const char* result = extract_result ( &env , resultP);
dieIfFaultOccurred(&env);
// printf("2^p/m = %s\n", result);
/*Dispose of our result value. */
xmlrpc_DECREF(resultP);
/* Clean up our error-handling environment. */
xmlrpc_env_clean(&env);
/* Shutdown our XML-RPC client library. */
xmlrpc_client_cleanup();
return 0;
}
xmlrpc_value* generate_request_array ( xmlrpc_env *env , xmlrpc_value *arr)
{
#if 1
Task task = gen_prime (task);
xmlrpc_value *v1 = xmlrpc_build_value(env, "s", task._clientid);
xmlrpc_value *v2 = xmlrpc_build_value(env, "s", task.p);
xmlrpc_value *v3 = xmlrpc_build_value(env, "s", task.m);
#endif
#if 0
char s[20];
sprintf(s,"%d",getpid());
Task task = gen_prime (task);
xmlrpc_value *v1 = xmlrpc_build_value(env, "s", s);
xmlrpc_value *v2 = xmlrpc_build_value(env, "s", "45");
xmlrpc_value *v3 = xmlrpc_build_value(env, "s", "54");
#endif
xmlrpc_array_append_item (env,arr,v1);
xmlrpc_array_append_item (env,arr,v2);
xmlrpc_array_append_item (env,arr,v3);
return arr;
}
const char* extract_result (xmlrpc_env *env , xmlrpc_value *temp)
{
xmlrpc_value * arrayP;
xmlrpc_decompose_value(env, temp, "(A)", &arrayP);
size_t size = xmlrpc_array_size(env, arrayP);
xmlrpc_value * strctP;
strctP = xmlrpc_array_get_item(env, arrayP, 0);
const char *primeResult = (const char *)malloc (256);
size_t str1_len;
xmlrpc_read_string_lp(env, strctP, &str1_len, &primeResult);
return primeResult;
}
void difference(struct timeval *diff, struct timeval now, struct timeval last)
{
int now_usec,last_usec;
now_usec = now.tv_sec*1000000 + now.tv_usec;
last_usec = last.tv_sec*1000000 + last.tv_usec;
diff->tv_usec=now_usec-last_usec; //Resolution of difference time is micro seconds
return ;
}