fixes & improvements
[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) : Join()
32 {
33         PDEBUG(DEBUG_JOIN, "Constructor(new join)");
34         union parameter *param;
35
36         SCPY(j_remote, remote);
37         j_type = JOIN_TYPE_REMOTE;
38
39         j_epoint_id = serial;
40         if (j_epoint_id)
41                 PDEBUG(DEBUG_JOIN, "New remote join connected to endpoint id %lu and application %s\n", j_epoint_id, remote);
42
43         /* send new ref to remote socket */
44         memset(&param, 0, sizeof(param));
45         if (admin_message_from_join(j_remote, j_serial, MESSAGE_NEWREF, param)<0)
46                 FATAL("No socket with remote application '%s' found, this shall not happen. because we already created one.\n", j_remote);
47 }
48
49
50 /*
51  * join descructor
52  */
53 JoinRemote::~JoinRemote()
54 {
55
56 }
57
58
59 /* join process is called from the main loop
60  * it processes the current calling state.
61  * returns 0 if join nothing was done
62  */
63 int JoinRemote::handler(void)
64 {
65         return(0);
66 }
67
68
69 void JoinRemote::message_epoint(unsigned long epoint_id, int message_type, union parameter *param)
70 {
71         /* if endpoint has just been removed, but still a message in the que */
72         if (epoint_id != j_epoint_id)
73                 return;
74         
75         /* look for Remote's interface */
76         if (admin_message_from_join(j_remote, j_serial, message_type, param)<0)
77         {
78                 PERROR("No socket with remote application '%s' found, this shall not happen. Closing socket shall cause release of all joins.\n", j_remote);
79                 return;         
80         }
81
82         if (message_type == MESSAGE_RELEASE)
83         {
84                 delete this;
85                 return;
86         }
87 }
88
89 void JoinRemote::message_remote(unsigned long ref, int message_type, union parameter *param)
90 {
91         struct message *message;
92
93         /* create relation if no relation exists */
94         if (!j_epoint_id)
95         {
96                 class Endpoint          *epoint;
97
98                 if (!(epoint = new Endpoint(0, j_serial, ref)))
99                         FATAL("No memory for Endpoint instance\n");
100                 if (!(epoint->ep_app = new DEFAULT_ENDPOINT_APP(epoint)))
101                         FATAL("No memory for Endpoint Application instance\n");
102         }
103
104         message = message_create(j_serial, j_epoint_id, JOIN_TO_EPOINT, message_type);
105         memcpy(&message->param, param, sizeof(message->param));
106         message_put(message);
107
108         if (message_type == MESSAGE_RELEASE)
109         {
110                 delete this;
111                 return;
112         }
113 }
114
115
116