struct message -> struct lcr_msg
[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 <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15 //#include <unistd.h>
16 //#include <poll.h>
17 //#include <sys/types.h>
18 //#include <sys/stat.h>
19 //#include <fcntl.h>
20 #include "main.h"
21 //#define __u8 unsigned char
22 //#define __u16 unsigned short
23 //#define __u32 unsigned long
24 //#include "linux/isdnif.h"
25
26
27 /*
28  * constructor for a new join 
29  * the join will have a relation to the calling endpoint
30  */
31 JoinRemote::JoinRemote(unsigned long serial, char *remote_name, int remote_id) : Join()
32 {
33         PDEBUG(DEBUG_JOIN, "Constructor(new join)");
34         union parameter param;
35
36         SCPY(j_remote_name, remote_name);
37         j_remote_id = remote_id;
38         j_type = JOIN_TYPE_REMOTE;
39
40         j_epoint_id = serial; /* this is the endpoint, if created by epoint */
41         if (j_epoint_id)
42                 PDEBUG(DEBUG_JOIN, "New remote join connected to endpoint id %lu and application %s\n", j_epoint_id, remote_name);
43
44         /* send new ref to remote socket */
45         memset(&param, 0, sizeof(union parameter));
46         if (serial)
47                 param.direction = 1; /* new ref from lcr */
48         /* the j_serial is assigned by Join() parent. this is sent as new ref */
49         if (admin_message_from_join(j_remote_id, j_serial, MESSAGE_NEWREF, &param)<0)
50                 FATAL("No socket with remote application '%s' found, this shall not happen. because we already created one.\n", j_remote_name);
51 }
52
53
54 /*
55  * join descructor
56  */
57 JoinRemote::~JoinRemote()
58 {
59 }
60
61
62 /* join process is called from the main loop
63  * it processes the current calling state.
64  * returns 0 if join nothing was done
65  */
66 int JoinRemote::handler(void)
67 {
68         return(0);
69 }
70
71
72 void JoinRemote::message_epoint(unsigned long epoint_id, int message_type, union parameter *param)
73 {
74         /* if endpoint has just been removed, but still a message in the que */
75         if (epoint_id != j_epoint_id)
76                 return;
77         
78         /* look for Remote's interface */
79         if (admin_message_from_join(j_remote_id, j_serial, message_type, param)<0)
80         {
81                 PERROR("No socket with remote application '%s' found, this shall not happen. Closing socket shall cause release of all joins.\n", j_remote_name);
82                 return;         
83         }
84
85         if (message_type == MESSAGE_RELEASE)
86         {
87                 delete this;
88                 return;
89         }
90 }
91
92 void JoinRemote::message_remote(int message_type, union parameter *param)
93 {
94         struct lcr_msg *message;
95
96         /* create relation if no relation exists */
97         if (!j_epoint_id)
98         {
99                 class Endpoint          *epoint;
100
101                 if (!(epoint = new Endpoint(0, j_serial)))
102                         FATAL("No memory for Endpoint instance\n");
103                 j_epoint_id = epoint->ep_serial;
104                 if (!(epoint->ep_app = new DEFAULT_ENDPOINT_APP(epoint, 1))) // outgoing
105                         FATAL("No memory for Endpoint Application instance\n");
106         }
107
108         /* set serial on bchannel message
109          * also ref is given, so we send message with ref */
110         if (message_type == MESSAGE_BCHANNEL)
111         {
112                 message_bchannel_from_join(this, param->bchannel.type, param->bchannel.handle);
113                 return;
114         }
115         
116         /* cannot just forward, because param is not of container "struct lcr_msg" */
117         message = message_create(j_serial, j_epoint_id, JOIN_TO_EPOINT, message_type);
118         memcpy(&message->param, param, sizeof(message->param));
119         message_put(message);
120
121         if (message_type == MESSAGE_RELEASE)
122         {
123                 delete this;
124                 return;
125         }
126 }
127
128 void message_bchannel_to_join(unsigned long remote_id, unsigned long ref, int type, unsigned long handle, int tx_gain, int rx_gain, char *pipeline, unsigned char *crypt, int crypt_len, int crypt_type)
129 {
130         union parameter param;
131
132         memset(&param, 0, sizeof(union parameter));
133         param.bchannel.type = type;
134         param.bchannel.handle = handle;
135         param.bchannel.tx_gain = tx_gain;
136         param.bchannel.rx_gain = rx_gain;
137         if (pipeline)
138                 SCPY(param.bchannel.pipeline, pipeline);
139         if (crypt_len)
140                 memcpy(param.bchannel.crypt, crypt, crypt_len);
141         param.bchannel.crypt_type = crypt_type;
142         if (admin_message_from_join(remote_id, ref, MESSAGE_BCHANNEL, &param)<0)
143         {
144                 PERROR("No socket with remote id %d found, this happens, if the socket is closed before all bchannels are imported.\n", remote_id);
145                 return;         
146         }
147 }
148
149
150