sympathy_print_stats.c from EmStar at Krugle
Show sympathy_print_stats.c syntax highlighted
/* ex: set tabstop=2 expandtab shiftwidth=2 softtabstop=2: */
/*
*
* 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.
*
*/
/*
*
* Author: Nithya Ramanthan
*
*/
#include <sympathy.h>
/* This is the example code for printing generic stats out! */
typedef struct _mdtnsym{
uint16_t my_stored_pkt_count;
uint16_t others_stored_pkt_count;
uint16_t current_pkts_stored;
uint32_t bytesUsed;
} __attribute__ ((packed)) mDTN_Sym_t;
lu_context_t* comp_status;
/* Component has translated the binar struct from the component -
* in this case it is from mDTN, and is sending back the ascii
* translaction. THIS WILL NOT work if the mDTN module is not
* wired in on the node side! */
static
void send_stats_pkt(link_pkt_t* pkt, buf_t* data_buf)
{
buf_t* buf = buf_new();
pkt->dst.id = LINK_BROADCAST;
pkt->type = SCOMP_ASCII_STATS;
bufcpy(buf, pkt, sizeof(link_pkt_t));
bufcpy(buf, data_buf->buf, data_buf->len);
elog(LOG_DEBUG(1), "buflen: %d\n", buf->len);
if (lu_send(comp_status, (link_pkt_t *)buf->buf, buf->len - sizeof(link_pkt_t)) < 0)
{
elog(LOG_ERR, "Unable to send pkt: %m!\n");
}
buf_free(buf);
}
static
void translate_comp3(link_pkt_t* pkt)
{
buf_t* buf = buf_new();
mDTN_Sym_t* d = (mDTN_Sym_t*) pkt->data;
bufprintf(buf, "Reliability Stats: My Pkt Stored, Other Pkts Stored, Current Stored, Bytes Used \n &*& %d,%d,%d,%d,%d \n",
pkt->src.id, d->my_stored_pkt_count, d->others_stored_pkt_count, d->current_pkts_stored, d->bytesUsed);
elog(LOG_DEBUG(1), "buf: %s\n", buf->buf);
/* Send the packet to sympathy */
send_stats_pkt(pkt, buf);
buf_free(buf);
}
static
int status_receive(lu_context_t* lu, link_pkt_t* pkt, ssize_t data_len)
{
elog(LOG_DEBUG(3), "src %d, Pkt w type/comp: %d/%d, datalen: %d\n",
pkt->src.id, pkt->type, pkt->ext_type, data_len);
/* Call print funcs! */
switch(pkt->type) {
case (SCOMP_STATS):
if (pkt->ext_type == SCOMP_STATS3_GEN) {
translate_comp3(pkt);
}
break;
default:
elog(LOG_DEBUG(3), "Ignoring pkt type %d\n", pkt->type);
break;
}
g_free(pkt);
return EVENT_RENEW;
}
static
void sympathy_shutdown(void *data)
{
elog(LOG_NOTICE, "sympathy-sink shutting down");
exit(0);
}
int main(int argc, char **argv)
{
emrun_opts_t emrun_opts = {
shutdown: sympathy_shutdown,
silent: 1
};
/* generic initialization common to all programs */
misc_init(&argc, argv, CVSTAG);
/* Link-user to pass status between components on the sink */
{
lu_opts_t lopts = {
opts: {
name: SYMPATHY_STATS_DEVICE,
pkt_type: PKT_TYPE_ALL
},
receive: status_receive
};
if (lu_open(&lopts, &comp_status) < 0)
{
elog(LOG_ERR, "UNable to open status device: %s: %m!\n",
lopts.opts.name);
exit(1);
}
elog(LOG_DEBUG(1), "Opened device: %s\n", lopts.opts.name);
}
emrun_init(&emrun_opts);
g_main();
elog_g(LOG_CRIT, "event loop terminated abnormally!");
return 0;
}
See more files for this project here