Exemplo n.º 1
0
void app_update(struct stasis_app *app, stasis_app_cb handler, void *data)
{
	SCOPED_AO2LOCK(lock, app);

	if (app->handler) {
		RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);

		ast_verb(1, "Replacing Stasis app '%s'\n", app->name);

		msg = ast_json_pack("{s: s, s: s}",
			"type", "ApplicationReplaced",
			"application", app->name);
		if (msg) {
			app_send(app, msg);
		}
	} else {
		ast_verb(1, "Activating Stasis app '%s'\n", app->name);
	}

	app->handler = handler;
	ao2_cleanup(app->data);
	if (data) {
		ao2_ref(data, +1);
	}
	app->data = data;
}
Exemplo n.º 2
0
static void sub_bridge_update_handler(void *data,
	struct stasis_subscription *sub,
	struct stasis_message *message)
{
	struct ast_json *json = NULL;
	struct stasis_app *app = data;
	struct ast_bridge_snapshot_update *update;
	const struct timeval *tv;

	update = stasis_message_data(message);

	tv = stasis_message_timestamp(message);

	if (!update->new_snapshot) {
		json = simple_bridge_event("BridgeDestroyed", update->old_snapshot, tv);
	} else if (!update->old_snapshot) {
		json = simple_bridge_event("BridgeCreated", update->new_snapshot, tv);
	} else if (update->new_snapshot && update->old_snapshot
		&& strcmp(update->new_snapshot->video_source_id, update->old_snapshot->video_source_id)) {
		json = simple_bridge_event("BridgeVideoSourceChanged", update->new_snapshot, tv);
		if (json && !ast_strlen_zero(update->old_snapshot->video_source_id)) {
			ast_json_object_set(json, "old_video_source_id",
				ast_json_string_create(update->old_snapshot->video_source_id));
		}
	}

	if (json) {
		app_send(app, json);
		ast_json_unref(json);
	}

	if (!update->new_snapshot && update->old_snapshot) {
		unsubscribe(app, "bridge", update->old_snapshot->uniqueid, 1);
	}
}
Exemplo n.º 3
0
void *sender_t(void* data) //@ : pthread_run_joinable
  //@ requires pthread_run_pre(sender_t)(data, ?x);
  //@ ensures pthread_run_post(sender_t)(data, x) &*& result == 0;
{
  //@ open pthread_run_pre(sender_t)(data, _);
  struct ss_auth_args *args = (void*) data;
  struct item *key = args->key;
  //@ assert principal(?principal, ?count);
  //@ item n = nonce_item(principal, count + 1, 0);
  //@ close ss_auth_pub(n);
  //@ leak ss_auth_pub(n);
  int i = random_int();
  struct item *mess_authage = create_data_item((void*) &i, (int) sizeof(int));
  //@ chars_to_integer(&i);
  //@ assert item(key, private_key_item(?sender, _), ss_auth_pub);
  //@ item datai = data_item(chars_of_int(i));
  //@ assume (app_send_event(sender, datai));
  //@ assert [_]world(ss_auth_pub, ss_auth_key_clsfy);
  //@ close ss_auth_pub(datai);
  //@ leak ss_auth_pub(datai);
  app_send(key, mess_authage);
  //@ close pthread_run_post(sender_t)(data, _);
  item_free(mess_authage);
  return 0;
}
Exemplo n.º 4
0
static void sub_endpoint_update_handler(void *data,
	struct stasis_subscription *sub,
	struct stasis_message *message)
{
	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
	struct stasis_app *app = data;
	struct stasis_cache_update *update;
	struct ast_endpoint_snapshot *new_snapshot;
	struct ast_endpoint_snapshot *old_snapshot;
	const struct timeval *tv;

	ast_assert(stasis_message_type(message) == stasis_cache_update_type());

	update = stasis_message_data(message);

	ast_assert(update->type == ast_endpoint_snapshot_type());

	new_snapshot = stasis_message_data(update->new_snapshot);
	old_snapshot = stasis_message_data(update->old_snapshot);

	if (new_snapshot) {
		tv = stasis_message_timestamp(update->new_snapshot);

		json = simple_endpoint_event("EndpointStateChange", new_snapshot, tv);
		if (!json) {
			return;
		}

		app_send(app, json);
	}

	if (!new_snapshot && old_snapshot) {
		unsubscribe(app, "endpoint", old_snapshot->id, 1);
	}
}
Exemplo n.º 5
0
void *sender_t(void* data) //@ : pthread_run_joinable
  //@ requires pthread_run_pre(sender_t)(data, ?info);
  //@ ensures pthread_run_post(sender_t)(data, info) &*& result == 0;
{
  //@ open pthread_run_pre(sender_t)(data, _);
  struct ss_args *args = data;
  app_send(args->key, args->key_len, args->message, args->message_len);
  //@ close pthread_run_post(sender_t)(data, _);
  return 0;
}
Exemplo n.º 6
0
static void sub_bridge_update_handler(void *data,
	struct stasis_subscription *sub,
	struct stasis_message *message)
{
	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
	struct stasis_app *app = data;
	struct stasis_cache_update *update;
	struct ast_bridge_snapshot *new_snapshot;
	struct ast_bridge_snapshot *old_snapshot;
	const struct timeval *tv;

	ast_assert(stasis_message_type(message) == stasis_cache_update_type());

	update = stasis_message_data(message);

	ast_assert(update->type == ast_bridge_snapshot_type());

	new_snapshot = stasis_message_data(update->new_snapshot);
	old_snapshot = stasis_message_data(update->old_snapshot);
	tv = update->new_snapshot ?
		stasis_message_timestamp(update->new_snapshot) :
		stasis_message_timestamp(message);

	if (!new_snapshot) {
		json = simple_bridge_event("BridgeDestroyed", old_snapshot, tv);
	} else if (!old_snapshot) {
		json = simple_bridge_event("BridgeCreated", new_snapshot, tv);
	} else if (new_snapshot && old_snapshot
		&& strcmp(new_snapshot->video_source_id, old_snapshot->video_source_id)) {
		json = simple_bridge_event("BridgeVideoSourceChanged", new_snapshot, tv);
		if (json && !ast_strlen_zero(old_snapshot->video_source_id)) {
			ast_json_object_set(json, "old_video_source_id",
				ast_json_string_create(old_snapshot->video_source_id));
		}
	}

	if (json) {
		app_send(app, json);
	}

	if (!new_snapshot && old_snapshot) {
		unsubscribe(app, "bridge", old_snapshot->uniqueid, 1);
	}
}
Exemplo n.º 7
0
static int message_received_handler(const char *endpoint_id, struct ast_json *json_msg, void *pvt)
{
	struct ast_endpoint_snapshot *snapshot;
	struct ast_json *json_endpoint;
	struct ast_json *message;
	struct stasis_app *app = pvt;
	char *tech;
	char *resource;

	tech = ast_strdupa(endpoint_id);
	resource = strchr(tech, '/');
	if (resource) {
		resource[0] = '\0';
		resource++;
	}

	if (ast_strlen_zero(tech) || ast_strlen_zero(resource)) {
		return -1;
	}

	snapshot = ast_endpoint_latest_snapshot(tech, resource);
	if (!snapshot) {
		return -1;
	}

	json_endpoint = ast_endpoint_snapshot_to_json(snapshot, stasis_app_get_sanitizer());
	ao2_ref(snapshot, -1);
	if (!json_endpoint) {
		return -1;
	}

	message = ast_json_pack("{s: s, s: o, s: o, s: o}",
		"type", "TextMessageReceived",
		"timestamp", ast_json_timeval(ast_tvnow(), NULL),
		"endpoint", json_endpoint,
		"message", ast_json_ref(json_msg));
	if (message) {
		app_send(app, message);
		ast_json_unref(message);
	}

	return 0;
}
Exemplo n.º 8
0
static void sub_default_handler(void *data, struct stasis_subscription *sub,
	struct stasis_message *message)
{
	struct stasis_app *app = data;
	struct ast_json *json;

	/* The dial type can be converted to JSON so it will always be passed
	 * here.
	 */
	if (stasis_message_type(message) == ast_channel_dial_type()) {
		call_forwarded_handler(app, message);
	}

	/* By default, send any message that has a JSON representation */
	json = stasis_message_to_json(message, stasis_app_get_sanitizer());
	if (!json) {
		return;
	}

	app_send(app, json);
	ast_json_unref(json);
}
Exemplo n.º 9
0
static void sub_channel_update_handler(void *data,
	struct stasis_subscription *sub,
	struct stasis_message *message)
{
	struct stasis_app *app = data;
	struct stasis_cache_update *update;
	struct ast_channel_snapshot *new_snapshot;
	struct ast_channel_snapshot *old_snapshot;
	const struct timeval *tv;
	int i;

	ast_assert(stasis_message_type(message) == stasis_cache_update_type());

	update = stasis_message_data(message);

	ast_assert(update->type == ast_channel_snapshot_type());

	new_snapshot = stasis_message_data(update->new_snapshot);
	old_snapshot = stasis_message_data(update->old_snapshot);

	/* Pull timestamp from the new snapshot, or from the update message
	 * when there isn't one. */
	tv = update->new_snapshot ?
		stasis_message_timestamp(update->new_snapshot) :
		stasis_message_timestamp(message);

	for (i = 0; i < ARRAY_LEN(channel_monitors); ++i) {
		RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);

		msg = channel_monitors[i](old_snapshot, new_snapshot, tv);
		if (msg) {
			app_send(app, msg);
		}
	}

	if (!new_snapshot && old_snapshot) {
		unsubscribe(app, "channel", old_snapshot->uniqueid, 1);
	}
}
Exemplo n.º 10
0
static void sub_default_handler(void *data, struct stasis_subscription *sub,
	struct stasis_message *message)
{
	struct stasis_app *app = data;
	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);

	if (stasis_subscription_final_message(sub, message)) {
		ao2_cleanup(app);
	}

	if (stasis_message_type(message) == ast_channel_dial_type()) {
		call_forwarded_handler(app, message);
	}

	/* By default, send any message that has a JSON representation */
	json = stasis_message_to_json(message, stasis_app_get_sanitizer());
	if (!json) {
		return;
	}

	app_send(app, json);
}
Exemplo n.º 11
0
void *sender_t(void* data) //@ : pthread_run_joinable
  //@ requires pthread_run_pre(sender_t)(data, ?x);
  //@ ensures pthread_run_post(sender_t)(data, x) &*& result == 0;
{
  //@ open pthread_run_pre(sender_t)(data, _);
  struct ss_args *args = (void*) data;
  struct item *key = args->key;
  int i = random_int();
  struct item *message = create_data_item((void*) &i, (int) sizeof(int));
  //@ chars_to_integer(&i);
  //@ assert item(key, symmetric_key_item(?sender, _), ss_pub);
  //@ item datai = data_item(chars_of_int(i));
  //@ assume (app_send_event(sender, datai));
  //@ assert [_]world(ss_pub);
  //@ get_info_for_item(datai);
  //@ close ss_pub(datai);
  //@ leak ss_pub(datai);
  app_send(key, message);
  //@ close pthread_run_post(sender_t)(data, _);
  item_free(message);
  return 0;
}
Exemplo n.º 12
0
static void sub_channel_update_handler(void *data,
	struct stasis_subscription *sub,
	struct stasis_message *message)
{
	struct stasis_app *app = data;
	struct ast_channel_snapshot_update *update = stasis_message_data(message);
	int i;

	for (i = 0; i < ARRAY_LEN(channel_monitors); ++i) {
		struct ast_json *msg;

		msg = channel_monitors[i](update->old_snapshot, update->new_snapshot,
			stasis_message_timestamp(message));
		if (msg) {
			app_send(app, msg);
			ast_json_unref(msg);
		}
	}

	if (ast_test_flag(&update->new_snapshot->flags, AST_FLAG_DEAD)) {
		unsubscribe(app, "channel", update->new_snapshot->base->uniqueid, 1);
	}
}
Exemplo n.º 13
0
void app_update(struct stasis_app *app, stasis_app_cb handler, void *data)
{
	ao2_lock(app);
	if (app->handler && app->data) {
		struct ast_json *msg;

		ast_verb(1, "Replacing Stasis app '%s'\n", app->name);

		msg = ast_json_pack("{s: s, s: s}",
			"type", "ApplicationReplaced",
			"application", app->name);
		if (msg) {
			app_send(app, msg);
			ast_json_unref(msg);
		}
	} else {
		ast_verb(1, "Activating Stasis app '%s'\n", app->name);
	}

	app->handler = handler;
	ao2_replace(app->data, data);
	ao2_unlock(app);
}