[svn-commits] twilson: branch twilson/calendaring r167018 - in /team/twilson/calendaring: m...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Wed Dec 31 14:45:17 CST 2008


Author: twilson
Date: Wed Dec 31 14:45:17 2008
New Revision: 167018

URL: http://svn.digium.com/view/asterisk?view=rev&rev=167018
Log:
Get rid of the taskprocessor used for waiting for FULLY_BOOTED and just handle the tech specific calendar loading in ast_calendar_register (as per russell on reviewboard).  We are still only reading the config file once and keeping the config around in a global variable that the calendar techs can see.

Modified:
    team/twilson/calendaring/main/calendar.c
    team/twilson/calendaring/res/res_caldav.c
    team/twilson/calendaring/res/res_exchangecal.c
    team/twilson/calendaring/res/res_icalendar.c

Modified: team/twilson/calendaring/main/calendar.c
URL: http://svn.digium.com/view/asterisk/team/twilson/calendaring/main/calendar.c?view=diff&rev=167018&r1=167017&r2=167018
==============================================================================
--- team/twilson/calendaring/main/calendar.c (original)
+++ team/twilson/calendaring/main/calendar.c Wed Dec 31 14:45:17 2008
@@ -27,7 +27,6 @@
 #include "asterisk/_private.h"
 #include "asterisk/calendar.h"
 #include "asterisk/utils.h"
-#include "asterisk/taskprocessor.h"
 #include "asterisk/astobj2.h"
 #include "asterisk/module.h"
 #include "asterisk/config.h"
@@ -73,6 +72,8 @@
 AST_LIST_HEAD_STATIC(techs, ast_calendar_tech);
 AST_LIST_HEAD_NOLOCK(eventlist, evententry); /* define the type */
 
+struct ast_config *calendar_config = NULL;
+
 static struct ast_calendar *unref_calendar(struct ast_calendar *cal)
 {
 	ao2_ref(cal, -1);
@@ -189,444 +190,6 @@
 	}
 
 	return calendar_is_busy(cal) ? AST_DEVICE_INUSE : AST_DEVICE_NOT_INUSE;
-}
-
-int ast_calendar_register(struct ast_calendar_tech *tech)
-{
-	struct ast_calendar_tech *iter;
-
-	AST_LIST_LOCK(&techs);
-	AST_LIST_TRAVERSE(&techs, iter, list) {
-		if(!strcasecmp(tech->type, iter->type)) {
-			ast_log(LOG_WARNING, "Already have a handler for calendar type '%s'\n", tech->type);
-			AST_LIST_UNLOCK(&techs);
-			return -1;
-		}
-	}
-	AST_LIST_INSERT_HEAD(&techs, tech, list);
-	AST_LIST_UNLOCK(&techs);
-
-	ast_verb(2, "Registered calendar type '%s' (%s)\n", tech->type, tech->description);
-
-	return 0;
-}
-
-static int match_caltech_cb(void *user_data, void *arg, int flags)
-{
-	struct ast_calendar *cal = user_data;
-	struct ast_calendar_tech *tech = arg;
-
-	if (cal->tech == tech) {
-		return CMP_MATCH;
-	}
-
-	return 0;
-}
-
-void ast_calendar_unregister(struct ast_calendar_tech *tech)
-{
-	struct ast_calendar_tech *iter;
-
-	AST_LIST_LOCK(&techs);
-	AST_LIST_TRAVERSE_SAFE_BEGIN(&techs, iter, list) {
-		if (iter != tech) {
-			continue;
-		}
-
-		ao2_callback(calendars, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, match_caltech_cb, (struct ast_calendar_tech *) tech);
-
-		AST_LIST_REMOVE_CURRENT(list);
-		ast_verb(2, "Unregistered calendar type '%s'\n", tech->type);
-		break;
-	}
-	AST_LIST_TRAVERSE_SAFE_END;
-	AST_LIST_UNLOCK(&techs);
-
-}
-
-static const struct ast_calendar_tech *ast_get_calendar_tech(const char *name)
-{
-	const struct ast_calendar_tech *ret = NULL;
-
-	AST_LIST_LOCK(&techs);
-	AST_LIST_TRAVERSE(&techs, ret, list) {
-		if (!strcasecmp(name, ret->type)) {
-			break;
-		}
-	}
-	AST_LIST_UNLOCK(&techs);
-
-	return ret;
-}
-
-static void calendar_event_destructor(void *obj)
-{
-	struct ast_calendar_event *event = obj;
-
-	ast_debug(3, "Destroying event for calendar '%s'\n", event->owner->name);
-	ast_string_field_free_memory(event);
-
-	return;
-}
-
-static int cb_true(void *user_data, void *arg, int flags)
-{
-	return CMP_MATCH;
-}
-
-static struct ast_calendar_event *destroy_event(struct ast_calendar_event *event)
-{
-	if (event->notify_sched > -1 && ast_sched_del(sched, event->notify_sched)) {
-		ast_debug(3, "Notification running, can't delete sched entry\n");
-	}
-	if (event->bs_start_sched > -1 && ast_sched_del(sched, event->bs_start_sched)) {
-		ast_debug(3, "Devicestate update (start) running, can't delete sched entry\n");
-	}
-	if (event->bs_end_sched > -1 && ast_sched_del(sched, event->bs_end_sched)) {
-		ast_debug(3, "Devicestate update (end) running, can't delete sched entry\n");
-	}
-
-	/* If an event is being deleted and we've fired an event changing the status at the beginning,
-	 * but haven't hit the end event yet, go ahead and set the devicestate to the current busy status */
-	if (event->bs_start_sched < 0 && event->bs_end_sched >= 0) {
-		if (!calendar_is_busy(event->owner)) {
-			ast_devstate_changed(AST_DEVICE_NOT_INUSE, "Calendar/%s", event->owner->name);
-		} else {
-			ast_devstate_changed(AST_DEVICE_BUSY, "Calendar/%s", event->owner->name);
-		}
-	}
-
-	return NULL;
-}
-
-static int clear_events_cb(void *user_data, void *arg, int flags)
-{
-	struct ast_calendar_event *event = user_data;
-
-	event = destroy_event(event);
-
-	return CMP_MATCH;
-}
-
-void ast_calendar_clear_events(struct ast_calendar *cal)
-{
-	ast_debug(3, "Clearing all events for calendar %s\n", cal->name);
-
-	ao2_callback(cal->events, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, clear_events_cb, NULL);
-}
-
-struct ast_calendar_event *ast_calendar_event_alloc(struct ast_calendar *cal)
-{
-	struct ast_calendar_event *event;
-	if (!(event = ao2_alloc(sizeof(*event), calendar_event_destructor))) {
-		ast_log(LOG_ERROR, "Could not allocate memory for event!\n");
-		return NULL;
-	}
-
-	if (ast_string_field_init(event, 32)) {
-		ast_log(LOG_ERROR, "Could not allocate memory for event fields\n");
-		event = ast_calendar_unref_event(event);
-		return NULL;
-	}
-
-	event->owner = cal;
-	event->notify_sched = -1;
-	event->bs_start_sched = -1;
-	event->bs_end_sched = -1;
-
-	return event;
-}
-
-struct ao2_container *ast_calendar_event_container_alloc(void)
-{
-	return ao2_container_alloc(MAX_BUCKETS, event_hash_fn, event_cmp_fn);
-}
-
-static void event_notification_destroy(void *data)
-{
-	struct ast_calendar_event *event = data;
-
-	event = ast_calendar_unref_event(event);
-
-}
-
-static void *event_notification_duplicate(void *data)
-{
-	struct ast_calendar_event *event = data;
-
-	if (!event) {
-		return NULL;
-	}
-
-	ao2_ref(event, +1);
-
-	return event;
-}
-
-/*! \brief Generate 32 byte random string (stolen from chan_sip.c)*/
-static char *generate_random_string(char *buf, size_t size)
-{
-	long val[4];
-	int x;
-
-	for (x = 0; x < 4; x++) {
-		val[x] = ast_random();
-	}
-	snprintf(buf, size, "%08lx%08lx%08lx%08lx", val[0], val[1], val[2], val[3]);
-
-	return buf;
-}
-
-static int calendar_event_notify(const void *data)
-{
-	struct ast_calendar_event *event = (void *)data;
-	char tech[256], dest[256], buf[8], *tmp;
-	struct ast_dial *dial = NULL;
-	struct ast_channel *chan = NULL;
-	struct ast_str *apptext = NULL;
-	int res = -1;
-	char start[12], end[12], busystate[2];
-	struct ast_datastore *datastore;
-
-	if (!(event && event->owner)) {
-		ast_log(LOG_ERROR, "Extremely low-cal...in fact cal is NULL!\n");
-		goto notify_cleanup;
-	}
-
-	ao2_ref(event, +1);
-	event->notify_sched = -1;
-
-	ast_copy_string(tech, event->owner->notify_channel, sizeof(tech));
-
-	if ((tmp = strchr(tech, '/'))) {
-		*tmp = '\0';
-		tmp++;
-		ast_copy_string(dest, tmp, sizeof(dest));
-	} else {
-		ast_log(LOG_WARNING, "Channel should be in form Tech/Dest\n");
-		goto notify_cleanup;
-	}
-
-	if (!(dial = ast_dial_create())) {
-		ast_log(LOG_ERROR, "Could not create dial structure\n");
-		goto notify_cleanup;
-	}
-
-	if (ast_dial_append(dial, tech, dest) < 0) {
-		ast_log(LOG_ERROR, "Could not append channel\n");
-		goto notify_cleanup;
-	}
-
-	ast_dial_set_global_timeout(dial, event->owner->notify_waittime);
-	generate_random_string(buf, sizeof(buf));
-	if (!(chan = ast_channel_alloc(1, AST_STATE_DOWN, 0, 0, 0, 0, 0, 0, "Calendar/%s-%s", event->owner->name, buf))) {
-		ast_log(LOG_ERROR, "Could not allocate notification channel\n");
-		goto notify_cleanup;
-	}
-
-	snprintf(busystate, sizeof(busystate), "%d", event->busy_state);
-	snprintf(start, sizeof(start), "%lu", event->start);
-	snprintf(end, sizeof(end), "%lu", event->end);
-
-	chan->nativeformats = AST_FORMAT_SLINEAR;
-
-	if (!(datastore = ast_datastore_alloc(&event_notification_datastore, NULL))) {
-		ast_log(LOG_ERROR, "Could not allocate datastore, notification not being sent!\n");
-		goto notify_cleanup;
-	}
-
-	datastore->data = event;
-	datastore->inheritance = DATASTORE_INHERIT_FOREVER;
-
-	ao2_ref(event, +1);
-	res = ast_channel_datastore_add(chan, datastore);
-
-	if (!(apptext = ast_str_create(32))) {
-		ast_log(LOG_ERROR, "Could not allocate space for string\n");
-		goto notify_cleanup;
-	}
-
-	if (!ast_strlen_zero(event->owner->notify_app)) {
-		ast_str_set(&apptext, 0, "%s,%s", event->owner->notify_app, event->owner->notify_appdata);
-	} else {
-		ast_str_set(&apptext, 0, "Dial,Local/%s@%s", event->owner->notify_extension, event->owner->notify_context);
-	}
-	ast_dial_option_global_enable(dial, AST_DIAL_OPTION_ANSWER_EXEC, ast_str_buffer(apptext));
-
-	ast_dial_run(dial, chan, 1);
-	res = 0;
-
-notify_cleanup:
-	event = ast_calendar_unref_event(event);
-	if (res == -1 && dial) {
-		ast_dial_destroy(dial);
-	}
-	if (apptext) {
-		ast_free(apptext);
-	}
-	if (chan) {
-		ast_channel_free(chan);
-	}
-
-	return res;
-}
-
-static int calendar_devstate_change(const void *data)
-{
-	struct ast_calendar_event *event = (struct ast_calendar_event *)data;
-	struct timeval now = ast_tvnow();
-	int is_end_event;
-
-	if (!event) {
-		ast_log(LOG_WARNING, "Event was NULL!\n");
-		return 0;
-	}
-
-	ao2_ref(event, +1);
-
-	is_end_event = event->end <= now.tv_sec;
-
-	if (is_end_event) {
-		event->bs_end_sched = -1;
-	} else {
-		event->bs_start_sched = -1;
-	}
-
-	/* We can have overlapping events, so ignore the event->busy_state and check busy state
-	 * based on all events in the calendar */
-	if (!calendar_is_busy(event->owner)) {
-		ast_devstate_changed(AST_DEVICE_NOT_INUSE, "Calendar/%s", event->owner->name);
-	} else {
-		ast_devstate_changed(AST_DEVICE_BUSY, "Calendar/%s", event->owner->name);
-	}
-
-	event = ast_calendar_unref_event(event);
-
-	return 0;
-}
-
-static void copy_event_data(struct ast_calendar_event *dst, struct ast_calendar_event *src)
-{
-	ast_string_field_set(dst, summary, src->summary);
-	ast_string_field_set(dst, description, src->description);
-	ast_string_field_set(dst, organizer, src->organizer);
-	ast_string_field_set(dst, location, src->location);
-	ast_string_field_set(dst, uid, src->uid);
-	dst->owner = src->owner;
-	dst->start = src->start;
-	dst->end = src->end;
-	dst->alarm = src->alarm;
-	dst->busy_state = src->busy_state;
-}
-
-static int schedule_calendar_event(struct ast_calendar *cal, struct ast_calendar_event *old_event, struct ast_calendar_event *cmp_event)
-{
-	struct timeval now = ast_tvnow();
-	struct ast_calendar_event *event;
-	time_t alarm_notify_sched = 0, devstate_sched_start, devstate_sched_end;
-	int changed = 0;
-
-	event = cmp_event ? cmp_event : old_event;
-
-	ao2_lock(event);
-	if (!cmp_event || old_event->alarm != event->alarm) {
-		changed = 1;
-		if (cal->autoreminder) {
-			alarm_notify_sched = (event->start - (60 * cal->autoreminder) - now.tv_sec) * 1000;
-		} else if (event->alarm) {
-			alarm_notify_sched = (event->alarm - now.tv_sec) * 1000;
-		}
-
-		/* For now, send the notification if we missed it, but the meeting hasn't happened yet */
-		if (event->start >=  now.tv_sec) {
-			if (alarm_notify_sched <= 0) {
-				alarm_notify_sched = 1;
-			}
-			ast_mutex_lock(&refreshlock);
-			AST_SCHED_REPLACE(old_event->notify_sched, sched, alarm_notify_sched, calendar_event_notify, old_event);
-			ast_mutex_unlock(&refreshlock);
-			ast_debug(3, "Calendar alarm event notification scheduled to happen in %ld ms\n", alarm_notify_sched);
-		}
-	}
-
-	if (!cmp_event || old_event->start != event->start) {
-		changed = 1;
-		devstate_sched_start = (event->start - now.tv_sec) * 1000;
-
-		if (devstate_sched_start < 1) {
-			devstate_sched_start = 1;
-		}
-
-		ast_mutex_lock(&refreshlock);
-		AST_SCHED_REPLACE(old_event->bs_start_sched, sched, devstate_sched_start, calendar_devstate_change, old_event);
-		ast_mutex_unlock(&refreshlock);
-		ast_debug(3, "Calendar bs_start event notification scheduled to happen in %ld ms\n", devstate_sched_start);
-	}
-
-	if (!cmp_event || old_event->end != event->end) {
-		changed = 1;
-		devstate_sched_end = (event->end - now.tv_sec) * 1000;
-		ast_mutex_lock(&refreshlock);
-		AST_SCHED_REPLACE(old_event->bs_end_sched, sched, devstate_sched_end, calendar_devstate_change, old_event);
-		ast_mutex_unlock(&refreshlock);
-		ast_debug(3, "Calendar bs_end event notification scheduled to happen in %ld ms\n", devstate_sched_end);
-	}
-
-	if (changed) {
-		ast_cond_signal(&refresh_condition);
-	}
-
-	ao2_unlock(event);
-
-	return 0;
-}
-
-static int merge_events_cb(void *obj, void *arg, int flags)
-{
-	struct ast_calendar_event *old_event = obj, *new_event;
-	struct ao2_container *new_events = arg;
-
-	/* If we don't find the old_event in new_events, then we can safely delete the old_event */
-	if (!(new_event = find_event(new_events, old_event->uid))) {
-		old_event = destroy_event(old_event);
-		return CMP_MATCH;
-	}
-
-	/* We have events to merge.  If any data that will affect a scheduler event has changed,
-	 * then we need to replace the scheduler event */
-	schedule_calendar_event(old_event->owner, old_event, new_event);
-
-	/* Since we don't want to mess with cancelling sched events and adding new ones, just
-	 * copy the internals of the new_event to the old_event */
-	copy_event_data(old_event, new_event);
-
-	/* Now we can go ahead and unlink the new_event from new_events and unref it so that only completely
-	 * new events remain in the container */
-	ao2_unlink(new_events, new_event);
-	new_event = ast_calendar_unref_event(new_event);
-
-	return 0;
-}
-
-static int add_new_event_cb(void *obj, void *arg, int flags)
-{
-	struct ast_calendar_event *new_event = obj;
-	struct ao2_container *events = arg;
-
-	ao2_link(events, new_event);
-	schedule_calendar_event(new_event->owner, new_event, NULL);
-	return CMP_MATCH;
-}
-
-void ast_calendar_merge_events(struct ast_calendar *cal, struct ao2_container *new_events)
-{
-	/* Loop through all events attached to the calendar.  If there is a matching new event
-	 * merge its data over and handle any schedule changes that need to be made.  Then remove
-	 * the new_event from new_events so that we are left with only new_events that we can add later. */
-	ao2_callback(cal->events, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, merge_events_cb, new_events);
-
-	/* Now, we should only have completely new events in new_events.  Loop through and add them */
-	ao2_callback(new_events, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, add_new_event_cb, cal->events);
 }
 
 static struct ast_calendar *build_calendar(struct ast_config *cfg, const char *cat, const struct ast_calendar_tech *tech)
@@ -685,14 +248,469 @@
 	return cal;
 }
 
+static int load_tech_calendars(struct ast_calendar_tech *tech)
+{
+	struct ast_calendar *cal;
+	const char *cat = NULL;
+	const char *val;
+
+	while ((cat = ast_category_browse(calendar_config, cat))) {
+		if (!strcasecmp(cat, "general")) {
+			continue;
+		}
+
+		if (!(val = ast_variable_retrieve(calendar_config, cat, "type")) || strcasecmp(val, tech->type)) {
+			continue;
+		}
+
+		/* A serious error occurred loading calendars from this tech and it should be disabled */
+		if (!(cal = build_calendar(calendar_config, cat, tech))) {
+			ast_calendar_unregister(tech);
+			return -1;
+		}
+
+		ao2_link(calendars, cal);
+
+		ast_pthread_create(&cal->thread, NULL, cal->tech->load_calendar, cal);
+
+		cal = unref_calendar(cal);
+	}
+
+	return 0;
+}
+
+int ast_calendar_register(struct ast_calendar_tech *tech)
+{
+	struct ast_calendar_tech *iter;
+
+	AST_LIST_LOCK(&techs);
+	AST_LIST_TRAVERSE(&techs, iter, list) {
+		if(!strcasecmp(tech->type, iter->type)) {
+			ast_log(LOG_WARNING, "Already have a handler for calendar type '%s'\n", tech->type);
+			AST_LIST_UNLOCK(&techs);
+			return -1;
+		}
+	}
+	AST_LIST_INSERT_HEAD(&techs, tech, list);
+	AST_LIST_UNLOCK(&techs);
+
+	ast_verb(2, "Registered calendar type '%s' (%s)\n", tech->type, tech->description);
+
+	return load_tech_calendars(tech);
+}
+
+static int match_caltech_cb(void *user_data, void *arg, int flags)
+{
+	struct ast_calendar *cal = user_data;
+	struct ast_calendar_tech *tech = arg;
+
+	if (cal->tech == tech) {
+		return CMP_MATCH;
+	}
+
+	return 0;
+}
+
+void ast_calendar_unregister(struct ast_calendar_tech *tech)
+{
+	struct ast_calendar_tech *iter;
+
+	AST_LIST_LOCK(&techs);
+	AST_LIST_TRAVERSE_SAFE_BEGIN(&techs, iter, list) {
+		if (iter != tech) {
+			continue;
+		}
+
+		ao2_callback(calendars, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, match_caltech_cb, (struct ast_calendar_tech *) tech);
+
+		AST_LIST_REMOVE_CURRENT(list);
+		ast_verb(2, "Unregistered calendar type '%s'\n", tech->type);
+		break;
+	}
+	AST_LIST_TRAVERSE_SAFE_END;
+	AST_LIST_UNLOCK(&techs);
+
+}
+
+static void calendar_event_destructor(void *obj)
+{
+	struct ast_calendar_event *event = obj;
+
+	ast_debug(3, "Destroying event for calendar '%s'\n", event->owner->name);
+	ast_string_field_free_memory(event);
+
+	return;
+}
+
+static int cb_true(void *user_data, void *arg, int flags)
+{
+	return CMP_MATCH;
+}
+
+static struct ast_calendar_event *destroy_event(struct ast_calendar_event *event)
+{
+	if (event->notify_sched > -1 && ast_sched_del(sched, event->notify_sched)) {
+		ast_debug(3, "Notification running, can't delete sched entry\n");
+	}
+	if (event->bs_start_sched > -1 && ast_sched_del(sched, event->bs_start_sched)) {
+		ast_debug(3, "Devicestate update (start) running, can't delete sched entry\n");
+	}
+	if (event->bs_end_sched > -1 && ast_sched_del(sched, event->bs_end_sched)) {
+		ast_debug(3, "Devicestate update (end) running, can't delete sched entry\n");
+	}
+
+	/* If an event is being deleted and we've fired an event changing the status at the beginning,
+	 * but haven't hit the end event yet, go ahead and set the devicestate to the current busy status */
+	if (event->bs_start_sched < 0 && event->bs_end_sched >= 0) {
+		if (!calendar_is_busy(event->owner)) {
+			ast_devstate_changed(AST_DEVICE_NOT_INUSE, "Calendar/%s", event->owner->name);
+		} else {
+			ast_devstate_changed(AST_DEVICE_BUSY, "Calendar/%s", event->owner->name);
+		}
+	}
+
+	return NULL;
+}
+
+static int clear_events_cb(void *user_data, void *arg, int flags)
+{
+	struct ast_calendar_event *event = user_data;
+
+	event = destroy_event(event);
+
+	return CMP_MATCH;
+}
+
+void ast_calendar_clear_events(struct ast_calendar *cal)
+{
+	ast_debug(3, "Clearing all events for calendar %s\n", cal->name);
+
+	ao2_callback(cal->events, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, clear_events_cb, NULL);
+}
+
+struct ast_calendar_event *ast_calendar_event_alloc(struct ast_calendar *cal)
+{
+	struct ast_calendar_event *event;
+	if (!(event = ao2_alloc(sizeof(*event), calendar_event_destructor))) {
+		ast_log(LOG_ERROR, "Could not allocate memory for event!\n");
+		return NULL;
+	}
+
+	if (ast_string_field_init(event, 32)) {
+		ast_log(LOG_ERROR, "Could not allocate memory for event fields\n");
+		event = ast_calendar_unref_event(event);
+		return NULL;
+	}
+
+	event->owner = cal;
+	event->notify_sched = -1;
+	event->bs_start_sched = -1;
+	event->bs_end_sched = -1;
+
+	return event;
+}
+
+struct ao2_container *ast_calendar_event_container_alloc(void)
+{
+	return ao2_container_alloc(MAX_BUCKETS, event_hash_fn, event_cmp_fn);
+}
+
+static void event_notification_destroy(void *data)
+{
+	struct ast_calendar_event *event = data;
+
+	event = ast_calendar_unref_event(event);
+
+}
+
+static void *event_notification_duplicate(void *data)
+{
+	struct ast_calendar_event *event = data;
+
+	if (!event) {
+		return NULL;
+	}
+
+	ao2_ref(event, +1);
+
+	return event;
+}
+
+/*! \brief Generate 32 byte random string (stolen from chan_sip.c)*/
+static char *generate_random_string(char *buf, size_t size)
+{
+	long val[4];
+	int x;
+
+	for (x = 0; x < 4; x++) {
+		val[x] = ast_random();
+	}
+	snprintf(buf, size, "%08lx%08lx%08lx%08lx", val[0], val[1], val[2], val[3]);
+
+	return buf;
+}
+
+static int calendar_event_notify(const void *data)
+{
+	struct ast_calendar_event *event = (void *)data;
+	char tech[256], dest[256], buf[8], *tmp;
+	struct ast_dial *dial = NULL;
+	struct ast_channel *chan = NULL;
+	struct ast_str *apptext = NULL;
+	int res = -1;
+	char start[12], end[12], busystate[2];
+	struct ast_datastore *datastore;
+
+	if (!(event && event->owner)) {
+		ast_log(LOG_ERROR, "Extremely low-cal...in fact cal is NULL!\n");
+		goto notify_cleanup;
+	}
+
+	ao2_ref(event, +1);
+	event->notify_sched = -1;
+
+	ast_copy_string(tech, event->owner->notify_channel, sizeof(tech));
+
+	if ((tmp = strchr(tech, '/'))) {
+		*tmp = '\0';
+		tmp++;
+		ast_copy_string(dest, tmp, sizeof(dest));
+	} else {
+		ast_log(LOG_WARNING, "Channel should be in form Tech/Dest\n");
+		goto notify_cleanup;
+	}
+
+	if (!(dial = ast_dial_create())) {
+		ast_log(LOG_ERROR, "Could not create dial structure\n");
+		goto notify_cleanup;
+	}
+
+	if (ast_dial_append(dial, tech, dest) < 0) {
+		ast_log(LOG_ERROR, "Could not append channel\n");
+		goto notify_cleanup;
+	}
+
+	ast_dial_set_global_timeout(dial, event->owner->notify_waittime);
+	generate_random_string(buf, sizeof(buf));
+	if (!(chan = ast_channel_alloc(1, AST_STATE_DOWN, 0, 0, 0, 0, 0, 0, "Calendar/%s-%s", event->owner->name, buf))) {
+		ast_log(LOG_ERROR, "Could not allocate notification channel\n");
+		goto notify_cleanup;
+	}
+
+	snprintf(busystate, sizeof(busystate), "%d", event->busy_state);
+	snprintf(start, sizeof(start), "%lu", event->start);
+	snprintf(end, sizeof(end), "%lu", event->end);
+
+	chan->nativeformats = AST_FORMAT_SLINEAR;
+
+	if (!(datastore = ast_datastore_alloc(&event_notification_datastore, NULL))) {
+		ast_log(LOG_ERROR, "Could not allocate datastore, notification not being sent!\n");
+		goto notify_cleanup;
+	}
+
+	datastore->data = event;
+	datastore->inheritance = DATASTORE_INHERIT_FOREVER;
+
+	ao2_ref(event, +1);
+	res = ast_channel_datastore_add(chan, datastore);
+
+	if (!(apptext = ast_str_create(32))) {
+		ast_log(LOG_ERROR, "Could not allocate space for string\n");
+		goto notify_cleanup;
+	}
+
+	if (!ast_strlen_zero(event->owner->notify_app)) {
+		ast_str_set(&apptext, 0, "%s,%s", event->owner->notify_app, event->owner->notify_appdata);
+	} else {
+		ast_str_set(&apptext, 0, "Dial,Local/%s@%s", event->owner->notify_extension, event->owner->notify_context);
+	}
+	ast_dial_option_global_enable(dial, AST_DIAL_OPTION_ANSWER_EXEC, ast_str_buffer(apptext));
+
+	ast_dial_run(dial, chan, 1);
+	res = 0;
+
+notify_cleanup:
+	event = ast_calendar_unref_event(event);
+	if (res == -1 && dial) {
+		ast_dial_destroy(dial);
+	}
+	if (apptext) {
+		ast_free(apptext);
+	}
+	if (chan) {
+		ast_channel_free(chan);
+	}
+
+	return res;
+}
+
+static int calendar_devstate_change(const void *data)
+{
+	struct ast_calendar_event *event = (struct ast_calendar_event *)data;
+	struct timeval now = ast_tvnow();
+	int is_end_event;
+
+	if (!event) {
+		ast_log(LOG_WARNING, "Event was NULL!\n");
+		return 0;
+	}
+
+	ao2_ref(event, +1);
+
+	is_end_event = event->end <= now.tv_sec;
+
+	if (is_end_event) {
+		event->bs_end_sched = -1;
+	} else {
+		event->bs_start_sched = -1;
+	}
+
+	/* We can have overlapping events, so ignore the event->busy_state and check busy state
+	 * based on all events in the calendar */
+	if (!calendar_is_busy(event->owner)) {
+		ast_devstate_changed(AST_DEVICE_NOT_INUSE, "Calendar/%s", event->owner->name);
+	} else {
+		ast_devstate_changed(AST_DEVICE_BUSY, "Calendar/%s", event->owner->name);
+	}
+
+	event = ast_calendar_unref_event(event);
+
+	return 0;
+}
+
+static void copy_event_data(struct ast_calendar_event *dst, struct ast_calendar_event *src)
+{
+	ast_string_field_set(dst, summary, src->summary);
+	ast_string_field_set(dst, description, src->description);
+	ast_string_field_set(dst, organizer, src->organizer);
+	ast_string_field_set(dst, location, src->location);
+	ast_string_field_set(dst, uid, src->uid);
+	dst->owner = src->owner;
+	dst->start = src->start;
+	dst->end = src->end;
+	dst->alarm = src->alarm;
+	dst->busy_state = src->busy_state;
+}
+
+static int schedule_calendar_event(struct ast_calendar *cal, struct ast_calendar_event *old_event, struct ast_calendar_event *cmp_event)
+{
+	struct timeval now = ast_tvnow();
+	struct ast_calendar_event *event;
+	time_t alarm_notify_sched = 0, devstate_sched_start, devstate_sched_end;
+	int changed = 0;
+
+	event = cmp_event ? cmp_event : old_event;
+
+	ao2_lock(event);
+	if (!cmp_event || old_event->alarm != event->alarm) {
+		changed = 1;
+		if (cal->autoreminder) {
+			alarm_notify_sched = (event->start - (60 * cal->autoreminder) - now.tv_sec) * 1000;
+		} else if (event->alarm) {
+			alarm_notify_sched = (event->alarm - now.tv_sec) * 1000;
+		}
+
+		/* For now, send the notification if we missed it, but the meeting hasn't happened yet */
+		if (event->start >=  now.tv_sec) {
+			if (alarm_notify_sched <= 0) {
+				alarm_notify_sched = 1;
+			}
+			ast_mutex_lock(&refreshlock);
+			AST_SCHED_REPLACE(old_event->notify_sched, sched, alarm_notify_sched, calendar_event_notify, old_event);
+			ast_mutex_unlock(&refreshlock);
+			ast_debug(3, "Calendar alarm event notification scheduled to happen in %ld ms\n", alarm_notify_sched);
+		}
+	}
+
+	if (!cmp_event || old_event->start != event->start) {
+		changed = 1;
+		devstate_sched_start = (event->start - now.tv_sec) * 1000;
+
+		if (devstate_sched_start < 1) {
+			devstate_sched_start = 1;
+		}
+
+		ast_mutex_lock(&refreshlock);
+		AST_SCHED_REPLACE(old_event->bs_start_sched, sched, devstate_sched_start, calendar_devstate_change, old_event);
+		ast_mutex_unlock(&refreshlock);
+		ast_debug(3, "Calendar bs_start event notification scheduled to happen in %ld ms\n", devstate_sched_start);
+	}
+
+	if (!cmp_event || old_event->end != event->end) {
+		changed = 1;
+		devstate_sched_end = (event->end - now.tv_sec) * 1000;
+		ast_mutex_lock(&refreshlock);
+		AST_SCHED_REPLACE(old_event->bs_end_sched, sched, devstate_sched_end, calendar_devstate_change, old_event);
+		ast_mutex_unlock(&refreshlock);
+		ast_debug(3, "Calendar bs_end event notification scheduled to happen in %ld ms\n", devstate_sched_end);
+	}
+
+	if (changed) {
+		ast_cond_signal(&refresh_condition);
+	}
+
+	ao2_unlock(event);
+
+	return 0;
+}
+
+static int merge_events_cb(void *obj, void *arg, int flags)
+{
+	struct ast_calendar_event *old_event = obj, *new_event;
+	struct ao2_container *new_events = arg;
+
+	/* If we don't find the old_event in new_events, then we can safely delete the old_event */
+	if (!(new_event = find_event(new_events, old_event->uid))) {
+		old_event = destroy_event(old_event);
+		return CMP_MATCH;
+	}
+
+	/* We have events to merge.  If any data that will affect a scheduler event has changed,
+	 * then we need to replace the scheduler event */
+	schedule_calendar_event(old_event->owner, old_event, new_event);
+
+	/* Since we don't want to mess with cancelling sched events and adding new ones, just
+	 * copy the internals of the new_event to the old_event */
+	copy_event_data(old_event, new_event);
+
+	/* Now we can go ahead and unlink the new_event from new_events and unref it so that only completely
+	 * new events remain in the container */
+	ao2_unlink(new_events, new_event);
+	new_event = ast_calendar_unref_event(new_event);
+
+	return 0;
+}
+
+static int add_new_event_cb(void *obj, void *arg, int flags)
+{
+	struct ast_calendar_event *new_event = obj;
+	struct ao2_container *events = arg;
+
+	ao2_link(events, new_event);
+	schedule_calendar_event(new_event->owner, new_event, NULL);
+	return CMP_MATCH;
+}
+
+void ast_calendar_merge_events(struct ast_calendar *cal, struct ao2_container *new_events)
+{
+	/* Loop through all events attached to the calendar.  If there is a matching new event
+	 * merge its data over and handle any schedule changes that need to be made.  Then remove
+	 * the new_event from new_events so that we are left with only new_events that we can add later. */
+	ao2_callback(cal->events, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, merge_events_cb, new_events);
+
+	/* Now, we should only have completely new events in new_events.  Loop through and add them */
+	ao2_callback(new_events, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, add_new_event_cb, cal->events);
+}
+
+
 static int load_config(void *data)
 {
 	const char *cat = NULL;
 	struct ast_flags config_flags = { 0 };
 	const char *val;
 
-	while (!ast_test_flag(&ast_options, AST_OPT_FLAG_FULLY_BOOTED)) {
-		sleep(1);
+	if (calendar_config) {
+		ast_config_destroy(calendar_config);
 	}
 
 	if (!(calendar_config = ast_config_load2("calendar.conf", "calendar", config_flags))) {
@@ -705,40 +723,13 @@
 			if (!(val = ast_variable_retrieve(calendar_config, cat, "enabled")) || !ast_true(val)) {
 				ast_debug(1, "Disabling calendar support\n");
 				ast_config_destroy(calendar_config);
+				calendar_config = NULL;
 				return 0;
 			} else {
 				ast_debug(1, "Enabling calendar support\n");
 			}
-		} else {
-			struct ast_calendar *cal;
-			const struct ast_calendar_tech *tech;
-			const char *val;
-
-			if (!(val = ast_variable_retrieve(calendar_config, cat, "type"))) {
-				ast_log(LOG_WARNING, "Calendar '%s' has no type, skipping!\n", cat);
-				continue;
-			}
-
-			if (!(tech = ast_get_calendar_tech(val))) {
-				ast_log(LOG_WARNING, "Calendar '%s' has unknown type '%s', skiping!\n", cat, val);
-				continue;
-			}
-
-			if (!(cal = build_calendar(calendar_config, cat, tech))) {
-				ast_config_destroy(calendar_config);
-				return -1;
-			}
-
-			ao2_link(calendars, cal);
-
-			/* Respsonsible for freeing load_data */
-			ast_pthread_create(&cal->thread, NULL, cal->tech->load_calendar, cal);
-
-			cal = unref_calendar(cal);
-		}
-	}
-
-	ast_config_destroy(calendar_config);
+		}
+	}
 
 	return 0;
 }
@@ -1380,9 +1371,20 @@
 
 int ast_calendar_reload()
 {
+	struct ast_calendar_tech *iter;
+
 	/* Delete all calendars */
 	ao2_callback(calendars, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, cb_true, NULL);
 	load_config(NULL);
+
+	AST_LIST_LOCK(&techs);
+	AST_LIST_TRAVERSE(&techs, iter, list) {
+		if (load_tech_calendars(iter)) {
+			ast_log(LOG_WARNING, "Failed to reload %s calendars, module disabled\n", iter->type);
+		}
+	}
+	AST_LIST_UNLOCK(&techs);
+
 	return 0;
 }
 
@@ -1412,8 +1414,6 @@
 
 int ast_calendar_init()
 {
-	struct ast_taskprocessor *load_task;
-
 	if (!(calendars = ao2_container_alloc(MAX_BUCKETS, calendar_hash_fn, calendar_cmp_fn))) {
 		ast_log(LOG_ERROR, "Unable to allocate calendars container!\n");
 		return -1;
@@ -1438,14 +1438,8 @@
 		ast_log(LOG_ERROR, "Unable to start refresh thread--notifications disabled!\n");
 	}
 
-	/* The calendar.conf file shold not be read until all modules are loaded
-	 * else the actual calendar res modules might not be loaded */
-	if (!(load_task = ast_taskprocessor_get("calendar_load", TPS_REF_DEFAULT))) {
-		ast_log(LOG_ERROR, "Could not create taskprocessor!\n");
-		return -1;
-	}
-	ast_taskprocessor_push(load_task, load_config, NULL);
-
 	ast_devstate_prov_add("Calendar", calendarstate);
+
+	load_config(NULL);
 	return 0;
 }

Modified: team/twilson/calendaring/res/res_caldav.c
URL: http://svn.digium.com/view/asterisk/team/twilson/calendaring/res/res_caldav.c?view=diff&rev=167018&r1=167017&r2=167018
==============================================================================
--- team/twilson/calendaring/res/res_caldav.c (original)
+++ team/twilson/calendaring/res/res_caldav.c Wed Dec 31 14:45:17 2008
@@ -521,7 +521,7 @@
 	ast_mutex_t refreshlock;
 
 	if (!(cal && calendar_config)) {
-		ast_log(LOG_ERROR, "Required parameters not provided\n");
+		ast_log(LOG_ERROR, "You must enable calendar support for res_caldav to load\n");
 		return NULL;
 	}
 
@@ -626,8 +626,12 @@
 static int load_module(void)
 {
 	ne_sock_init();
-	ast_calendar_register(&caldav_tech);
-	return 0;
+	if (ast_calendar_register(&caldav_tech)) {
+		ne_sock_exit();
+		return AST_MODULE_LOAD_DECLINE;
+	}
+
+	return AST_MODULE_LOAD_SUCCESS;
 }
 
 static int unload_module(void)

Modified: team/twilson/calendaring/res/res_exchangecal.c
URL: http://svn.digium.com/view/asterisk/team/twilson/calendaring/res/res_exchangecal.c?view=diff&rev=167018&r1=167017&r2=167018
==============================================================================
--- team/twilson/calendaring/res/res_exchangecal.c (original)
+++ team/twilson/calendaring/res/res_exchangecal.c Wed Dec 31 14:45:17 2008
@@ -620,7 +620,7 @@
 	ast_mutex_t refreshlock;
 
 	if (!(cal && calendar_config)) {
-		ast_log(LOG_ERROR, "Required parameters not provided\n");
+		ast_log(LOG_ERROR, "You must enable calendar support for res_exchangecal to load\n");
 		return NULL;
 	}
 
@@ -725,8 +725,12 @@
 static int load_module(void)
 {
 	ne_sock_init();
-	ast_calendar_register(&exchangecal_tech);
-	return 0;
+	if (ast_calendar_register(&exchangecal_tech)) {
+		ne_sock_exit();
+		return AST_MODULE_LOAD_DECLINE;
+	}
+
+	return AST_MODULE_LOAD_SUCCESS;
 }
 
 static int unload_module(void)

Modified: team/twilson/calendaring/res/res_icalendar.c
URL: http://svn.digium.com/view/asterisk/team/twilson/calendaring/res/res_icalendar.c?view=diff&rev=167018&r1=167017&r2=167018
==============================================================================
--- team/twilson/calendaring/res/res_icalendar.c (original)
+++ team/twilson/calendaring/res/res_icalendar.c Wed Dec 31 14:45:17 2008
@@ -316,7 +316,7 @@
 	ast_mutex_t refreshlock;
 
 	if (!(cal && calendar_config)) {
-		ast_log(LOG_ERROR, "Required parameters not provided\n");
+		ast_log(LOG_ERROR, "You must enable calendar support for res_icalendar to load\n");
 		return NULL;
 	}
 
@@ -430,8 +430,12 @@
 static int load_module(void)
 {
 	ne_sock_init();
-	ast_calendar_register(&ical_tech);
-	return 0;
+	if (ast_calendar_register(&ical_tech)) {
+		ne_sock_exit();
+		return AST_MODULE_LOAD_DECLINE;
+	}
+
+	return AST_MODULE_LOAD_SUCCESS;
 }
 
 static int unload_module(void)




More information about the svn-commits mailing list