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
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
* Copyright (C) 2009 Alexander Sack <asac@jwsdot.com>
*
* This file is part of:
* ntrack - Network Status Tracking for Desktop Applications
* http://launchpad.net/ntrack
*
* ntrack is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* ntrack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with ntrack. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ntrackmonitor.h"
#include "ntrack-arch.h"
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#define PACK_BLOCK_SIZE 256
static ntrack_monitor_t* __new ();
struct _listener_pack {
ntrack_monitor_callback_f callback;
ntrackpointer user_data;
ntrack_destroy_f destroy;
};
ntrack_monitor_t*
ntrack_monitor_get ()
{
static ntrack_monitor_t *monitor = 0;
if (!monitor) {
monitor = __new ();
}
return monitor;
}
ntrackpointer
ntrack_monitor_register (ntrack_monitor_t *self,
ntrack_monitor_callback_f callback,
ntrackpointer data)
{
int i, p = -1;
if (self->pack_size <= self->pack_len + 1) {
struct _listener_pack **sav = self->packs;
self->pack_size += PACK_BLOCK_SIZE;
self->packs = calloc (sizeof (struct _listener_pack*), self->pack_size);
free(sav);
}
for (i = 0; i < self->pack_size && p == -1; i++) {
if(!self->packs[i])
p=i;
}
/* its a bug if no slot is found */
assert (p >= 0);
self->pack_len++;
self->packs[p] = calloc (sizeof (struct _listener_pack), 1);
assert (self->packs[p]);
self->packs[p]->callback = callback;
self->packs[p]->user_data = data;
return self->packs[p];
}
ntrackpointer
ntrack_monitor_register_full (ntrack_monitor_t *self,
ntrack_monitor_callback_f callback,
ntrackpointer data,
ntrack_destroy_f func)
{
struct _listener_pack *pack = ntrack_monitor_register (self, callback, data);
if (pack)
pack->destroy = func;
return pack;
}
int
ntrack_monitor_remove (ntrack_monitor_t *self,
ntrackpointer ntrack_handle)
{
int i;
struct _listener_pack *pack;
if (!ntrack_handle)
return 0;
for (i = 0; i < self->pack_size; i++) {
if (self->packs[i] == ntrack_handle)
break;
}
if (i >= self->pack_size)
return 0;
pack = self->packs[i];
if (pack->destroy)
pack->destroy(pack->user_data);
self->packs[i] = 0;
free (pack);
return 1;
}
int*
ntrack_monitor_get_rfds (ntrack_monitor_t *self)
{
return _ntrack_arch_get_rfds (self);
}
int
ntrack_monitor_process_data (ntrack_monitor_t *self, int* fds)
{
return _ntrack_arch_process_data (self, fds);
}
ntrack_state_t
ntrack_monitor_get_state (ntrack_monitor_t *self)
{
return self->state;
}
static void
dispatch_monitor_event (ntrack_monitor_t *self,
ntrack_event_t event)
{
int i;
switch (event) {
case NTRACK_EVENT_CONNECT:
self->state = NTRACK_STATE_ONLINE;
break;
case NTRACK_EVENT_DISCONNECT:
self->state = NTRACK_STATE_OFFLINE;
break;
case NTRACK_EVENT_RECONNECT:
self->state = NTRACK_STATE_ONLINE;
break;
default:
self->state = NTRACK_STATE_UNKNOWN;
}
for (i = 0; i < self->pack_len; i++) {
struct _listener_pack *pack = self->packs[i];
pack->callback (self, event, pack->user_data);
}
}
static void
process_monitor_arch_event (ntrack_monitor_t *self,
ntrack_event_t event,
ntrackpointer user_data)
{
switch (event) {
case NTRACK_EVENT_CONNECT:
dispatch_monitor_event (self, NTRACK_EVENT_CONNECT);
break;
case NTRACK_EVENT_DISCONNECT:
dispatch_monitor_event (self, NTRACK_EVENT_DISCONNECT);
break;
case NTRACK_EVENT_RECONNECT:
dispatch_monitor_event (self, NTRACK_EVENT_RECONNECT);
break;
default:
printf ("Unknown ntrack_event_t: %d\n", event);
assert (FALSE);
}
}
/* private/static funcs */
static ntrack_monitor_t*
__new ()
{
/*delegate to arch specific implementation */
ntrack_monitor_t *arch = _ntrack_arch_new (process_monitor_arch_event, NULL);
return arch;
}
|