File: nws_api.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 (2147 lines) | stat: -rw-r--r-- 55,487 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
/* $Id: nws_api.c,v 1.19 2004/12/06 20:05:44 graziano Exp $ */

#include "config_nws.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

#include "diagnostic.h"
#include "host_protocol.h"
#include "messages.h"
#include "strutil.h"
#include "dnsutil.h"
#include "nws_sensor.h"
#include "nws_memory.h"
#include "register.h"
#include "osutil.h"
#include "skills.h"
#include "messages.h"
#include "nws_proxy.h"
#include "nws_api.h"

#include "nws_forecast_api.h"

/* module lock */
static void *lock = NULL;

/*
 * Information cached for auto-fetch series that we're tracking.  A connection
 * to the memory that holds the series and the (tab-deliminted) list of series
 * names that this memory is sending to us. #lostSeries# contains the
 * list of series that have been disconnected.
 */
typedef struct {
	struct host_cookie memory;
	registrations *series;
} AutoFetchInfo;
static registrations *lostSeries = NULL;


/*
 * Module globals.  #autoFetches# holds a list of memory connections that we're
 * using to track auto-fetched series, and #autoFetchCount# is the length of
 * this list.  The host_cookies contain connections to a default forecaster,
 * memory, and name server.  All these values are overridden by a call to one
 * of the Use*() functions.  We're supposed to pick the "best" forecaster and
 * memory if the client doesn't specify one, and there is no longer a default
 * name server, so we leave the initial values emtpy. 
 */
static AutoFetchInfo *autoFetches = NULL;
static unsigned int autoFetchCount = 0;

static struct host_cookie defaultMemory = {"", 0, NO_SOCKET};
static struct host_cookie *NSes = NULL;
static int howManyNS = 0;

/*
 * First of all the FORECAST stuff
 */
void
NWSAPI_FreeForecastState(NWSAPI_ForecastState **state) {
	FORECASTAPI_FreeForecastState((FORECASTAPI_ForecastState **)state);
}

const char *
NWSAPI_MethodName(unsigned int methodIndex) {
	return (FORECASTAPI_MethodName(methodIndex));
}

NWSAPI_ForecastState *
NWSAPI_NewForecastState() {
	return (NWSAPI_ForecastState *)FORECASTAPI_NewForecastState();
}

void
NWSAPI_UpdateForecastState(	NWSAPI_ForecastState *s,
				const NWSAPI_Measurement *m,
				unsigned int howMany,
				NWSAPI_ForecastCollection *f,
				unsigned int howManyF) {
	FORECASTAPI_UpdateForecastState((FORECASTAPI_ForecastState *)s, 
			(FORECASTAPI_Measurement *)m, 
			howMany, 
			(FORECASTAPI_ForecastCollection *)f, 
			howManyF);
}

int
NWSAPI_ComputeForecast(	NWSAPI_ForecastState *state,
			NWSAPI_ForecastCollection *forecast) {
	return FORECASTAPI_ComputeForecast((FORECASTAPI_ForecastState *)state, 
			(FORECASTAPI_ForecastCollection *)forecast);
}

/* END FORECAST */



/* utility function to get a tab-separated list of names from a
 * registrations structure. Returns a char * that needs to be freed */
static char *
GenerateList(const registrations *r) {
	char *tmp, *name;
	int len, i;

	/* let's generate the list */
	for (tmp = NULL, len = 0, i = 0;  i < r->howMany; i++) {
		name = NwsAttributeValue_r(FindNwsAttribute(r->vals[i], "name"));
		if (name == NULL) {
			WARN("GenerateList: no name??\n");
			continue;
		}
		tmp = REALLOC(tmp, len + strlen(name) + 2);
		if (tmp == NULL) {
			ABORT("GenerateList: out of memory\n");
		}
		if (len > 0) {
			tmp[len] = '\t';
			len++;
		}
		memcpy(tmp + len, name, strlen(name) + 1);
		len += strlen(name);
		FREE(name);
	}

	return tmp;
}

/* utility functions to add/remove a list of series from lostSeries
 * (which contains the series which aren't autofetching anymore due to
 * some errors) */
static void
AddToLost(const char *myList) {
	char name[255 + 1];
	const char *ind;
	int i;
	Object obj;

	/* quick check */
	if (myList == NULL || myList[0] == '\0') {
		return;
	}

	GetNWSLock(&lock);
	if (lostSeries == NULL) {
		if (!InitRegistrations(&lostSeries)) {
			ABORT("AddToLost: failed to initialize structure\n");
		}
	}
	for (ind = myList; GETWORD(name, ind, &ind); ) {
		if (SearchForName(lostSeries, name, 1, &i)) {
			/* we already have it */
			continue;
		}
		
		/* we need to insert it */
		obj = NewObject();
		AddNwsAttribute(&obj, "name", name);
		if (!InsertRegistration(lostSeries, obj, 0, i)) {
			WARN1("AddToLost: couldn't insert %s\n", name);
		}
		FreeObject(&obj);
	}
	LOG1("AddToLost: we have %d lost series\n", lostSeries->howMany);
	ReleaseNWSLock(&lock);

	return;
}
static void
AddToLostFromSocket(Socket sock) {
	int i;
	char * tmp;

	if (sock < 0) {
		return;
	}

	tmp = NULL;
	GetNWSLock(&lock);
	for (i = 0; i < autoFetchCount; i++) {
		if (autoFetches[i].memory.sd == sock) {
			/* found it */
			break;
		}
	}
	if (i < autoFetchCount) {
		tmp = GenerateList(autoFetches[i].series);
	}
	ReleaseNWSLock(&lock);

	AddToLost(tmp);
	FREE(tmp);

	return;
}
static void
RemoveFromLost(const char *myList) {
	char name[255 + 1];
	const char *ind;
	int i;

	/* sanity check */
	if (myList == NULL || lostSeries == NULL) {
		return;
	}

	GetNWSLock(&lock);
	for (ind = myList; GETWORD(name, ind, &ind); ) {
		if (!SearchForName(lostSeries, name, 1, &i)) {
			/* we don't have this series */
			continue;
		}
		DeleteRegistration(lostSeries, i);
	}
	LOG1("RemoveFromLost: we have %d lost series\n", lostSeries->howMany);
	ReleaseNWSLock(&lock);
}

/*
 * Disconnects the auto-fetch memory connected to #who# and removes its entry
 * from the #autoFetches# module global list.
 */
static void
AutoFetchDisconnect(Socket who) {
	int i;
	DataDescriptor des = SIMPLE_DATA(CHAR_TYPE, 1);

	/* just get out of here if no socket */
	if (who == NO_SOCKET) {
		return;
	}
	/* we operate on global variables all over the place */
	GetNWSLock(&lock);
	for (i = 0; i < autoFetchCount; i++) {
		if (autoFetches[i].memory.sd == who) {
			/* found it */
			break;
		}
	}
	if (i >= autoFetchCount) {
		ReleaseNWSLock(&lock);
		WARN("AutoFetchDisconnect: memory not found!\n");
		return;
	}

	INFO2("AutoFetchDisconnect: disconnecting from %s:%d\n", autoFetches[i].memory.name, autoFetches[i].memory.port);
	
	/* let's clean the autoFetches about this memory */
	while(autoFetches[i].series->howMany > 0) {
		DeleteRegistration(autoFetches[i].series, 0);
	}
	FREE(autoFetches[i].series);
	if (autoFetchCount > 1) {
		autoFetches[i] = autoFetches[--autoFetchCount];
	} else {
		FREE(autoFetches);
		autoFetchCount = 0;
	}
	ReleaseNWSLock(&lock);

	/* let's stop autofetch (empty string) */
	des.repetitions = 1;
	if (!SendMessageAndData(who, AUTOFETCH_BEGIN, "", &des, 1, 5)) {
		ERROR("AutoFetchDisconnect: failed to talk to memory\n");
	}

	/* Ignore the reply.  It might get mixed with an autofetch anyway */
	DROP_SOCKET(&who);
}

/*
 * Connects to #memory#, sends it #seriesList# in an AUTOFETCH_BEGIN message,
 * and places an entry for the connection in the #autoFetches# global.  Returns
 * 1 if successful, else 0.
 *
 * NOTE: we need to hook a check whenever a socket gets disconnected,
 * since it can be an autofetching socket.
 */
static int
AutoFetchConnect(	struct host_cookie *memory,
			const char *seriesList) {
	AutoFetchInfo *extendedAutoFetches;
	size_t ignored;
	DataDescriptor seriesDescriptor = SIMPLE_DATA(CHAR_TYPE, 0);
	char name[256], *series;
	const char *ind;
	Object obj;
	int i;

	/* sanity check */
	if (memory == NULL || seriesList == NULL) {
		FAIL("AutoFetchConnect: NULL parameters!\n");
	}

	/* let's check if we can talk to the memory first */
	if (!ConnectToHost(memory, NULL)) {
		FAIL1("AutoFetchConnect: Unable to contact memory %s\n", HostCImage(memory));
	}

	/* since we operates on the global variables, we lock a lot */
	GetNWSLock(&lock);
	/* extend the autoFetchers */
	extendedAutoFetches = (AutoFetchInfo *) REALLOC(autoFetches, (autoFetchCount + 1) * sizeof(AutoFetchInfo));
	if (extendedAutoFetches == NULL) {
		ReleaseNWSLock(&lock);
		FAIL("AutoFetchConnect: out of memory\n");
	}
	extendedAutoFetches[autoFetchCount].series = NULL;
	if (!InitRegistrations(&extendedAutoFetches[autoFetchCount].series)) {
		FREE(extendedAutoFetches);
		ReleaseNWSLock(&lock);
		FAIL("AutoFetchConnect: cannot initialize structure\n");
	}
	autoFetches = extendedAutoFetches;

	/* let's save the series/memory */
	autoFetches[autoFetchCount].memory = *memory;
	for (ind = seriesList; GETWORD(name, ind, &ind); ) {
		if (SearchForName(autoFetches[autoFetchCount].series, name, 1, &i)) {
			/* we already have it */
			continue;
		}

		/* insert the element */
		obj = NewObject();
		AddNwsAttribute(&obj, "name", name);
		if (!InsertRegistration(autoFetches[autoFetchCount].series, obj, 0, i)) {
			WARN1("AutoFetchConnect: couldn't insert %s\n", name);
		}
		FreeObject(&obj);
	}
	/* generate the list of series we got in */
	series = GenerateList(autoFetches[autoFetchCount].series);
	autoFetchCount++;
	ReleaseNWSLock(&lock);
	if (series == NULL) {
		WARN("AutoFetchConnect: empty series list?\n");
		return 0;
	}

	/* let's try to autofetch the series list */
	i = 1;
	seriesDescriptor.repetitions = strlen(series) + 1;
	if (!SendMessageAndData(memory->sd, AUTOFETCH_BEGIN, (const void *)series, &seriesDescriptor, 1, -1) || !RecvMessage(memory->sd, AUTOFETCH_ACK, &ignored, -1)) {
		i = 0;
		AutoFetchDisconnect(memory->sd);
		ERROR1("AutoFetchConnect: Memory %s refused request\n", HostCImage(memory));
	}
	FREE(series);

	return i;
}


/*
 * Contacts the name server to look up the series #seriesName# and return the
 * memory which is storing its measurements.  Returns 1 if successful, else 0.
 */
static int
GetSeriesMemory(	const char *seriesName,
                	struct host_cookie *memory,
			int timeout) {
	ObjectSet seriesObject;
	Object obj;
	char myFilter[255+1], *tmp; 
	int ret;

	snprintf(myFilter, sizeof(myFilter), "(name=%s)", seriesName);

	seriesObject = NewObjectSet();
	if (!NWSAPI_GetObjectsTimeout(myFilter, &seriesObject, timeout)) {
		FreeObject(&seriesObject);
		FAIL1("GetSeriesMemory: couldn't find the memory for %s\n", seriesName);
	}

	/* now we can have multiple series: we pick the one that does
	 * have a 'activity' field, since it means that is been
	 * currently updated. */
	ret = 0;
	NWSAPI_ForEachObject(seriesObject, obj) {
		tmp = NwsAttributeValue_r(FindNwsAttribute(obj, "memory"));
		if (tmp == NULL) {
			ERROR1("GetSeriesMemory: %s has no memory!\n", obj);
			continue;
		}

		Host2Cookie(tmp, DefaultHostPort(MEMORY_HOST), memory);
		FREE(tmp);
		ret = 1;	/* we indeed found something */

		/* if this series has an activity, we are done */
		if (FindNwsAttribute(obj, "activity") == NO_OBJECT) {
			break;
		}
	}
	FreeObject(&seriesObject);

	return ret;
}


#define OPTION_NAME_LEN (31 + 1)
typedef struct {
	char name[OPTION_NAME_LEN];
	unsigned int minOccurrences;
	unsigned int maxOccurrences;
} OptionInfo;


/*
 * A "local" function of StartActivity().  If #attr# is listed in #options#,
 * sets the fields of #intoWhere# based on the name and value of #attr# and
 * returns 1; otherwise, returns 0.
 */
static int
ParseOption(	const NWSAPI_NwsAttribute attr,
		const char *options,
		OptionInfo *intoWhere) {
	char *attrName, *tmp;
	OptionInfo newOption;

	attrName = NwsAttributeName_r(attr);
	if(attrName == NULL || strstr(options, attrName) == 0) {
		FREE(attrName);
		return 0;
	}

	/* Option values have the format MIN_to_MAX_TYPE */
	SAFESTRCPY(newOption.name, attrName);
	FREE(attrName);

	tmp = NwsAttributeValue_r(attr);
	if (tmp == NULL) {
		return 0;
	}

	/* find the min/max occurrences for this option */
	newOption.minOccurrences = (unsigned int)strtol(tmp, &attrName, 10);
	attrName += strlen("_to_");
	newOption.maxOccurrences = (unsigned int)strtol(attrName, NULL, 10);

	/* done */
	*intoWhere = newOption;
	FREE(tmp);

	return 1;
}


/*
 * given a #seriesSpec# returns the default options in a string (to be
 * freed).
 */
static char *
ResourceOptions(	const NWSAPI_SeriesSpec *series) {
	struct host_cookie host;
	const unsigned short tmp = DefaultHostPort(SENSOR_HOST);
	int res, skill, len;
	const MeasuredResources *r;
	const char *opts;
	char opt[35], options[EXP_LIST_SIZE]; 

	/* let's find first which resource we have */
	for (res = 0; res < RESOURCE_COUNT; res++) {
		if (strcmp(series->resourceName, ResourceName((MeasuredResources) res)) == 0) {
			break;
		}
	}
	if (res >= RESOURCE_COUNT) {
		/* nothing more we can do here: no idea which resource we
		 * are talking about */
		WARN1("ResourceOptions: unknown resource (%s)\n", series->resourceName);
		return NULL;
	}

	/* now let's find which skill is responsible for this resource */
	for (skill = 0; skill < SKILL_COUNT; skill++) {
		if (!SkillResources((KnownSkills)skill, &r, &len)) {
			/* ?? we shouldn't be here */
			continue;
		}
		for (; len > 0; len--) {
			if (r[len - 1] == (MeasuredResources) res) {
				/* found it */
				break;
			}
		}
		if (len > 0) {
			/* we found it */
			break;
		}
	}
	if (skill >= SKILL_COUNT) {
		/* nothing more we can do here: no idea which skill we
		 * are talking about */
		WARN1("ResourceOptions: unknown skill for resource (%s)\n", series->resourceName);
		return NULL;
	}

	/* now we can find the options supported by the skill, and find
	 * their deafaults */
	opts = SkillSupportedOptions((KnownSkills) skill);
	options[0] = '\0';
	while(GETTOK(opt, opts, ",", &opts)) {
		char *str;

		str = SkillOptionDefault((KnownSkills)skill, opt);
		if (opt == NULL) {
			/* nothing here */
			continue;
		}

		/* add to the options */
		if (options[0] != '\0') {
			strcat(options, "\t");
		}
		strcat(options, opt);
		strcat(options, ":");
		strcat(options, str);
		free(str);
	}

	/* do we have a destination? */
	if (series->destinationMachine[0] != '\0') {
		if (options[0] != '\0') {
			strcat(options, "\t");
		}
		strcat(options, "target:");
		Host2Cookie(series->destinationMachine, tmp, &host);
		strcat(options, HostCImage(&host));
	}

	return strdup(options);
}


/*
 * Attempts to connect to #host#.  If successful, copies host name and
 * port number into #whichCookie# and returns 1; else returns 0.
 */
static int
UseHost(	const NWSAPI_HostSpec *host,
		struct host_cookie *whichCookie) {
	struct host_cookie tmp;

	MakeHostCookie(host->machineName, host->machinePort, &tmp);
	if (!ConnectToHost(&tmp, NULL)) {
		FAIL1("Unable to contact %s\n", HostCImage(&tmp));
	}
	SocketInUse(tmp.sd);
	*whichCookie = tmp;

	return 1;
}


char *
NWSAPI_NwsAttributeName_r(NWSAPI_NwsAttribute attribute) {
	return NwsAttributeName_r(attribute);
}


char *
NWSAPI_NwsAttributeValue_r(NWSAPI_NwsAttribute attribute) {
	return NwsAttributeValue_r(attribute);
}


/* DEPRECATED */
const char *
NWSAPI_NwsAttributeName(NWSAPI_NwsAttribute attribute) {
	ERROR("NwsAttributeName has been deprecated: use the _r version\n");
	return NwsAttributeName(attribute);
}
const char *
NWSAPI_NwsAttributeValue(NWSAPI_NwsAttribute attribute) {
	ERROR("NwsAttributeValue has been deprecated: use the _r version\n");
	return NwsAttributeValue(attribute);
}
const char *
NWSAPI_EnvironmentValue(const char *name,
                        const char *defaultValue) {
	static char *ret = NULL;

	ERROR("EnvironmentValue has been deprecated: use the _r version\n");
	if (ret != NULL) {
		FREE(ret);
	}
	ret = GetEnvironmentValue(name, "~", ".nwsrc", defaultValue);

	return ret;
}
/* end deprecated */

char *
NWSAPI_AutoFetchError() {
	char *ret;
	int i;
	Socket s;

	/* first of all let's check that all the sockets we have here are
	 * good to go */
	GetNWSLock(&lock);
	for (i = 0; i < autoFetchCount; i++) {
		if (!IsOkay(autoFetches[i].memory.sd)) {
			s = autoFetches[i].memory.sd;
			WARN1("AutoFetchError: lost contact with memory %s\n", autoFetches[i].memory.name);
			ReleaseNWSLock(&lock);
			AddToLostFromSocket(s);
			AutoFetchDisconnect(s);
			GetNWSLock(&lock);

			/* let's recheck this index */
			i--;
		}
	}

	if (lostSeries == NULL) {
		ret = NULL;
	} else {
		ret = GenerateList(lostSeries);
		while (lostSeries->howMany > 0) {
			DeleteRegistration(lostSeries, 0);
		}
	}
	ReleaseNWSLock(&lock);

	return ret;
}

int
NWSAPI_AutoFetchAlready(const char *seriesName) {
	int i, ret;
	
	/* sanity check */
	if (seriesName == NULL) {
		FAIL("AutoFetchAlready: NULL parameter!\n");
	}

	/* easy way out */
	if (seriesName[0] == '\0') {
		return 0;
	}

	GetNWSLock(&lock);
	/* let's see if we already have this series */
	for(i = 0; i < autoFetchCount; i++) {
		if (SearchForName(autoFetches[i].series, seriesName, 1, &ret)) {
			break;
		}
	}
	if (i < autoFetchCount) {
		ret = 1;
	} else {
		ret = 0;
	}
	ReleaseNWSLock(&lock);

	return ret;
}

/* temporary structure used to keep track of which memory has which
 * series. The 2 following functions (AddMemorySeries & StartFetch) are
 * local to the AutoFetchBegin* functions.*/
typedef struct {
	struct host_cookie memory;
	char *toFetch;
} memorySeries;
static int
AddMemorySeries(	memorySeries **l,
			int *howMany,
			struct host_cookie *memory) {
	int i;
	memorySeries *list;

	list = *l;

	/* See whether we're already have this memory for other series. */
	for(i = 0; i < *howMany; i++) {
		if (SameHost(memory, &list[i].memory)) {
			break;
		}
	}
	if (i >= *howMany) {
		/* nope it's a new memory */
		i = *howMany;
		(*howMany)++;

		/* allocate space to save the memory */
		list = REALLOC(list, sizeof(memorySeries)*(*howMany));
		if (list == NULL) {
			return -1;
		}
		list[i].toFetch = strdup("");
		if (list[i].toFetch == NULL) {
			return -1;
		}

		list[i].memory = *memory;
		*l = list;
	}

	return i;
}
static int
StartFetch(	memorySeries *list,
		int howMany) {
	int i, j, len, ret;
	char *listToFetch, *c;
	Socket sock;

	ret = 0;

	if (howMany <= 0) {
		/* I guess we are done! */
		return 1;
	}
	for (i = 0; i < howMany; i++) {
		sock = NO_SOCKET;

		GetNWSLock(&lock);
		/* See whether we're already asking this memory for other
		 * series. */
		for(j = 0; j < autoFetchCount; j++) {
			if (SameHost(&list[i].memory, &autoFetches[j].memory)) {
				break;
			}
		}
		if (j >= autoFetchCount) {
			/* this is the first series for this memory */
			listToFetch = strdup(list[i].toFetch);
		} else {
			/* An existing memory.  Because we're receiving
			 * messages asynchronously, we can't reliably
			 * reuse the connection -- we might mistake the
			 * ack for a series coming in or vice versa.
			 * Instead, get rid of this connection and open a
			 * new one with the extended list.  */
			c = GenerateList(autoFetches[i].series);
			len = strlen(c) + strlen(list[i].toFetch) + 2;
			listToFetch = (char *)MALLOC(len);
			if (listToFetch) {
				len = strlen(c);
				memcpy(listToFetch, c, len);
				memcpy(listToFetch + len, list[i].toFetch, strlen(list[i].toFetch));
				sock = autoFetches[j].memory.sd;
			}
			FREE(c);
		}

		/* let's restart the autofecthing for this memory */
		ReleaseNWSLock(&lock);
		if (listToFetch == NULL) {
			ERROR("StartFetch out of memory\n");
			break;
		}
		if (sock != NO_SOCKET) {
			AutoFetchDisconnect(sock);
		}

		if (AutoFetchConnect(&list[i].memory, listToFetch)) {
			ret = 1;
		} else {
			AddToLost(listToFetch);
		}
		FREE(listToFetch);
	}

	/* let's free memory */
	for (i = 0; i < howMany; i++) {
		FREE(list[i].toFetch);
	}
	FREE(list);

	return ret;
}

int
NWSAPI_AutoFetchBegin(const char *seriesName) {
	int i, len;
	struct host_cookie memory;
	char tmpSeries[255];
	const char *ind;
	memorySeries *list;
	int howMany;

	/* sanity check */
	if (seriesName == NULL) {
		FAIL("AutoFetchBegin: NULL parameter\n");
	}

	/* let's loop through the series */
	list = NULL;
	for (howMany = 0, ind = seriesName; GETWORD(tmpSeries, ind, &ind); ) {
		/* let's see if we already checked this series */
		if (NWSAPI_AutoFetchAlready(tmpSeries)) {
			continue;
		}

		/* let's see if we can find the memory responsible */
		if (!GetSeriesMemory(tmpSeries, &memory, -1)) {
			WARN("AutoFetchBegin: unable to determine memory\n");
			AddToLost(tmpSeries);
			continue;
		}

		/* let's get the right index for this memory */
		i = AddMemorySeries(&list, &howMany, &memory);
		if (i == -1 ) {
			ABORT("AutoFetchBegin: out of memory\n");
			return 0;
		}

		/* now let's add the series name to the to be fetch list */
		len = strlen(tmpSeries) + strlen(list[i].toFetch) + 2;
		list[i].toFetch = REALLOC(list[i].toFetch, len);
		if (list[i].toFetch == NULL) {
			ERROR("AutoFetchBegin: out of memory\n");
			AddToLost(seriesName);
			for (i = 0; i < howMany; i++) {
				FREE(list[i].toFetch);
			}
			FREE(list);

			return 0;
		}
		strcat(list[i].toFetch, "\t");
		strcat(list[i].toFetch, tmpSeries);
	}

	/* we got all the memories/series coupled: time to fetch */
	return StartFetch(list, howMany);
}


int
NWSAPI_AutoFetchBeginObject(ObjectSet seriesSet) {
	int i, len;
	char *name;
	struct host_cookie memory;
	memorySeries *list;
	int howMany;
	Object obj;

	/* sanity check */
	if (seriesSet == NULL) {
		FAIL("AutoFetchBeginObject: NULL parameter\n");
	}

	/* let's loop through the series */
	list = NULL;
	obj = NWSAPI_FirstObject(seriesSet);
	for (howMany = 0; obj != NULL; obj = NextObject(seriesSet, obj)) {
		/* now we can get the name and the memory */
		name = NwsAttributeValue_r(FindNwsAttribute(obj, "memory"));
		if (name == NULL) {
			ERROR("AutoFetchBeginObject: series with no memory!\n");
			continue;
		}
		Host2Cookie(name, DefaultHostPort(NAME_SERVER_HOST), &memory);
		FREE(name);
		name = NwsAttributeValue_r(FindNwsAttribute(obj, "name"));
		if (name == NULL) {
			ERROR("AutoFetchBeginObject: series with no name!\n");
			continue;
		}

		/* let's see if we already checked this series */
		if (NWSAPI_AutoFetchAlready(name)) {
			FREE(name);
			continue;
		}

		/* let's get the right index for this memory */
		i = AddMemorySeries(&list, &howMany, &memory);
		if (i == -1 ) {
			ABORT("AutoFetchBegin: out of memory\n");
			return 0;
		}

		/* now let's add the series name to the to be fetch list */
		len = strlen(name) + strlen(list[i].toFetch) + 2;
		list[i].toFetch = REALLOC(list[i].toFetch, len);
		if (list[i].toFetch == NULL) {
			ERROR("AutoFetchBeginObject: out of memory\n");
			AddToLost(name);
			FREE(name);
			for (i = 0; i < howMany; i++) {
				FREE(list[i].toFetch);
			}
			FREE(list);

			return 0;
		}
		strcat(list[i].toFetch, "\t");
		strcat(list[i].toFetch, name);
		FREE(name);
	}

	/* we got all the memories/series coupled: time to fetch */
	return StartFetch(list, howMany);
}

int
NWSAPI_AutoFetchMessage(	int sock,
				char *seriesName,
				unsigned int nameLen,
				NWSAPI_Measurement *meas) {
	char *autoFetchContents;
	struct nws_memory_state s;
	const char *nextWord;
	DataDescriptor des = SIMPLE_DATA(CHAR_TYPE, 0);
	char measurement[127 + 1];
	char timeStamp[127 + 1];
	Socket sender;

	nextWord = autoFetchContents = NULL;

	/* sanity check */
	sender = (Socket)sock;
	if (sender == NO_SOCKET) {
		FAIL("AutoFetchMessage: no valid socket\n");
	}

	/* let's get the state */
	if (!RecvData(sender, &s, stateDescriptor, stateDescriptorLength, -1)) {
		AddToLostFromSocket(sender);
		AutoFetchDisconnect(sender);
		FAIL("AutoFetchMessage: failed to receive state\n");
	}

	/* get room for the data */
	des.repetitions = s.rec_size * s.rec_count;
	autoFetchContents = (char *)MALLOC(des.repetitions + 1);
	if (autoFetchContents == NULL) {
		AddToLostFromSocket(sender);
		AutoFetchDisconnect(sender);
		FAIL("AutoFetchMessage: out of memory\n");
	}

	/* let's get the message */
	if (!RecvData(sender, autoFetchContents, &des, 1, -1)) {
		AddToLostFromSocket(sender);
		AutoFetchDisconnect(sender);
		FREE(autoFetchContents);
		FAIL("AutoFetchMessage: failed to receive message\n");
	}

	/* let's parse it */
	if (GETWORD(timeStamp, autoFetchContents, &nextWord) && GETWORD(measurement, nextWord, &nextWord)) {
		/* let's return the values and series name */
		zstrncpy(seriesName, s.id, nameLen);
		meas->timeStamp = strtod(timeStamp, NULL);
		meas->measurement = strtod(measurement, NULL);
	} else {
		AddToLostFromSocket(sender);
		AutoFetchDisconnect(sender);
		FREE(autoFetchContents);
		FAIL("AutoFetchMessage: failed to parse message\n");
	}
	FREE(autoFetchContents);

	return 1;
}


int
NWSAPI_AutoFetchCheck(char *seriesName,
                      unsigned int nameLen,
                      NWSAPI_Measurement *seriesMeasurement,
                      long timeOut) {
	MessageHeader header;
	Socket sender;
	int i;

	/* sanity check */
	if (seriesName == NULL || seriesMeasurement == NULL || nameLen <= 0) {
		FAIL("AutoFetchCheck: wrong parameter!\n");
	}
	seriesName[0] = '\0';

	 /* let's wait for a message: a message contains only 1 series
	  * name plus 1 measurements */
	if (!IncomingRequest(timeOut, &sender)) {
		/* nope, nothing for us */
		return 0;
	}

	/* let's get the header */
	if (!RecvHeader(sender, &header, -1) ||
			header.message != STATE_FETCHED) {
		char *name;
		
		AddToLostFromSocket(sender);
		AutoFetchDisconnect(sender);

		name = PeerName_r(sender);
		ERROR1("AutoFetchCheck: failed to receive from %s\n", name);
		FREE(name);

		return 0;
	}

	/* let's get the auto fetch data */
	i = NWSAPI_AutoFetchMessage(sender, seriesName, nameLen, seriesMeasurement);

	/* done with this socket */
	SocketIsAvailable(sender);

	return i;
}


void
NWSAPI_AutoFetchEnd(const char *seriesName) {
	int i, len, j;
	struct host_cookie memory;
	char *tmp, name[255];
	const char *ind;
	Socket sock;

	/* sanity check */
	if (seriesName == NULL) {
		ERROR("NWSAPI_AutoFetchEnd: NULL parameter\n");
		return;
	}

	/* we can have multiple series: let's get them one by one */
	for (ind = seriesName; GETWORD(name, ind, &ind); ) {
		/* if we don't have it, we don't need to stop it */
		if (!NWSAPI_AutoFetchAlready(name)) {
			continue;
		}

		len = strlen(name);

		/* let's look for the series */
		GetNWSLock(&lock);
		for(sock = NO_SOCKET, tmp = NULL, i = 0; i < autoFetchCount; i++) {
			/* let's look for the series */
			if (!SearchForName(autoFetches[i].series, name, 1, &j)) {
				/* not here */
				continue;
			}

			/* let's remmeber the memory to restart */
			SAFESTRCPY(memory.name, autoFetches[i].memory.name);
			memory.port = autoFetches[i].memory.port;
			sock = autoFetches[i].memory.sd;

			/* let's remove it: if there is only one series
			 * we remove the whole memory. */
			if (autoFetches[i].series->howMany > 1) {
				DeleteRegistration(autoFetches[i].series, j);
				tmp = GenerateList(autoFetches[i].series);
			}

			/* since we found the series is time to reset the
			 * autofetching with the memory */
			break;
		}
		ReleaseNWSLock(&lock);

		if (sock != NO_SOCKET) {
			/* See comment in AutoFetchBegin() as to why we
			 * can't reuse the connection. */
			AutoFetchDisconnect(sock);
			memory.sd = NO_SOCKET;
			if (tmp != NULL && strlen(tmp) > 0) {
				if (!AutoFetchConnect(&memory, tmp)) {
					/* got problem: add the series to the
					 * orphaned series */
					AddToLost(tmp);
				}
			}
		}
		FREE(tmp);
	}

	/* we remove the series from the lostSeries no matter what: we
	 * don't care about it anymore */
	RemoveFromLost(seriesName);
}


char *
NWSAPI_EnvironmentValue_r(const char *name,
                        const char *defaultValue) {
	return GetEnvironmentValue(name, "~", ".nwsrc", defaultValue);
}

unsigned short
NWSAPI_GetDefaultPort(unsigned int hostType) {
	return DefaultHostPort(hostType);
}


int
NWSAPI_HaltActivity(	const NWSAPI_HostSpec *whichSensor,
			const char *activityName) {
	DataDescriptor activityDescriptor = SIMPLE_DATA(CHAR_TYPE, 0);
	size_t ignored;
	int returnValue;
	struct host_cookie tmp;

	/* sanity check */
	if (whichSensor == NULL || activityName == NULL) {
		FAIL("HaltActivity: NULL parameter(s)\n");
	}

	Host2Cookie(whichSensor->machineName, whichSensor->machinePort, &tmp);
	if (!ConnectToHost(&tmp, NULL)) {
		FAIL1("HaltActivity: unable to contact %s\n", HostCImage(&tmp));
	}

	activityDescriptor.repetitions = strlen(activityName) + 1;
	returnValue = SendMessageAndData(tmp.sd,
                                   ACTIVITY_STOP,
                                   activityName,
                                   &activityDescriptor,
                                   1,
                                   -1) &&
		RecvMessage(tmp.sd,
                            ACTIVITY_STOPPED,
                            &ignored,
                            -1);
	DisconnectHost(&tmp);

	return returnValue;
}


int
NWSAPI_AddObject(	NWSAPI_ObjectSet *toSet,
			const NWSAPI_Object obj) {
	return AddObject(toSet, obj);
}

NWSAPI_ObjectSet
NWSAPI_NewObjectSet() {
	return NewObjectSet();
}


void
NWSAPI_FreeObjectSet(NWSAPI_ObjectSet *toBeFreed) {
	FreeObjectSet(toBeFreed);
}



int
NWSAPI_GetHostsTimeout(	const char *filter,
			NWSAPI_HostSpec *whereTo,
			unsigned int atMost,
			unsigned int *numberReturned,
			int timeout) {
	char *c, *name;
	char *hostFilter;
	int i, port;
	struct host_cookie host;
	NWSAPI_Object    hostObject;
	NWSAPI_ObjectSet hostObjects;

	/* sanity check */
	if (filter == NULL || whereTo == NULL || numberReturned == NULL) {
		FAIL("GetHosts: NULL parameter(s)!\n");
	}

	hostFilter = (char *)MALLOC(strlen(filter) + strlen(NWSAPI_HOSTS) + 6);
	if (hostFilter == NULL) {
		FAIL("GetHosts: out of memory\n");
	}
	sprintf(hostFilter, "(&(%s)%s)", filter, NWSAPI_HOSTS);
	if (!NWSAPI_GetObjectsTimeout(hostFilter, &hostObjects, timeout)) {
		FREE(hostFilter);
		FAIL("GetHosts: object retrieval failed\n");
	}
	FREE(hostFilter);

	i = 0;
	NWSAPI_ForEachObject(hostObjects, hostObject) {
		if (i >= atMost) {
			break;
		}

		port = 0;
		name = NwsAttributeValue_r(FindNwsAttribute(hostObject, NWSAPI_PORT_ATTR));
		if (name == NULL) {
			WARN("NWSAPI_GetHosts: inconsistent host object\n");
			continue;
		}
		port = (int)strtol(name, NULL, 10);
		FREE(name);
		name = NwsAttributeValue_r(FindNwsAttribute(hostObject, NWSAPI_IP_ATTR));
		if (name == NULL) {
			WARN("NWSAPI_GetHosts: inconsistent host object\n");
			continue;
		}
		/* TBD: We really should iterate through the IP addresses
		 * here looking for one we can connect to.  Instead, we
		 * just grab the first one.  */
		if((c = strchr(name, ',')) != NULL) {
			*c = '\0';
		}

		/* let's normalize the names */
		Host2Cookie(name, port, &host);
		FREE(name);
		SAFESTRCPY(whereTo[i].machineName, host.name);
		whereTo[i].machinePort = host.port;
		i++;
	}
	NWSAPI_FreeObjectSet(&hostObjects);
  
	if (numberReturned != NULL) {
		*numberReturned = i;
	}

	return 1;
}


int
NWSAPI_GetMeasurements(	const char *seriesName,
			double sinceWhen,
			NWSAPI_Measurement *whereTo,
			unsigned int atMost,
			unsigned int *numberReturned) {
	int ret;
	double lastTimeStamp;
	struct host_cookie memory;
	size_t returnCount;

	/* sanity check */
	if (seriesName == NULL || whereTo == NULL) {
		FAIL("GetMeasurements: NULL parameter(s)\n");
	}
	if (strlen(seriesName) <= 1) {
		FAIL("GetMeasurements: empty seriesName\n");
	}

	/* we try first to use the default memory we have set: if it
	 * doesn't work we go and ask the nameserver */
	returnCount = ret = 0;
	if (defaultMemory.port != 0) {
		if(LoadExperiments(&defaultMemory,
				seriesName,
				whereTo,
				atMost,
				sinceWhen,
				&returnCount,
				&lastTimeStamp,
				-1)) {
			ret = 1;		/* success */
		}
	}

	/* if we didn't get it from the deafult memory, let's go and ask
	 * the nameserver */
	if (ret != 1 && GetSeriesMemory(seriesName, &memory, -1)) {
		if (LoadExperiments(&memory,
				seriesName,
				whereTo,
				atMost,
				sinceWhen,
				&returnCount,
				&lastTimeStamp,
				-1)) {
			ret = 1; 		/* success */
		}
		DisconnectHost(&memory);
	}

	if(numberReturned != NULL) {
		*numberReturned = returnCount;
	}

	/* if we didnt' get result let's tell something */
	if (ret != 1) {
		ERROR1("GetMeasurement: couldn't get experiment for %s\n", seriesName);
	}

	return ret;
}

int
NWSAPI_GetForecasts(	const char *name,
			double sinceWhen,
			NWSAPI_ForecastCollection *whereTo,
			unsigned int atMost,
			unsigned int *num) {
	NWSAPI_Measurement *meas;
	NWSAPI_ForecastState *state;
	unsigned int len;

	/* sanity check */
	if (name == NULL || whereTo == NULL || num == NULL) {
		FAIL("GetForecasts: NULL parameter(s)\n");
	}

	/* let's create room for the measurements */
	meas = (NWSAPI_Measurement *)MALLOC((sizeof(NWSAPI_Measurement) * atMost));
	if (meas == NULL) {
		FAIL("GetForecasts: out of memory\n");
	}

	/* let's compute the forecasts */
	state = NWSAPI_NewForecastState();
	if (state == NULL) {
		FAIL("GetForecasts: out of memory\n");
	}

	/* let's retrieve them */
	if (!NWSAPI_GetMeasurements(name, sinceWhen, meas, atMost, &len)) {
		NWSAPI_FreeForecastState(&state);
		FAIL("GetForecasts: failed to retrieve measurements.\n");
	}


	NWSAPI_UpdateForecastState(state, meas, len, whereTo, len);
	NWSAPI_FreeForecastState(&state);
	*num = len;

	return 1;
}

int
NWSAPI_GetNameServerTimeout(	const NWSAPI_HostSpec *whoToAsk,
				NWSAPI_HostSpec *NS,
				int timeout) {
	HostInfo hostInfo;
	NsInfo nsInfo;
	struct host_cookie tmp;
	int ret;

	/* sanity check */
	if (whoToAsk == NULL || NS == NULL) {
		FAIL("GetNameServer NULL parameter(s)!\n");
	}

	/* Basic approach: send an HOST_GET_NS or HOST_TEST message to
	 * whoToAsk to find out what name server it's using. */
	MakeHostCookie(whoToAsk->machineName, whoToAsk->machinePort, &tmp);
	tmp.sd = NO_SOCKET;

	/* First, try sending a HOST_GET_NS message */
	ret = 0;
	if (HostNameServerInfo(&tmp, &nsInfo, timeout)) {
		if (nsInfo.nsType == NWS_NS) {
			*NS = *NWSAPI_MakeHostSpec(nsInfo.nameServer, 0);
			ret = 1;
		} else {
			ERROR1("GetNameServer: unknown name server type %i\n", nsInfo.nsType);
		}
	} else {
		/* HOST_GET_NS failed; try HOST_TEST */
		if (!GetHostInfo(&tmp, &hostInfo, timeout)) {
			FAIL("GetNameServer: failed to get HOST_TEST\n");
		}
		*NS = *NWSAPI_MakeHostSpec(hostInfo.nameServer, 0);
		ret = 1;
	}
	DisconnectHost(&tmp);

	return ret;
}

int
NWSAPI_GetMemoryTimeout(	const NWSAPI_HostSpec *whoToAsk,
				NWSAPI_HostSpec *memory,
				int timeout) {
	NsInfo nsInfo;
	struct host_cookie tmp;
	int ret;

	/* sanity check */
	if (whoToAsk == NULL || memory == NULL) {
		FAIL("GetMemory: NULL parameter(s)!\n");
	}

	MakeHostCookie(whoToAsk->machineName, whoToAsk->machinePort, &tmp);
	tmp.sd = NO_SOCKET;

	/* try sending a HOST_GET_MEMORY message */
	ret = 0;
	if (HostMemoryInfo(&tmp, &nsInfo, timeout)) {
		if (nsInfo.nsType == NWS_MEMORY) {
			*memory = *NWSAPI_MakeHostSpec(nsInfo.nameServer, 0);
			ret = 1;
		} else {
			ERROR1("GetMemory: unknown memory type %i\n", nsInfo.nsType);
		}
	} else {
		ERROR("GetMemory: couldn't get memory, perhaps old sensor?\n");
	}
	DisconnectHost(&tmp);

	return ret;
}

int
NWSAPI_GetObjectsTimeout(	const char *filter,
				NWSAPI_ObjectSet *whereTo,
				int timeout) {
	int i, ret, len;
	ObjectSet tmp, returnedValue;
	
	/* sanity check */
	if (filter == NULL || whereTo == NULL) {
		FAIL("GetObjects: NULL parameter(s)\n");
	}

	/* let's go through all the nameservers to find what we need */
	ret = 0;

	GetNWSLock(&lock);
	/* let's check if we have NS to talk to */
	if (howManyNS <= 0) {
		WARN("GetObjects: no NameServer has been set!\n");
	}

	/* let's go for it */
	returnedValue = NewObjectSet();
	for (i = 0; i < howManyNS; i++) {
		tmp = NewObjectSet();
		if (!RetrieveObjects(&NSes[i], filter, &tmp, timeout)) {
			ERROR2("GetObjects: unable to retrieve objects from %s (filter '%s')\n", HostCImage(&NSes[i]), filter);
		} else {
			/* we got at least something back */
			ret = 1;

			/* if we got an empty object, let's move on */
			if (strlen(tmp) <= OBJECT_TERMINATOR_LEN) {
				FREE(tmp);
				continue;
			}

			len = strlen(returnedValue);
			returnedValue = REALLOC(returnedValue, len + strlen(tmp) + 1);
			if (returnedValue == NULL) {
				ABORT("GetObjects: out of memory\n");
			}
			/* copy the terminating char too */
			memcpy(returnedValue + len, tmp, strlen(tmp) + 1);
		}
		FREE(tmp);
	}
	ReleaseNWSLock(&lock);

	if (ret == 1) {
		*whereTo = returnedValue;
	} else {
		FREE(returnedValue);
	}

	return ret;
}

int
NWSAPI_IsResourceValid(const char *name) {
	MeasuredResources r;
	int len;

	/* sanity check */
	if (name == NULL) {
		ERROR("IsResourceValid: NULL parameter\n");
		return -1;
	}

	/* ResourceName's return value can be overwritten */
	len = strlen(name);
	GetNWSLock(&lock);
	for(r = 0; r < RESOURCE_COUNT; r++) {
		if (strncmp(ResourceName(r), name, len) == 0){
			/* found it */
			break;
		}
	}
	ReleaseNWSLock(&lock);
	if (r >= RESOURCE_COUNT) {
		ERROR1("IsResourceValid: unknown resource (%s)\n", name);
		return -1;
	}

	return (int) r;
}


int
NWSAPI_IntermachineResource(const char *name) {
	int i, j, len;
	const MeasuredResources *res;

	/* let's get the resource index */
	i = NWSAPI_IsResourceValid(name);
	if (i < 0) {
		FAIL1("IntermachineResource: unknown resource (%s)\n", name);
	}

	/* let's look for the responsible skill */
	for (j = 0; j < SKILL_COUNT; j++) {
		if (!SkillResources((KnownSkills) j, &res, &len)) {
			LOG("IntermachineResource: unknown skill!\n");
			continue;
		} 

		for (; len > 0; len--) {
			if (res[len - 1] != (MeasuredResources) i) {
				/* it's not the right resouce */
				continue;
			}

			/* does the skill do clique measurments? */
			return SkillAvailableForControl((KnownSkills) j, "", CLIQUE_CONTROL);
		}
	}
	/* we shouldn't get here! */
	ERROR("IntermachineResource: run out of skills!\n");

	return 0;
}

const NWSAPI_SeriesSpec *
NWSAPI_MakeSeriesSpec(const char *sourceMachine,
                      const char *destinationMachine,
                      const char *resourceName) {
	static NWSAPI_SeriesSpec returnValue;

	SAFESTRCPY(returnValue.sourceMachine, sourceMachine);
	SAFESTRCPY(returnValue.destinationMachine,
		(destinationMachine == NULL) ? "" : destinationMachine);
	SAFESTRCPY(returnValue.resourceName, resourceName);

	return &returnValue;
}


const NWSAPI_HostSpec *
NWSAPI_MakeHostSpec(const char *host,
                    NWSAPI_Port machinePort) {
	struct host_cookie hostD;
	static NWSAPI_HostSpec returnValue;

	Host2Cookie(host, machinePort, &hostD);
	SAFESTRCPY(returnValue.machineName, hostD.name);
	returnValue.machinePort = hostD.port;

	return &returnValue;
}


NWSAPI_NwsAttribute
NWSAPI_NextNwsAttribute(NWSAPI_Object object,
                     NWSAPI_NwsAttribute current) {
	return NextNwsAttribute(object, (current == NULL) ? NO_ATTRIBUTE : current);
}

NWSAPI_NwsAttribute
NWSAPI_FindNwsAttribute(	const NWSAPI_Object object,
				const char *name) {
	return FindNwsAttribute(object, name);
}


NWSAPI_Object
NWSAPI_NextObject(NWSAPI_ObjectSet objects,
                  NWSAPI_Object current) {
	return NextObject(objects, (current == NULL) ? NO_OBJECT : current);
}


#define KEEP_A_LONG_TIME 315360000.0
int
NWSAPI_PutMeasurements(	const char *seriesName,
			const NWSAPI_SeriesSpec *whichSeries,
			const NWSAPI_Measurement *measurements,
			unsigned int howMany) {
	struct host_cookie localMemory;
	NWSAPI_HostSpec memorySpec;
	struct host_cookie *memoryToUse;
	Object toRegister;
	unsigned int i;
	int ret;
	char *options;

	/* sanity check */
	if (seriesName == NULL || whichSeries == NULL || measurements == NULL) {
		FAIL("PutMeasurements: NULL parameters!\n");
	}

	/* let's try to talk the memory if we have it ... */
	if ((defaultMemory.port != 0) && (defaultMemory.name[0] != '\0')) {
		memoryToUse = &defaultMemory;
	} else {
		/* let's find a memory from a nameserver */
		if (!NWSAPI_GetHosts("hostType=memory", &memorySpec, 1, &i)) {
			i = 0;
		}
		if (i <= 0) {
			FAIL("PutMeasurements: couldn't get a memory!\n");
		}
		SAFESTRCPY(localMemory.name, memorySpec.machineName);
		localMemory.port = memorySpec.machinePort;
		memoryToUse = &localMemory;
	}

	/* let's try to talk to the memory */
	if (!ConnectToHost(memoryToUse, NULL)) {
		FAIL("PutMeasurements: couldn't talk to a memory!\n");
	}

	/* let's generate the series, and the registration */
	options = ResourceOptions(whichSeries);
	toRegister = CreateSeriesObject(seriesName, 
			whichSeries->sourceMachine,
			NULL,
			"noname",
			options,
			whichSeries->resourceName,
			NULL);
	if (toRegister == NULL) {
		FreeObject(&toRegister);
		DisconnectHost(memoryToUse);
		FAIL("PutMeasurements: out of memory\n");
	}

	/* and let's store it. The memory will take care or registering
	 * the series. */
	ret = StoreNewExperiments(	memoryToUse,
					toRegister,
					measurements,
					howMany,
					KEEP_A_LONG_TIME);
	FreeObject(&toRegister);

	return ret;
}


#define MAX_OPTIONS 64
int
NWSAPI_StartActivity(	const NWSAPI_HostSpec *whichSensor,
			const char *activityName,
			const char *controlName,
			const char *skillName,
			const char **options,
			unsigned int howManyOptions) {
	DataDescriptor activityDescriptor = SIMPLE_DATA(CHAR_TYPE, 0);
	char *activitySpec;
	NWSAPI_NwsAttribute controlAttr;
	char controlFilter[127 + 1];
	NWSAPI_Object controlObject;
	NWSAPI_ObjectSet controlObjects;
	int i, j, valueCount, returnValue;
	size_t ignored;
	const char *nameEnd;
	char optionNames[255 + 1];
	OptionInfo optInfo[MAX_OPTIONS];
	unsigned int optInfoCount;
	char portImage[MAX_PORT_IMAGE + 1];
	char sensorName[MAX_MACHINE_NAME + MAX_PORT_IMAGE + 1];
	struct host_cookie sensorLink;
	NWSAPI_NwsAttribute skillAttr;
	char skillFilter[127 + 1];
	NWSAPI_Object skillObject;
	NWSAPI_ObjectSet skillObjects;

	/* Retrieve control and skill registrations from the name server. */
	sprintf(portImage, "%d", whichSensor->machinePort);
	vstrncpy(sensorName, sizeof(sensorName), 3,
		whichSensor->machineName, ":", portImage);
	vstrncpy(controlFilter, sizeof(controlFilter), 12,
		"(&", NWSAPI_CONTROLS,
		"(", NWSAPI_HOST_ATTR, "=", sensorName, ")",
		"(", NWSAPI_CONTROL_ATTR, "=", controlName, "))");
	vstrncpy(skillFilter, sizeof(skillFilter), 12,
		"(&", NWSAPI_SKILLS,
		"(", NWSAPI_HOST_ATTR, "=", sensorName, ")",
		"(", NWSAPI_SKILL_ATTR, "=", skillName, "))");

	if(!NWSAPI_GetObjects(controlFilter, &controlObjects)) {
		FAIL("StartActivity: unable to retrieve sensor control information\n");
	} else if((controlObject = NWSAPI_FirstObject(controlObjects)) == NULL) {
		NWSAPI_FreeObjectSet(&controlObjects);
		FAIL2("StartActivity: no control named %s is registered by %s\n", controlName, sensorName);
	} if(!NWSAPI_GetObjects(skillFilter, &skillObjects)) {
		NWSAPI_FreeObjectSet(&controlObjects);
		FAIL("StartActivity: unable to retrieve sensor skill information\n");
	} else if((skillObject = NWSAPI_FirstObject(skillObjects)) == NULL) {
		NWSAPI_FreeObjectSet(&controlObjects);
		NWSAPI_FreeObjectSet(&skillObjects);
		FAIL2("StartActivity: no skill named %s is registered by %s\n", skillName, sensorName);
	}

	/* Parse each skill option attribute value into an element of
	 * optInfo. */
	controlAttr = FindNwsAttribute(controlObject, NWSAPI_OPTION_ATTR);
	SAFESTRCPY(optionNames, (controlAttr == NULL) ? "" : NwsAttributeValue(controlAttr));
	skillAttr = FindNwsAttribute(skillObject, NWSAPI_OPTION_ATTR);
	if((skillAttr != NULL) && (*NwsAttributeValue(skillAttr) != '\0')) {
		if(optionNames[0] != '\0')
			strcat(optionNames, ",");
		strcat(optionNames, NwsAttributeValue(skillAttr));
	}
	optInfoCount = 0;
	NWSAPI_ForEachNwsAttribute(controlObject, controlAttr) {
		if(ParseOption(controlAttr, optionNames, &optInfo[optInfoCount])) {
			optInfoCount++;
		}
	}
	NWSAPI_ForEachNwsAttribute(skillObject, skillAttr) {
		if(ParseOption(skillAttr, optionNames, &optInfo[optInfoCount])) {
			optInfoCount++;
		}
	}
	NWSAPI_FreeObjectSet(&controlObjects);
	NWSAPI_FreeObjectSet(&skillObjects);

	/* Make sure all names in #options# are valid option names... */
	for(i = 0; i < howManyOptions; i++) {
		nameEnd = strchr(options[i], ':');
		if(nameEnd == NULL) {
			FAIL1("StartActivity: bad option format %s\n", options[i]);
		}
		for(j = 0; j < optInfoCount; j++) {
			if (strncmp(optInfo[j].name, options[i], nameEnd - options[i]) == 0) {
				/* found it */
				break;
			}
		}
		if(j == optInfoCount) {
			FAIL1("StartActivity: unknown option %s\n", options[i]);
		}
	}

	/* ... and that #options# includes a proper number of each option. */
	for(i = 0; i < optInfoCount; i++) {
		valueCount = 0;
		for(j = 0; j < howManyOptions; j++) {
			nameEnd = strchr(options[j], ':');
			if (strncmp(optInfo[i].name, options[j], nameEnd - options[j]) == 0) {
				for(nameEnd++; nameEnd != NULL; nameEnd = strchr(nameEnd + 1, ',')) {
					valueCount++;
				}
			}
		}
		if((valueCount < optInfo[i].minOccurrences) || (valueCount > optInfo[i].maxOccurrences)) {
			FAIL4("StartActivity: %d values given for %s option; %d to %d allowed\n", valueCount, optInfo[i].name, optInfo[i].minOccurrences, optInfo[i].maxOccurrences);
		}
	}

	Host2Cookie(sensorName, DefaultHostPort(SENSOR_HOST), &sensorLink);
	if(!ConnectToHost(&sensorLink, &sensorLink.sd)) {
		FAIL1("StartActivity: unable to contact sensor %s\n", sensorName);
	}

	/* Package the registration name, control name, skill name, and
	 * options into a series of nul-terminated strings to accompany
	 * the start message.
	 */
	activityDescriptor.repetitions = strlen(activityName) + strlen(controlName) + strlen(skillName) + 4;
	for(i = 0; i < howManyOptions; i++) {
		activityDescriptor.repetitions += strlen(options[i]) + 1;
	}
	activitySpec = (char *)MALLOC(activityDescriptor.repetitions);
	if(activitySpec == NULL) {
		DisconnectHost(&sensorLink);
		FAIL("StartActivity: out of memory\n");
	}

	/* Use spaces to reserve space for the nul characters. */
	vstrncpy(activitySpec, activityDescriptor.repetitions, 6,
		activityName, " ", controlName, " ", skillName, " ");
	for(i = 0; i < howManyOptions; i++) {
		strcat(activitySpec, options[i]);
		strcat(activitySpec, "\t");
	}
	activitySpec[strlen(activityName)] = '\0';
	activitySpec[strlen(activityName) + strlen(controlName) + 1] = '\0';
	activitySpec[strlen(activityName) + strlen(controlName) + strlen(skillName) + 2] = '\0';

	/* Finally, send the start message. */
	returnValue = SendMessageAndData(sensorLink.sd,
			ACTIVITY_START,
			activitySpec,
			&activityDescriptor,
			1,
			-1) &&
		RecvMessage(sensorLink.sd,
			ACTIVITY_STARTED,
			&ignored,
			-1);
	DisconnectHost(&sensorLink);
	FREE(activitySpec);

	return returnValue;
}

int
NWSAPI_RestartActivities(const NWSAPI_HostSpec *whichSensor) {

  char portImage[MAX_PORT_IMAGE + 1];
  char sensorName[MAX_MACHINE_NAME + MAX_PORT_IMAGE + 1];
  char controlFilter[127 + 1];
  NWSAPI_NwsAttribute controlAttr;
  NWSAPI_Object controlObject;
  NWSAPI_ObjectSet controlObjectSet;
  char *optionstring,*word;
  int i;
  
  /* args for StartActivity */
  char activityName[127 + 1];
  char controlName[127 + 1];
  char skillName[127 + 1];
  char **options=NULL;
  unsigned int howManyOptions=0;

  /* Retrieve control and skill registrations from the name server. */
  sprintf(portImage, "%d", whichSensor->machinePort);
  vstrncpy(sensorName, sizeof(sensorName), 3,
           whichSensor->machineName, ":", portImage);
  vstrncpy(controlFilter, sizeof(controlFilter), 7,
           "(&", NWSAPI_ACTIVITIES,
          "(", NWSAPI_HOST_ATTR, "=", sensorName, "))");

  LOG1("Restart all activities on sensor %s\n",sensorName);

  if(!NWSAPI_GetObjects(controlFilter, &controlObjectSet)) {
    FAIL("RestartActivities: unable to retrieve activity informations\n");
  }
  else if(!NWSAPI_FirstObject(controlObjectSet)) {
    NWSAPI_FreeObjectSet(&controlObjectSet);
    FAIL1("RestartActivities: no activities is registered by %s\n",
         sensorName);
  }
  NWSAPI_ForEachObject(controlObjectSet, controlObject) {
    activityName[0]='\0';
    controlName[0]='\0';
    for (i=0;i<howManyOptions;i++)
      FREE(options[i]);
    FREE(options); 
    options=NULL;
    howManyOptions = 0;

    NWSAPI_ForEachNwsAttribute(controlObject, controlAttr) {
      if(!strcmp(NWSAPI_NwsAttributeName(controlAttr), NWSAPI_NAME_ATTR)) {
       vstrncpy(activityName,128,1,NWSAPI_NwsAttributeValue(controlAttr));

      } else if (!strcmp(NWSAPI_NwsAttributeName(controlAttr),
                        NWSAPI_CONTROL_ATTR)) {
       vstrncpy(controlName,128,1,NWSAPI_NwsAttributeValue(controlAttr));

      } else if (!strcmp(NWSAPI_NwsAttributeName(controlAttr),
                        NWSAPI_SKILL_ATTR)) {
       vstrncpy(skillName,128,1,NWSAPI_NwsAttributeValue(controlAttr));

      } else if (!strcmp(NWSAPI_NwsAttributeName(controlAttr),
                        NWSAPI_OPTION_ATTR)) {
       optionstring=strdup(NWSAPI_NwsAttributeValue(controlAttr));
       for(word = strtok(optionstring, ",");
           word != NULL;
           word = strtok(NULL, ",")) {
         howManyOptions++;
         if (!(options=REALLOC(options,howManyOptions))
             || !(options[howManyOptions-1]=(char*)MALLOC(sizeof(char)*128))){
            FAIL("Out of memory\n"); 
         }
         snprintf(options[howManyOptions-1],128,word);
       }
       FREE(optionstring);
       
      } else { /* search for an option */
       for (i=0 ; i<howManyOptions ; i++) {
         if (!strcmp(NWSAPI_NwsAttributeName(controlAttr),options[i])) {
           strcat(options[i],":");
           strcat(options[i],NWSAPI_NwsAttributeValue(controlAttr));
         }
       }
      }
    }

    if (!NWSAPI_StartActivity(whichSensor,activityName,controlName,skillName,
                             (const char**)options,howManyOptions)) {
      NWSAPI_FreeObjectSet(&controlObjectSet);
      for (i=0;i<howManyOptions;i++)
       FREE(options[i]);
      FREE(options);
      return 0;
    }
  }

  for (i=0;i<howManyOptions;i++)
    FREE(options[i]);
  FREE(options);
  NWSAPI_FreeObjectSet(&controlObjectSet);
  return 1;
}



char *
NWSAPI_SeriesName_r(const NWSAPI_SeriesSpec *whichSeries) {
	char *options, *tmp;

	options = ResourceOptions(whichSeries);
	if (options == NULL) {
		ERROR("SeriesName_r: bad SeriesSpec\n");
		return NULL;
	}

	tmp = NameOfSeries_r(	whichSeries->sourceMachine, 
				whichSeries->resourceName,
				options);
	FREE(options);

	return (tmp);
}


const char *
NWSAPI_SeriesName(const NWSAPI_SeriesSpec *whichSeries) {
	char *tmp; 
	static char returnValue[127 + 1];

	returnValue[0] = '\0';

	tmp =  NWSAPI_SeriesName_r(whichSeries);
	if (tmp != NULL) {
		SAFESTRCPY(returnValue, tmp);
	}
	FREE(tmp);

	return &returnValue[0];
}


int
NWSAPI_UseMemory(const NWSAPI_HostSpec *whichMemory) {
	return UseHost(whichMemory, &defaultMemory);
}


int
NWSAPI_UseNameServer(const NWSAPI_HostSpec *whichNS) {
	struct host_cookie tmp;
	int i;

	/* if NULL, we need to reset the list of namesevers */
	if (whichNS == NULL || strlen(whichNS->machineName) <= 0) {
		GetNWSLock(&lock);
		/* let's drop the sockets */
		for (i = 0; i < howManyNS; i++) {
			DisconnectHost(&NSes[i]);
		}

		/* and clean up the memory */
		FREE(NSes);
		howManyNS = 0;
		ReleaseNWSLock(&lock);
	} else {
		/* let's add the nameserver to our list */
		if (!UseHost(whichNS, &tmp)) {
			/* it was no good */
			return 0;
		}

		/* we need to lock */
		GetNWSLock(&lock);
		NSes = REALLOC(NSes, sizeof(struct host_cookie) * (howManyNS + 1));
		if (NSes == NULL) {
			ReleaseNWSLock(&lock);
			FAIL("UseNameServer: out of memory\n");
		}
		NSes[howManyNS] = tmp;
		howManyNS++;
		ReleaseNWSLock(&lock);
	}

	return 1;
}

int
NWSAPI_GetAllForecasts(	NWSAPI_HostSpec *proxy,
			char *resource,
			char *opts,
			char **hosts,
			NWSAPI_ForecastCollection **col,
			int timeOut) {
	struct host_cookie tmp;
	char *request;
	int i, howMany;
	NWSAPI_ForecastCollection *c;
	DataDescriptor des = SIMPLE_DATA(CHAR_TYPE, 0);
	DataDescriptor howManyDes = SIMPLE_DATA(INT_TYPE, 1);

	/* sanity check */
	if (proxy == NULL || resource == NULL || hosts == NULL || col == NULL) {
		ERROR("GetAllForecasts: NULL parameter!\n");
		return 0;
	}

	/* let's connect to the proxy */
	MakeHostCookie(proxy->machineName, proxy->machinePort, &tmp);
	if (!ConnectToHost(&tmp, NULL)) {
		FAIL1("GetAllForecasts: failed to connect to %s\n", HostCImage(&tmp));
	}

	/* let's create the request */
	des.repetitions = strlen(resource) + 1;
	if (opts != NULL) {
		des.repetitions += strlen(opts) + 1;
	} else {
		des.repetitions += 1;
	}
	for (i = 0; hosts[i] != NULL; i++) {
		des.repetitions += strlen(hosts[i]) + 1;
	}
	request = MALLOC(sizeof(char) * des.repetitions);
	if (request == NULL) {
		DROP_SOCKET(&tmp.sd);
		FAIL("GetAllForecasts: out of memory\n");
	}
	memcpy(request, resource, strlen(resource) + 1);
	des.repetitions = strlen(resource) + 1;
	if (opts != NULL) {
		memcpy(request + des.repetitions, opts, strlen(opts) + 1);
		des.repetitions += strlen(opts) + 1;
	} else {
		/* empty string */
		memcpy(request + des.repetitions, "\0", 1);
		des.repetitions += 1;
	}
	for (i = 0; hosts[i] != NULL; i++) {
		memcpy(request + des.repetitions, hosts[i], strlen(hosts[i])+1);
		des.repetitions += strlen(hosts[i]) + 1;
	}
	howMany = i--;

	/* let's send the request */
	if (!SendMessageAndData(tmp.sd, GET_FORECASTS, request, &des, 1, timeOut)) {
		FREE(request);
		DROP_SOCKET(&tmp.sd);
		FAIL1("GetAllForecasts: failed to talk %s\n", HostCImage(&tmp));
	}
	FREE(request);

	/* let's get the forecasts */
	if (!RecvMessageAndData(tmp.sd, GOT_FORECASTS, &i, &howManyDes, 1, timeOut)) {
		DROP_SOCKET(&tmp.sd);
		FAIL1("GetAllForecasts: failed to talk %s\n", HostCImage(&tmp));
	}
	if ((howMany*howMany) < i) {
		FAIL("GetAllForecasts: too many forecasts!\n");
	}

	/* allocate space */
	c = (NWSAPI_ForecastCollection *)MALLOC(sizeof(NWSAPI_ForecastCollection)*i);
	if (c == NULL) {
		FAIL("GetAllForecasts: out of memory\n");
	}

	/* we receive the forecastCollection one at a time */
	howMany = i;
	for (i = 0; i < howMany; i++) {
		if (!RecvData(tmp.sd, &c[i], forecastCollectionDescriptor, forecastCollectionDescriptorLength, timeOut)) {
			FREE(c);
			FAIL("GetAllForecasts: failed to receive forecasts\n");
		}
	}
	DROP_SOCKET(&tmp.sd);
	*col = c;

	return howMany;
}

int
NWSAPI_StartFetching(	NWSAPI_HostSpec *proxy,
			char *resource,
			char *opts,
			char **hosts,
			int timeOut) {
	struct host_cookie tmp;
	char *request;
	int i;
	DataDescriptor des = SIMPLE_DATA(CHAR_TYPE, 0);

	/* sanity check */
	if (proxy == NULL || resource == NULL || hosts == NULL) {
		ERROR("StartFetching: NULL parameter!\n");
		return 0;
	}

	/* let's connect to the proxy */
	MakeHostCookie(proxy->machineName, proxy->machinePort, &tmp);
	if (!ConnectToHost(&tmp, NULL)) {
		FAIL1("StartFetching: failed to connect to %s\n", HostCImage(&tmp));
	}

	/* let's create the request */
	des.repetitions = strlen(resource) + 1;
	if (opts != NULL) {
		des.repetitions += strlen(opts) + 1;
	} else {
		des.repetitions += 1;
	}
	for (i = 0; hosts[i] != NULL; i++) {
		des.repetitions += strlen(hosts[i]) + 1;
	}
	request = MALLOC(sizeof(char) * des.repetitions);
	if (request == NULL) {
		DROP_SOCKET(&tmp.sd);
		FAIL("StartFetching: out of memory\n");
	}
	memcpy(request, resource, strlen(resource) + 1);
	des.repetitions = strlen(resource) + 1;
	if (opts != NULL) {
		memcpy(request + des.repetitions, opts, strlen(opts) + 1);
		des.repetitions += strlen(opts) + 1;
	} else {
		/* empty string */
		memcpy(request + des.repetitions, "\0", 1);
		des.repetitions += 1;
	}
	for (i = 0; hosts[i] != NULL; i++) {
		memcpy(request + des.repetitions, hosts[i], strlen(hosts[i])+1);
		des.repetitions += strlen(hosts[i]) + 1;
	}

	/* let's send the request */
	if (!SendMessageAndData(tmp.sd, START_FETCHING, request, &des, 1, timeOut)) {
		FREE(request);
		DROP_SOCKET(&tmp.sd);
		FAIL1("StartFetching: failed to talk %s\n", HostCImage(&tmp));
	}
	FREE(request);

	/* let's get the forecasts */
	if (!RecvMessage(tmp.sd, PROXY_ACK, (size_t *)&i, timeOut)) {
		DROP_SOCKET(&tmp.sd);
		ERROR("StartFetching: failed to receive ack\n");
	}
	DROP_SOCKET(&tmp.sd);

	return 1;
}