1 /*
2  * mod_rayo for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3  * Copyright (C) 2013-2018, Grasshopper
4  *
5  * Version: MPL 1.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is mod_rayo for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
18  *
19  * The Initial Developer of the Original Code is Grasshopper
20  * Portions created by the Initial Developer are Copyright (C)
21  * the Initial Developer. All Rights Reserved.
22  *
23  * Contributor(s):
24  * Chris Rienzo <chris.rienzo@grasshopper.com>
25  *
26  * mod_rayo.h -- Rayo server / node implementation.  Allows MxN clustering of FreeSWITCH and Rayo Clients (like Adhearsion)
27  *
28  */
29 #ifndef MOD_RAYO_H
30 #define MOD_RAYO_H
31 
32 #include <switch.h>
33 #include <iksemel.h>
34 
35 #include "iks_helpers.h"
36 
37 #define RAYO_VERSION "1"
38 #define RAYO_BASE "urn:xmpp:rayo:"
39 
40 #define RAYO_NS RAYO_BASE RAYO_VERSION
41 #define RAYO_CLIENT_NS RAYO_BASE "client:" RAYO_VERSION
42 #define RAYO_CALL_NS RAYO_BASE "call:" RAYO_VERSION
43 #define RAYO_MIXER_NS RAYO_BASE "mixer:" RAYO_VERSION
44 
45 #define RAYO_CPA_BASE RAYO_BASE "cpa:"
46 #define RAYO_CPA_NS RAYO_CPA_BASE RAYO_VERSION
47 
48 #define RAT_CALL "CALL"
49 #define RAT_COMPONENT "COMPONENT"
50 #define RAT_CALL_COMPONENT RAT_COMPONENT"_CALL"
51 #define RAT_MIXER "MIXER"
52 #define RAT_MIXER_COMPONENT RAT_COMPONENT"_MIXER"
53 #define RAT_SERVER "SERVER"
54 #define RAT_PEER_SERVER "PEER_SERVER"
55 #define RAT_CLIENT "CLIENT"
56 
57 struct rayo_actor;
58 struct rayo_call;
59 struct rayo_mixer;
60 struct rayo_component;
61 
62 /**
63  * A message sent to an actor
64  */
65 struct rayo_message {
66 	iks *payload;
67 	char *to_jid;
68 	iksid *to;
69 	char *from_jid;
70 	iksid *from;
71 	char *from_type;
72 	char *from_subtype;
73 	int is_reply;
74 	char *file;
75 	int line;
76 };
77 
78 typedef void (* rayo_actor_cleanup_fn)(struct rayo_actor *);
79 typedef void (* rayo_actor_send_fn)(struct rayo_actor *, struct rayo_message *);
80 
81 /**
82  * A rayo actor - this is an entity that can be controlled by a rayo client
83  */
84 struct rayo_actor {
85 	/** Type of actor */
86 	char *type;
87 	/** Sub-type of actor */
88 	char *subtype;
89 	/** domain part of JID */
90 	char *domain;
91 	/** Internal ID */
92 	char *id;
93 	/** actor JID */
94 	char *jid;
95 	/** Actor pool */
96 	switch_memory_pool_t *pool;
97 	/** synchronizes access to this actor */
98 	switch_mutex_t *mutex;
99 	/** an atomically incrementing sequence for this actor */
100 	int seq;
101 	/** number of users of this actor */
102 	int ref_count;
103 	/** destroy flag */
104 	int destroy;
105 	/** XMPP message handling function */
106 	rayo_actor_send_fn send_fn;
107 	/** optional cleanup */
108 	rayo_actor_cleanup_fn cleanup_fn;
109 	/** optional parent */
110 	struct rayo_actor *parent;
111 };
112 
113 /**
114  * A Rayo component
115  */
116 struct rayo_component {
117 	/** base actor class */
118 	struct rayo_actor base;
119 	/** owning client JID */
120 	const char *client_jid;
121 	/** external ref */
122 	const char *ref;
123 	/** true if component has completed */
124 	int complete;
125 };
126 
127 #define RAYO_ACTOR(x) ((struct rayo_actor *)x)
128 #define RAYO_COMPONENT(x) ((struct rayo_component *)x)
129 #define RAYO_CALL(x) ((struct rayo_call *)x)
130 #define RAYO_MIXER(x) ((struct rayo_mixer *)x)
131 
132 SWITCH_DECLARE(void) rayo_message_send(struct rayo_actor *from, const char *to, iks *payload, int dup, int reply, const char *file, int line);
133 SWITCH_DECLARE(void) rayo_message_destroy(struct rayo_message *msg);
134 SWITCH_DECLARE(iks *) rayo_message_remove_payload(struct rayo_message *msg);
135 #define RAYO_SEND_MESSAGE(from, to, payload) rayo_message_send(RAYO_ACTOR(from), to, payload, 0, 0, __FILE__, __LINE__)
136 #define RAYO_SEND_MESSAGE_DUP(from, to, payload) rayo_message_send(RAYO_ACTOR(from), to, payload, 1, 0, __FILE__, __LINE__)
137 #define RAYO_SEND_REPLY(from, to, payload) rayo_message_send(RAYO_ACTOR(from), to, payload, 0, 1, __FILE__, __LINE__)
138 #define RAYO_SEND_REPLY_DUP(from, to, payload) rayo_message_send(RAYO_ACTOR(from), to, payload, 1, 1, __FILE__, __LINE__)
139 
140 SWITCH_DECLARE(struct rayo_actor *) rayo_actor_locate(const char *jid, const char *file, int line);
141 SWITCH_DECLARE(struct rayo_actor *) rayo_actor_locate_by_id(const char *id, const char *file, int line);
142 SWITCH_DECLARE(int) rayo_actor_seq_next(struct rayo_actor *actor);
143 SWITCH_DECLARE(void) rayo_actor_retain(struct rayo_actor *actor, const char *file, int line);
144 SWITCH_DECLARE(void) rayo_actor_release(struct rayo_actor *actor, const char *file, int line);
145 SWITCH_DECLARE(void) rayo_actor_destroy(struct rayo_actor *actor, const char *file, int line);
146 
147 #define RAYO_LOCATE(jid) rayo_actor_locate(jid, __FILE__, __LINE__)
148 #define RAYO_LOCATE_BY_ID(id) rayo_actor_locate_by_id(id, __FILE__, __LINE__)
149 #define RAYO_SET_EVENT_FN(actor, event) rayo_actor_set_event_fn(RAYO_ACTOR(actor), event)
150 #define RAYO_DOMAIN(x) RAYO_ACTOR(x)->domain
151 #define RAYO_JID(x) RAYO_ACTOR(x)->jid
152 #define RAYO_ID(x) RAYO_ACTOR(x)->id
153 #define RAYO_POOL(x) RAYO_ACTOR(x)->pool
154 #define RAYO_RETAIN(x) rayo_actor_retain(RAYO_ACTOR(x), __FILE__, __LINE__)
155 #define RAYO_RELEASE(x) rayo_actor_release(RAYO_ACTOR(x), __FILE__, __LINE__)
156 #define RAYO_DESTROY(x) rayo_actor_destroy(RAYO_ACTOR(x), __FILE__, __LINE__)
157 #define RAYO_SEQ_NEXT(x) rayo_actor_seq_next(RAYO_ACTOR(x))
158 
159 SWITCH_DECLARE(int) rayo_call_is_joined(struct rayo_call *call);
160 SWITCH_DECLARE(int) rayo_call_is_faxing(struct rayo_call *call);
161 SWITCH_DECLARE(void) rayo_call_set_faxing(struct rayo_call *call, int faxing);
162 SWITCH_DECLARE(const char *) rayo_call_get_dcp_jid(struct rayo_call *call);
163 
164 #define rayo_mixer_get_name(mixer) RAYO_ID(mixer)
165 
166 #define rayo_component_init(component, pool, type, subtype, id, parent, client_jid) _rayo_component_init(component, pool, type, subtype, id, parent, client_jid, NULL, __FILE__, __LINE__)
167 #define rayo_component_init_cleanup(component, pool, type, subtype, id, parent, client_jid, cleanup) _rayo_component_init(component, pool, type, subtype, id, parent, client_jid, cleanup, __FILE__, __LINE__)
168 SWITCH_DECLARE(struct rayo_component *) _rayo_component_init(struct rayo_component *component, switch_memory_pool_t *pool, const char *type, const char *subtype, const char *id, struct rayo_actor *parent, const char *client_jid, rayo_actor_cleanup_fn cleanup, const char *file, int line);
169 SWITCH_DECLARE(switch_bool_t) is_component_actor(struct rayo_actor *);
170 
171 typedef iks *(*rayo_actor_xmpp_handler)(struct rayo_actor *, struct rayo_message *, void *);
172 SWITCH_DECLARE(void) rayo_actor_command_handler_add(const char *type, const char *subtype, const char *name, rayo_actor_xmpp_handler fn);
173 SWITCH_DECLARE(void) rayo_actor_event_handler_add(const char *from_type, const char *from_subtype, const char *to_type, const char *to_subtype, const char *name, rayo_actor_xmpp_handler fn);
174 
175 #endif
176 
177 
178 /* For Emacs:
179  * Local Variables:
180  * mode:c
181  * indent-tabs-mode:t
182  * tab-width:4
183  * c-basic-offset:4
184  * End:
185  * For VIM:
186  * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet
187  */
188