c02de8e4a70c406505ee281647c0f40d4f49f346
[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         PDEBUG(DEBUG_JOIN, "Constructor(new join)");
26         union parameter param;
27
28         SCPY(j_remote_name, remote_name);
29         j_remote_id = remote_id;
30         j_type = JOIN_TYPE_REMOTE;
31         j_remote_ref = new_remote++;
32
33         j_epoint_id = serial; /* this is the endpoint, if created by epoint */
34         if (j_epoint_id)
35                 PDEBUG(DEBUG_JOIN, "New remote join connected to endpoint id %lu and application %s\n", j_epoint_id, remote_name);
36
37         /* send new ref to remote socket */
38         memset(&param, 0, sizeof(union parameter));
39         if (serial)
40                 param.newref.direction = 1; /* new ref from lcr */
41         if (admin_message_from_lcr(j_remote_id, j_remote_ref, MESSAGE_NEWREF, &param)<0)
42                 FATAL("No socket with remote application '%s' found, this shall not happen. because we already created one.\n", j_remote_name);
43 }
44
45
46 /*
47  * join descructor
48  */
49 JoinRemote::~JoinRemote()
50 {
51 }
52
53 void JoinRemote::message_epoint(unsigned int epoint_id, int message_type, union parameter *param)
54 {
55         /* if endpoint has just been removed, but still a message in the que */
56         if (epoint_id != j_epoint_id)
57                 return;
58         
59         /* look for Remote's interface */
60         if (admin_message_from_lcr(j_remote_id, j_remote_ref, message_type, param)<0) {
61                 PERROR("No socket with remote application '%s' found, this shall not happen. Closing socket shall cause release of all joins.\n", j_remote_name);
62                 return;         
63         }
64
65         if (message_type == MESSAGE_RELEASE) {
66                 delete this;
67                 return;
68         }
69 }
70
71 void JoinRemote::message_remote(int message_type, union parameter *param)
72 {
73         struct lcr_msg *message;
74
75         /* create relation if no relation exists */
76         if (!j_epoint_id) {
77                 class Endpoint          *epoint;
78
79                 if (!(epoint = new Endpoint(0, j_serial)))
80                         FATAL("No memory for Endpoint instance\n");
81                 j_epoint_id = epoint->ep_serial;
82                 if (!(epoint->ep_app = new DEFAULT_ENDPOINT_APP(epoint, 1))) // outgoing
83                         FATAL("No memory for Endpoint Application instance\n");
84         }
85
86         /* set serial on bchannel message
87          * also ref is given, so we send message with ref */
88         if (message_type == MESSAGE_BCHANNEL) {
89                 message_bchannel_from_remote(this, param->bchannel.type, param->bchannel.handle);
90                 return;
91         }
92         
93         /* cannot just forward, because param is not of container "struct lcr_msg" */
94         message = message_create(j_serial, j_epoint_id, JOIN_TO_EPOINT, message_type);
95         memcpy(&message->param, param, sizeof(message->param));
96         message_put(message);
97
98         if (message_type == MESSAGE_RELEASE) {
99                 delete this;
100                 return;
101         }
102 }
103
104
105