Updated debugging patch.
[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
18 /*
19  * constructor for a new join 
20  * the join will have a relation to the calling endpoint
21  */
22 JoinRemote::JoinRemote(unsigned int serial, char *remote_name, int remote_id) : Join()
23 {
24         PDEBUG(DEBUG_JOIN, "Constructor(new join)");
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
31         j_epoint_id = serial; /* this is the endpoint, if created by epoint */
32         if (j_epoint_id)
33                 PDEBUG(DEBUG_JOIN, "New remote join connected to endpoint id %lu and application %s\n", j_epoint_id, remote_name);
34
35         /* send new ref to remote socket */
36         memset(&param, 0, sizeof(union parameter));
37         if (serial)
38                 param.direction = 1; /* new ref from lcr */
39         /* the j_serial is assigned by Join() parent. this is sent as new ref */
40         if (admin_message_from_join(j_remote_id, j_serial, MESSAGE_NEWREF, &param)<0)
41                 FATAL("No socket with remote application '%s' found, this shall not happen. because we already created one.\n", j_remote_name);
42 }
43
44
45 /*
46  * join descructor
47  */
48 JoinRemote::~JoinRemote()
49 {
50 }
51
52 void JoinRemote::message_epoint(unsigned int epoint_id, int message_type, union parameter *param)
53 {
54         /* if endpoint has just been removed, but still a message in the que */
55         if (epoint_id != j_epoint_id)
56                 return;
57         
58         /* look for Remote's interface */
59         if (admin_message_from_join(j_remote_id, j_serial, message_type, param)<0) {
60                 PERROR("No socket with remote application '%s' found, this shall not happen. Closing socket shall cause release of all joins.\n", j_remote_name);
61                 return;         
62         }
63
64         if (message_type == MESSAGE_RELEASE) {
65                 delete this;
66                 return;
67         }
68 }
69
70 void JoinRemote::message_remote(int message_type, union parameter *param)
71 {
72         struct lcr_msg *message;
73
74         /* create relation if no relation exists */
75         if (!j_epoint_id) {
76                 class Endpoint          *epoint;
77
78                 if (!(epoint = new Endpoint(0, j_serial)))
79                         FATAL("No memory for Endpoint instance\n");
80                 j_epoint_id = epoint->ep_serial;
81                 if (!(epoint->ep_app = new DEFAULT_ENDPOINT_APP(epoint, 1))) // outgoing
82                         FATAL("No memory for Endpoint Application instance\n");
83         }
84
85         /* set serial on bchannel message
86          * also ref is given, so we send message with ref */
87         if (message_type == MESSAGE_BCHANNEL) {
88                 message_bchannel_from_remote(this, param->bchannel.type, param->bchannel.handle);
89                 return;
90         }
91         
92         /* cannot just forward, because param is not of container "struct lcr_msg" */
93         message = message_create(j_serial, j_epoint_id, JOIN_TO_EPOINT, message_type);
94         memcpy(&message->param, param, sizeof(message->param));
95         message_put(message);
96
97         if (message_type == MESSAGE_RELEASE) {
98                 delete this;
99                 return;
100         }
101 }
102
103 void message_bchannel_to_remote(unsigned int remote_id, unsigned int ref, int type, unsigned int handle, int tx_gain, int rx_gain, char *pipeline, unsigned char *crypt, int crypt_len, int crypt_type)
104 {
105         union parameter param;
106
107         memset(&param, 0, sizeof(union parameter));
108         param.bchannel.type = type;
109         param.bchannel.handle = handle;
110         param.bchannel.tx_gain = tx_gain;
111         param.bchannel.rx_gain = rx_gain;
112         if (pipeline)
113                 SCPY(param.bchannel.pipeline, pipeline);
114         if (crypt_len)
115                 memcpy(param.bchannel.crypt, crypt, crypt_len);
116         param.bchannel.crypt_type = crypt_type;
117         if (admin_message_from_join(remote_id, ref, MESSAGE_BCHANNEL, &param)<0) {
118                 PERROR("No socket with remote id %d found, this happens, if the socket is closed before all bchannels are imported.\n", remote_id);
119                 return;         
120         }
121 }
122
123
124