sim_sensor_client.cc from EmStar at Krugle
Show sim_sensor_client.cc syntax highlighted
/*
*
* Copyright (c) 2003 The Regents of the University of California. All
* rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Neither the name of the University nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/*
*/
#include "sensor_state.h"
__BEGIN_DECLS
#include "sensor_nims.h"
__END_DECLS
#include "devel/as-emstar/ds.h"
//int retrieve_sample_value (void* arg, buf_t * buf,
//const sdev_sample_t * one_sample)
//replace the old print_bin_out
int retrieve_sample_value (void *arg, sc_sample_t * one_sample)
{
struct Nims_sensor_state *state = (struct Nims_sensor_state *)arg;
Adaptive_Sampling *as = (state == NULL)? NULL: state->as;
elog( LOG_ERR, "RETRIEVE, state: %x, as: %x\n", (int) state, (int) as);
if ( sizeof(NimsSensorDataType) != one_sample->data->len )
{
elog( LOG_ERR, "Invalid sample size: %d\n", one_sample->data->len );
return 0;
}
NimsSensorDataType value = * (NimsSensorDataType *) one_sample->data->buf;
if ( state )
state->sample_val = value;
else return EVENT_DONE;
// request_sample(as);
// return EVENT_DONE;
#ifdef LOG_SAMPLES
static FILE *fd;
static bool flag = false;
static bool already_log = false;
#endif
/*adaptive sampling processing */
if ( state->sample_val != INVALID_SENSOR_DATA)
{
as->cur_sp->value = state->sample_val;
//#ifdef TRANSFER_SAMPLES
send_sample_to_matlab( as, as->pos, state->sample_val);
//#endif
//add it to the corresponding Stratum it belongs to
assert( as->cur_sp->stratum_p != NULL );
if ( as->cur_sp->stratum_p->samples == NULL )
as->cur_sp->stratum_p->samples = new Samples;
as->cur_sp->stratum_p->samples->push_back( as->cur_sp );
}
if ( ! as->sample_list.empty() )
take_first_sample( as, &(as->sample_list) );
else {
if ( as->verbose() )
{
print_samples( & (as->sample_list), LOC_N_VALUE);
}
//stratum_node_cur_queue holds all the stratums in the current rank
//compute statistics for all the children Stratum of
//nodes in stratum_node_cur_queue.
//if a leaf node needs more sampling, add it to the working
//queue: stratum_node_work_queue
compute_stats_for_all( as, &(as->stratum_node_work_queue), &(as->stratum_node_cur_queue) );
elog(LOG_ERR, "after compute_stats_for_all\n" );
as_prepare_samples( as );
if ( ! as->sample_list.empty() )
{
elog(LOG_ERR, "sample list is not empty, After as_prepare_samples\n" );
take_first_sample( as, &(as->sample_list) );
} else {
//if a leaf node donot have new samples, only have samples
//migrated from its parent, then its statis never get computed,
//so compute it in print_tree
//END of this run;
//print out samples:
#ifdef LOG_SAMPLES
// if ( ! flag )
// {
fd = fopen("/tmp/sample_log", "a");
if ( fd == NULL )
{
elog( LOG_WARNING, "open /tmp/sample_log ERROR\n" );
}
// }
flag = true;
// if ( as->sample_list.empty() && fd && (! already_log) )
if ( as->sample_list.empty() && fd )
{
elog(LOG_ERR, "Before print_tree in log samples\n" );
fprintf(fd, "Tree\n" );
print_tree( fd, as, as->root );
elog(LOG_ERR, "After print_tree in log samples\n" );
fclose(fd);
already_log = true;
as_one_frame_done( as );
}
#endif
}
}
/* done? or keep watching? */
// if (state->watch == 0)
// g_status_client_destroy(state->ctx);
elog(LOG_ERR, "at the end of print_bin_output\n" );
return EVENT_DONE;
}
See more files for this project here
EmStar is a software system for developing and deploying wireless sensor networks involving Linux-based platforms. As the wireless sensor network community has attempted to deploy more complex designs---large-scale, long-lived systems that need self-organization and adaptivity---a number of difficult software design issues have arisen. Advances in software design have not kept pace with the capabilities of hardware. This is because designing for an adaptive, efficient, and useful sensor network has turned out to be surprisingly complex and difficult. EmStar is a Linux-based software framework, whose goal is to dramatically reduce this complexity, enabling work to be shared and reused, and simplifying and speeding the design of new sensor network applications.
Project homepage:
http://cvs.cens.ucla.edu/emstar/
Programming language(s): C,Shell Script
License: other
as-misc.cc
as-restart.cc
as-with-ta.cc
as.cc
as_process.cc
move_n_sample.cc
sim_sensor_client.cc
socket_server.cc
tcp_client.cc
tcp_func.c