Minor fix in interface.conf example
[lcr.git] / joinremote.cpp
1 /*****************************************************************************\
2 **                                                                           **
3 ** Linux Call Router                                                         **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** join functions for remote application                                     **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 #include "main.h"
13 //#define __u8 unsigned char
14 //#define __u16 unsigned short
15 //#define __u32 unsigned int
16
17 extern unsigned int new_remote;
18
19 /*
20  * constructor for a new join 
21  * the join will have a relation to the calling endpoint
22  */
23 JoinRemote::JoinRemote(unsigned int serial, char *remote_name, int remote_id) : Join()
24 {
25         union parameter param;
26
27         SCPY(j_remote_name, remote_name);
28         j_remote_id = remote_id;
29         j_type = JOIN_TYPE_REMOTE;
30         j_remote_ref = new_remote++;
31
32         PDEBUG(DEBUG_JOIN, "Constructor(new join) ref=%d\n", j_remote_ref);
33
34         j_epoint_id = serial; /* this is the endpoint, if created by epoint */
35         if (j_epoint_id)
36                 PDEBUG(DEBUG_JOIN, "New remote join connected to endpoint id %lu and application %s (ref=%d)\n", j_epoint_id, remote_name, j_remote_ref);
37
38         /* send new ref to remote socket */
39         memset(&param, 0, sizeof(union parameter));
40         if (serial)
41                 param.newref.direction = 1; /* new ref from lcr */
42         if (admin_message_from_lcr(j_remote_id, j_remote_ref, MESSAGE_NEWREF, &param)<0)
43                 FATAL("No socket with remote application '%s' found, this shall not happen. because we already created one.\n", j_remote_name);
44 }
45
46
47 /*
48  * join descructor
49  */
50 JoinRemote::~JoinRemote()
51 {
52 }
53
54 void JoinRemote::message_epoint(unsigned int epoint_id, int message_type, union parameter *param)
55 {
56         /* if endpoint has just been removed, but still a message in the que */
57         if (epoint_id != j_epoint_id)
58                 return;
59         
60         PDEBUG(DEBUG_JOIN, "Message %d of endpoint %d from LCR to remote (ref=%d)\n", message_type, j_epoint_id, j_remote_ref);
61
62         /* look for Remote's interface */
63         if (admin_message_from_lcr(j_remote_id, j_remote_ref, message_type, param)<0) {
64                 PERROR("No socket with remote application '%s' found, this shall not happen. Closing socket shall cause release of all joins.\n", j_remote_name);
65                 return;         
66         }
67
68         if (message_type == MESSAGE_RELEASE) {
69                 delete this;
70                 return;
71         }
72 }
73
74 void JoinRemote::message_remote(int message_type, union parameter *param)
75 {
76         struct lcr_msg *message;
77
78         PDEBUG(DEBUG_JOIN, "Message %d of endpoint %d from remote to LCR (ref=%d)\n", message_type, j_epoint_id, j_remote_ref);
79
80         /* create relation if no relation exists */
81         if (!j_epoint_id) {
82                 class Endpoint          *epoint;
83
84                 if (!(epoint = new Endpoint(0, j_serial)))
85                         FATAL("No memory for Endpoint instance\n");
86                 j_epoint_id = epoint->ep_serial;
87                 PDEBUG(DEBUG_JOIN, "Created endpoint %d\n", j_epoint_id);
88                 epoint->ep_app = new_endpointapp(epoint, 1, EAPP_TYPE_PBX); // outgoing
89         }
90
91         /* set serial on bchannel message
92          * also ref is given, so we send message with ref */
93         if (message_type == MESSAGE_BCHANNEL) {
94                 message_bchannel_from_remote(this, param->bchannel.type, param->bchannel.handle);
95                 return;
96         }
97         
98         /* cannot just forward, because param is not of container "struct lcr_msg" */
99         message = message_create(j_serial, j_epoint_id, JOIN_TO_EPOINT, message_type);
100         memcpy(&message->param, param, sizeof(message->param));
101         message_put(message);
102
103         if (message_type == MESSAGE_RELEASE) {
104                 delete this;
105                 return;
106         }
107 }
108
109
110