To be able to access the NWS API you need to include the nws_api.h header file and compile with -llibnws.a option to include the library.
nws_api.h
NWSAPI_HostSpec
NWSAPI_SeriesSpec
NWSAPI_Measurement
NWSAPI_Forecast
NWSAPI_ForecastCollection
NWSAPI_ForecastState
NWSAPI_ObjectSet
NWSAPI_Object
NWSAPI_ObjectSet
const HostSpec * MakeHostSpec (const char *host, Port machinePort)
const SeriesSpec * MakeSeriesSpec (const char *sourceMachine, const char *destinationMachine, const char *seriesName);
const char * SeriesName (const SeriesSpec *whichSeries);
const char * ForecasterMethodName (unsigned int methodIndex)
ForecatState * NewForecastState ();
void FreeForecastState (ForecastState **state);
void UpdateForecastState (ForecastState *state, const Measurement *measurements, size_t howManyMeasurements, ForecastCollection *forecasts, size_t howManyForecasts);
void UpdateForecast (ForecastState *state, const Measurement *measurements, size_t howManyMeasurements,);
int GetForecasts (const char *seriesName, double sinceWhen, ForecastCollection *whereTo, size_t atMost, size_t *numberReturned);
It stores ths count od retrieved collections in numberedReturned if it is non-NULL. Return 1 if succesfull, else 0.
int GetForecast (const char *seriesName, ForecastCollection *whereTo);
int GetMeasurements (const char *seriesName, double sinceWhen, Measurement *whereTo, size_t atMost, size_t *numberReturned);
Returns the measurements in the atMost ling array whereTo with the newest one in the first element. Stores the count of retrieved measurements in numberReturned if non-NULL. Returns 1 on success, 0 else.
int GetMeasurment (const char *seriesName, Measurement *whereTo);
int UseMemory (const HostSpec *whichMemory);
Returns 1 if succesfull, else 0.
int UseNameServer (const HostSpec *whichNS)
If whichNS is NULL or the empty string, the list is reset.
Returns 1 if succesfull, else 0.
int GetNameServer (const HostSpec *whoToAsk, HostSpec *nameServer);
Returns 1 if succesfull, else 0.
int GetMemory (const HostSpec *whoToAsk, HostSpec *memory);
Returns 1 if succesfull, else 0.
int GetObjects (const char *filter, ObjectSet *whereTo);
Returns the restrieved objects in whereTo. After a succesful call, FreeObjectSet needs to be called on whereTo to free the memory. Returns 1 if succesfull, else 0.
void FreeObjectSet (ObjectSet *toBeFreed);
Objecct NextObject (ObjectSet objects, Object current);
Returns NULL if no more objects are presents.
NwsAttribute NextNwsAttribute (Object object, NwsAttribute current);
Returns NULL if no more attribute are presents.
The following example retrieves a list of hosts registered with the nameserver:
#define NWSAPI_SHORTNAMES
#include "nws_api.h"
HostSpec *NS;
char filter[256];
ObjectSet objs;
Object current;
NwsAttribute attr;
char *tmp, *tmp1;
/* let's instruct the interface to use the right nameserver */
NS = MakeHostSpec("NS.test.edu", 8090);
UseNameServer(&NS);
/* create the right filter */
sprintf(filter, "objectclass=nwsHost");
/* query the NS */
GetObjects(filter, &objs)
/* now we can browse though the objects*/
for (current = NextObject(objs, NULL); current != NULL; current = NextObject(objs, current)) {
/* now we can browse though the attribute*/
for (attr = NextNwsAttribute(current, NULL); attr != NULL; attr = NextNwsAttribute(current, attr)) {
/* do some pretty printing */
tmp = NwsAttributeName_r(attr);
tmp1 = NwsAttributeValue_r(attr);
printf("%s: %s\n", tmp, tmp1);
free(tmp);
free(tmp1);
}
}
Of course one important feature of NWS is to report measurements and forecasts of specific resources. The following example extracts measurements and generate forecasts for a known nwsSeries (where the series name can be found querying the nws_nameserver -- see the previous example):
#define NWSAPI_SHORTNAMES
#include "nws_api.h"
char *series;
SeriesSpec *spec;
int howMany;
HostSpec NS;
/* how many measurement we retrieve */
#define HOW_MANY 20
Measurement meas[HOW_MANY];
ForecastCollection forecasts[HOW_MANY];
ForecastState *state;
/* let's instruct the interface to use the right nameserver */
NS = MakeHostSpec("NS.test.edu", 8090);
UseNameServer(&NS);
/* get the series name: the right way is to query the nameserver to find
* the right name of the series. For simplicity we build one here,
* assuming that there is indeed such a series reigsterd with the
* nameserver */
spec = MakeSeriesSpec("a.edu", "b.edu", "bandwidthTcp");
series = SeriesName(spec);
/* initialize the forecast state */
state = NewForecastState();
/* get the measurements */
if (!GetMeasurements(series, NWSAPI_BEGINNING_OF_TIME, meas, HOW_MANY, &howMany))
{
printf("failed to retrieve measurements\n");
exit(0);
}
/* we have howMany measurements: generate the forecasts now. */
UpdateForecastState(state, meas, howMany, forecasts, howMany);
/* ForecastCollection keeps a copy of the measurements handy : let's
* print measurments and forecasts */
for (; howMany >= 0; howMany--)
{
printf("time %.5f measurement %.2f ", forecasts[howMany].measurement.timeStamp, forecasts[howMany].measurement.measurement)
/* we use only the MAE forecasts here */
pritnf("forecast %.5f with error %.5f method used %s\n", forecasts[howMany].forecasts[NWSAPI_MAE_FORECAST].forecast, forecasts[howMany].forecasts[NWSAPI_MAE_FORECAST].error, MethodName(forecasts[howMany].forecasts[NWSAPI_MAE_FORECAST].methodUsed);
}
/* free the memory associated with the forecast state */
FreeForecastState(&state);
Starting from version 2.11 of NWS, you can call directly GetForecasts without having the need of a forecaster process running. The previous code can then be written as:
#define NWSAPI_SHORTNAMES
#include "nws_api.h"
char *series;
SeriesSpec *spec;
int howMany;
HostSpec NS;
/* how many measurement we retrieve */
#define HOW_MANY 20
ForecastCollection forecasts[HOW_MANY];
/* let's instruct the interface to use the right nameserver */
NS = MakeHostSpec("NS.test.edu", 8090);
UseNameServer(&NS);
/* get the series name: the right way is to query the nameserver to find
* the right name of the series. For simplicity we build one here,
* assuming that there is indeed such a series reigsterd with the
* nameserver */
spec = MakeSeriesSpec("a.edu", "b.edu", "bandwidthTcp");
series = SeriesName(spec);
/* get the forecasts */
if (!GetForecasts(series, NWSAPI_BEGINNING_OF_TIME, forecasts, HOW_MANY, &howMany))
{
printf("GetForecasts failed\n");
exit(0);
}
/* ForecastCollection keeps a copy of the measurements handy : let's
* print measurments and forecasts */
for (; howMany >= 0; howMany--)
{
printf("time %.5f measurement %.2f ", forecasts[howMany].measurement.timeStamp, forecasts[howMany].measurement.measurement)
/* we use only the MAE forecasts here */
pritnf("forecast %.5f with error %.5f method used %s\n", forecasts[howMany].forecasts[NWSAPI_MAE_FORECAST].forecast, forecasts[howMany].forecasts[NWSAPI_MAE_FORECAST].error, MethodName(forecasts[howMany].forecasts[NWSAPI_MAE_FORECAST].methodUsed);
}
Bugs list is at http://nws.cs.ucsb.edu.
Neil Spring, Jim Hayes (jhayes@cs.ucsd.edu) and Martin Swany maintained and enhanced it.
Graziano Obertelli (graziano@cs.ucsb.edu) currently maintains NWS and wrote this man page.