2 * @file sipe-schedule.c
6 * Copyright (C) 2010 SIPE Project <http://sipe.sourceforge.net/>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "sipe-backend.h"
26 #include "sipe-core.h"
27 #include "sipe-core-private.h"
28 #include "sipe-schedule.h"
30 struct sipe_schedule
{
33 * Format is <Event>[<Data>...]
34 * Example: <presence><sip:user@domain.com> or <registration>
37 struct sipe_core_private
*sipe_private
;
38 gpointer backend_private
;
40 sipe_schedule_action action
;
41 GDestroyNotify destroy
;
44 static void sipe_schedule_deallocate(struct sipe_schedule
*schedule
)
46 if (schedule
->destroy
) (*schedule
->destroy
)(schedule
->payload
);
47 g_free(schedule
->name
);
51 void sipe_core_schedule_execute(gpointer data
)
53 struct sipe_schedule
*expired
= data
;
54 struct sipe_core_private
*sipe_private
= expired
->sipe_private
;
56 SIPE_DEBUG_INFO("sipe_core_schedule_execute: executing %s", expired
->name
);
57 sipe_private
->timeouts
= g_slist_remove(sipe_private
->timeouts
, expired
);
58 SIPE_DEBUG_INFO("sipe_core_schedule_execute timeouts count %d after removal",
59 g_slist_length(sipe_private
->timeouts
));
61 (*expired
->action
)(sipe_private
, expired
->payload
);
62 sipe_schedule_deallocate(expired
);
65 static struct sipe_schedule
*sipe_schedule_allocate(struct sipe_core_private
*sipe_private
,
68 sipe_schedule_action action
,
69 GDestroyNotify destroy
)
71 struct sipe_schedule
*new;
73 /* Make sure each action only exists once */
74 sipe_schedule_cancel(sipe_private
, name
);
76 new = g_new0(struct sipe_schedule
, 1);
77 new->name
= g_strdup(name
);
78 new->sipe_private
= sipe_private
;
79 new->payload
= payload
;
81 new->destroy
= destroy
;
82 sipe_private
->timeouts
= g_slist_append(sipe_private
->timeouts
, new);
83 SIPE_DEBUG_INFO("sipe_schedule_allocate timeouts count %d after addition",
84 g_slist_length(sipe_private
->timeouts
));
88 void sipe_schedule_seconds(struct sipe_core_private
*sipe_private
,
92 sipe_schedule_action action
,
93 GDestroyNotify destroy
)
95 struct sipe_schedule
*new = sipe_schedule_allocate(sipe_private
,
100 SIPE_DEBUG_INFO("scheduling action %s timeout %d seconds",
102 new->backend_private
= sipe_backend_schedule_seconds(SIPE_CORE_PUBLIC
,
107 void sipe_schedule_mseconds(struct sipe_core_private
*sipe_private
,
111 sipe_schedule_action action
,
112 GDestroyNotify destroy
)
114 struct sipe_schedule
*new = sipe_schedule_allocate(sipe_private
,
119 SIPE_DEBUG_INFO("scheduling action %s timeout %d milliseconds",
121 new->backend_private
= sipe_backend_schedule_mseconds(SIPE_CORE_PUBLIC
,
126 static void sipe_schedule_remove(struct sipe_core_private
*sipe_private
,
127 struct sipe_schedule
*schedule
)
129 SIPE_DEBUG_INFO("sipe_schedule_remove: action name=%s",
131 sipe_backend_schedule_cancel(SIPE_CORE_PUBLIC
,
132 schedule
->backend_private
);
133 sipe_schedule_deallocate(schedule
);
136 void sipe_schedule_cancel(struct sipe_core_private
*sipe_private
,
141 if (!sipe_private
->timeouts
|| !name
) return;
143 entry
= sipe_private
->timeouts
;
145 struct sipe_schedule
*schedule
= entry
->data
;
146 if (sipe_strequal(schedule
->name
, name
)) {
147 GSList
*to_delete
= entry
;
149 sipe_private
->timeouts
= g_slist_delete_link(sipe_private
->timeouts
,
151 sipe_schedule_remove(sipe_private
, schedule
);
158 void sipe_schedule_cancel_all(struct sipe_core_private
*sipe_private
)
160 GSList
*entry
= sipe_private
->timeouts
;
163 sipe_schedule_remove(sipe_private
, entry
->data
);
167 g_slist_free(sipe_private
->timeouts
);
168 sipe_private
->timeouts
= NULL
;