Show libusfusd_g.c 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.
*
*/
/*
* event driven implementation of the usfusd socket protocol
*/
struct usfusd_proto_context {
usfusd_proto_opts_t opts;
usfusd_proto_context_t **ref;
buf_t *buf;
int disable;
g_event_t *event;
};
/* used to figure out whether we closed.. */
static usfusd_proto_context_t *context_in_process = NULL;
static
void usfusd_proto_destroy_aux(void *data, g_event_t *closure)
{
usfusd_proto_context_t *ctx = (usfusd_proto_context_t *)data;
if (ctx) {
if (ctx == context_in_progress)
context_in_progress = NULL;
if (ctx->ref) *(ctx->ref) = NULL;
if (ctx->buf) buf_free(ctx->buf);
if (ctx->opts.close)
ctx->opts.close(ctx->opts.private_data);
close(ctx->opts.socket);
free(ctx);
}
}
static
int usfusd_flush_queue(usfusd_proto_context_t *ctx)
{
/* got a complete message? */
while (ctx->buf->len >= sizeof(usfusd_msg_t)) {
usfusd_msg_t *msg = (usfusd_msg_t *)ctx->buf->buf;
int total = sizeof(usfusd_msg_t) + msg->datalen;
if (ctx->buf->len >= total) {
/* save last */
buf_t *last = ctx->buf;
ctx->buf = NULL;
/* copy remainder */
if (last->len > total) {
ctx->buf = buf_new();
bufcpy(ctx->buf, last->buf + total, last->len - total);
}
/* ready to detect closure */
context_in_process = c;
/* call input handler */
if (ctx->opts.input)
ctx->opts.input(msg, (msg->datalen > 0) ? (last->buf + sizeof(usfusd_msg_t)) : NULL,
ctx->opts.private_data);
/* free last buf */
buf_free(last);
/* if we got closed in the middle, quit now */
if (context_in_process == NULL)
return EVENT_DONE;
context_in_process = NULL;
/* if we are now disabled, return */
if (ctx->disable)
return EVENT_RENEW;
}
}
return EVENT_RENEW;
}
static
int usfusd_proto_input(void *data, int fd, int cond, g_event_t *event)
{
usfusd_proto_context_t *c = (usfusd_proto_context_t *)data;
if (ctx->disable) {
g_event_set_enable(event, 0);
return EVENT_RENEW;
}
if (cond & FUSD_NOTIFY_INPUT) {
char buf[4096];
int status = read(fd, buf, sizeof(buf));
/* handle eof */
if (status == 0)
return EVENT_DONE;
/* check for error return */
if (status < 0) {
if ((errno != EINTR) && (errno != EAGAIN))
return EVENT_DONE;
return EVENT_RENEW;
}
/* store the data and parse */
if (ctx->buf == NULL)
ctx->buf = buf_new();
bufcpy(ctx->buf, buf, status);
return usfusd_flush_queue(ctx);
}
return EVENT_RENEW;
}
void usfusd_proto_destroy(usfusd_proto_context_t *ref)
{
if (ref)
g_event_destroy(ref->event);
}
int usfusd_proto_new(usfusd_proto_opts_t *popts, usfusd_proto_context_t **ref)
{
struct g_event_opts_t ev_opts = {
destroy: usfusd_proto_destroy_aux
};
usfusd_proto_context_t *ctx = malloc(sizeof(usfusd_proto_context_t));
if (ctx == NULL) {
elog(LOG_CRIT, "Unable to malloc protocol context: %m");
exit(1);
}
memset(ctx, 0, sizeof(usfusd_proto_context_t));
ctx->opts = *popts;
ctx->ref = ref;
if (g_event_add(popts->socket, FUSD_NOTIFY_INPUT, usfusd_proto_input, ctx,
&ev_opts, &(ctx->event)) < 0) {
elog(LOG_CRIT, "Failed to create event for accept socket: %m\n");
exit(1);
}
return 0;
}
int usfusd_proto_set_enable(usfusd_proto_context_t *ref, int enable)
{
if (ref) {
ref->disable = !enable;
g_event_set_enable(event, enable);
if (enable)
usfusd_flush_queue(ref);
return 0;
}
return -1;
}
int usfusd_proto_send(usfusd_proto_context_t *ref, usfusd_msg_t *msg, char *data_buf)
{
if (ref)
return usfusd_write_msg(ref->opts.socket, msg, data_buf);
return -1;
}
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
libusfusd_fops.c
libusfusd_g.c
libusfusd_i.h
libusfusd_overload.c
libusfusd_rw.c
libusfusd_socket.c
libusfusd_util.c