File: experiments.c

package info (click to toggle)
nws 2.11-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,700 kB
  • ctags: 2,820
  • sloc: ansic: 28,849; sh: 3,289; java: 1,205; makefile: 697; perl: 12
file content (240 lines) | stat: -rw-r--r-- 6,476 bytes parent folder | download
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* $Id: experiments.c,v 1.3 2004/11/15 20:59:44 graziano Exp $ */

#include "config_nws.h"

#include <stdlib.h>
#include <string.h>

#include "diagnostic.h"    /* FAIL() WARN() */
#include "host_protocol.h" /* Host connection & registration routines */
#include "strutil.h"       /* GETTOK() GETWORD() SAFESTRCPY() vstrncpy() */
#include "messages.h"      /* message send/receive */
#include "osutil.h"        /* CurrentTime() */
#include "nws_memory.h"    /* Memory-specific messages */
#include "experiments.h"  /* our prototypes */


/**
 * A "local" function of StoreExperiments().  Packs the #count#-long
 * array of experiments #from# into #to#, returning the record size in
 * #rec_size#.
 */
static int
PackExperiments(	const NWSAPI_Measurement *from,
			size_t count,
			char **to,
			size_t *rec_size) {
	char *curr;
	int i;
	char rec_buff[63 + 1];

	/* make a test record to see how big they will be */
	memset(rec_buff, 0, sizeof(rec_buff));
	sprintf(rec_buff, WEXP_FORMAT, from->timeStamp, from->measurement);
	*rec_size = strlen(rec_buff);

	*to = (char *)MALLOC(*rec_size * count + 1);  /* Allow trailing '\0'. */
	if(*to == NULL) {
		FAIL("PackExperiments: out of memory\n");
	}
	memset(*to, ' ', *rec_size * count);

	curr = *to;

	for(i = 0; i < count; i++) {
		memset(rec_buff, 0, sizeof(rec_buff));
		sprintf(curr, WEXP_FORMAT, from->timeStamp, from->measurement);
		from++;
		curr += *rec_size;
	}

	return(1);
}


/*
** Extracts experiment information from #from# and stores it in the
** #max_count#-long array #to#.  Returns the number of experiments stored in
** #count#.
*/
static int
UnpackExperiments(	const struct nws_memory_state *s,
			const char *from,
			size_t max_count,
			NWSAPI_Measurement *to,
			size_t *count) {
	const char *curr;
	int i;
	char word[128];

	curr = from;

	for(i = 0; (i < s->rec_count) && (i < max_count); i++) {
		if(!GETWORD(word, curr, &curr)) {
			WARN1("UnpackExperiments: formatting failed: %s\n", curr);
			break;
		}
		to[i].timeStamp = strtod(word, NULL);
		if(!GETWORD(word, curr, &curr)) {
			WARN1("UnpackExperiments: formatting failed: %s\n", curr);
			break;
		}
		to[i].measurement = strtod(word, NULL);
	}
	*count = i;

	return(1);
}


int
LoadExperiments(	struct host_cookie *mem_c,
			const char *exp_name,
			NWSAPI_Measurement *meas,
			size_t count,
			double seq_no,
			size_t *out_count,
			double *out_seq_no,
			double timeout) {
	DataDescriptor des = SIMPLE_DATA(CHAR_TYPE, 0);
	char *expContents;
	struct nws_memory_state expState;
	Socket memorySock;
	size_t recvSize;

	*out_count = 0;
	*out_seq_no = 0.0;

	/* let's contact the memory */
	if(!ConnectToHost(mem_c, &memorySock)) {
		FAIL2("LoadExperiments: couldn't contact server %s at port %d\n", mem_c->name, mem_c->port);
	}

	memset(&expState, 0, sizeof(struct nws_memory_state));
	SAFESTRCPY(expState.id, exp_name);
	expState.rec_count = count;

	/* a negative seq_no means to read count number of records
	 * regardless */
	expState.seq_no = (seq_no >= 0.0) ? seq_no : 0.0;

	if(!SendMessageAndData(	memorySock,
				FETCH_STATE,
				&expState,
				stateDescriptor,
				stateDescriptorLength,
				timeout)) {
		DisconnectHost(mem_c);
		FAIL("LoadExperiments: error making request\n");
	} else if(!RecvMessage(memorySock, STATE_FETCHED, &recvSize, timeout)) {
		DisconnectHost(mem_c);
		FAIL("LoadExperiments: message receive failed\n");
	} else if(recvSize == 0) {
		*out_count = 0;
	} else if(!RecvData(	memorySock,
				&expState,
				stateDescriptor,
				stateDescriptorLength,
				timeout)) {
		DisconnectHost(mem_c);
		FAIL("LoadExperiments: state receive failed\n");
	} else if(expState.rec_count == 0) {
		*out_count = 0;
	} else {
		des.repetitions = expState.rec_size * expState.rec_count;
		expContents = (char *)MALLOC(des.repetitions + 1);
		if (expContents == NULL) {
			DisconnectHost(mem_c);
			FAIL("LoadExperiments: out of memory\n");
		}
		if (!RecvData(	memorySock,
				expContents,
				&des,
				1,
				timeout)) {
			FREE(expContents);
			DisconnectHost(mem_c);
			FAIL("LoadExperiments: data receive failed\n");
		}
		expContents[des.repetitions] = '\0';
		if (!UnpackExperiments(&expState, expContents, count, meas, out_count)) {
			FREE(expContents);
			FAIL("LoadExperiments: unable to unpack experiment data.\n");
		}
		FREE(expContents);
	}

	*out_seq_no = expState.seq_no;

	return(1);
}

int
StoreNewExperiments(	struct host_cookie *mem_c,
			const char *id,
			const NWSAPI_Measurement *exper,
			size_t count,
			double forHowLong) {
	char *content;
	DataDescriptor contentsDescriptor = SIMPLE_DATA(CHAR_TYPE, 0);
	struct nws_memory_new_state expState;
	size_t recSize;
	int ret;
	MessageHeader header;

	if (!ConnectToHost(mem_c, NULL)) {
		INFO2("StoreNewExperiments: couldn't contact server %s at port %d\n", mem_c->name, mem_c->port);
		return 0;
	}

	if (!PackExperiments(exper, count, &content, &recSize)) {
		FAIL("StoreNewExperiments: packing failed\n");
	}

	/* as of version 2.9, we piggy back the id (that now is the full
	 * experiment registration) in the content */
	expState.id_len = strlen(id);
	expState.rec_count = count;
	expState.seq_no = CurrentTime();
	expState.rec_size = recSize;
	expState.time_out = forHowLong;
	contentsDescriptor.repetitions = count * recSize;
	
	/* now let's piggy back the registration */
	contentsDescriptor.repetitions += expState.id_len;
	content = (char *)REALLOC(content, contentsDescriptor.repetitions);
	memmove(content + expState.id_len, content, count * recSize);
	memcpy(content, id, expState.id_len);

	ret = SendMessageAndDatas(mem_c->sd,
       	                   STORE_AND_REGISTER,
       	                   &expState,
       	                   newStateDescriptor,
       	                   newStateDescriptorLength,
       	                   content,
       	                   &contentsDescriptor,
       	                   1,
       	                   -1);
	FREE(content);
	if (!ret) {
		DisconnectHost(mem_c);
		FAIL("StoreNewExperiments: error storing experiment\n");
	}

	/* let's get the ack */
	if (!RecvHeader(mem_c->sd, &header, -1)) {
		DisconnectHost(mem_c);
		FAIL("StoreNewExperiments: error receiving ack\n");
	}
	/* let's check is the right one */
	if (header.message == MEMORY_FAILED) {
		DisconnectHost(mem_c);
		FAIL("StoreNewExperiments: received MEMORY_FAILED\n");
	} else if (header.message != STATE_STORED) {
		DisconnectHost(mem_c);
		FAIL("StoreNewExperiments: received unknown message\n");
	}

	return ret;
}