]> rtime.felk.cvut.cz Git - frescor/forb.git/blob - src/peer.h
Added more debugging messages to protocols
[frescor/forb.git] / src / peer.h
1 /**************************************************************************/
2 /* ---------------------------------------------------------------------- */
3 /* Copyright (C) 2006 - 2008 FRESCOR consortium partners:                 */
4 /*                                                                        */
5 /*   Universidad de Cantabria,              SPAIN                         */
6 /*   University of York,                    UK                            */
7 /*   Scuola Superiore Sant'Anna,            ITALY                         */
8 /*   Kaiserslautern University,             GERMANY                       */
9 /*   Univ. Politécnica  Valencia,           SPAIN                        */
10 /*   Czech Technical University in Prague,  CZECH REPUBLIC                */
11 /*   ENEA                                   SWEDEN                        */
12 /*   Thales Communication S.A.              FRANCE                        */
13 /*   Visual Tools S.A.                      SPAIN                         */
14 /*   Rapita Systems Ltd                     UK                            */
15 /*   Evidence                               ITALY                         */
16 /*                                                                        */
17 /*   See http://www.frescor.org for a link to partners' websites          */
18 /*                                                                        */
19 /*          FRESCOR project (FP6/2005/IST/5-034026) is funded             */
20 /*       in part by the European Union Sixth Framework Programme          */
21 /*       The European Union is not liable of any use that may be          */
22 /*       made of this code.                                               */
23 /*                                                                        */
24 /*                                                                        */
25 /*  This file is part of FORB (Frescor Object Request Broker)             */
26 /*                                                                        */
27 /* FORB is free software; you can redistribute it and/or modify it        */
28 /* under terms of the GNU General Public License as published by the      */
29 /* Free Software Foundation; either version 2, or (at your option) any    */
30 /* later version.  FORB is distributed in the hope that it will be        */
31 /* useful, but WITHOUT ANY WARRANTY; without even the implied warranty    */
32 /* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU    */
33 /* General Public License for more details. You should have received a    */
34 /* copy of the GNU General Public License along with FORB; see file       */
35 /* COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,  */
36 /* Cambridge, MA 02139, USA.                                              */
37 /*                                                                        */
38 /* As a special exception, including FORB header files in a file,         */
39 /* instantiating FORB generics or templates, or linking other files       */
40 /* with FORB objects to produce an executable application, does not       */
41 /* by itself cause the resulting executable application to be covered     */
42 /* by the GNU General Public License. This exception does not             */
43 /* however invalidate any other reasons why the executable file might be  */
44 /* covered by the GNU Public License.                                     */
45 /**************************************************************************/
46
47 /**
48  * @file   peer.h
49  * @author Michal Sojka <sojkam1@fel.cvut.cz>
50  * @date   Sun Oct 12 17:31:51 2008
51  * 
52  * @brief  Declarations of peer data structure and functions.
53  * 
54  * 
55  */
56
57 #ifndef FORB_PEER_H
58 #define FORB_PEER_H
59
60 #include <forb/forb-internal.h>
61 #include "port.h"
62 #include "refcnt.h"
63 #include <ul_gavl.h>
64 #include <ul_list.h>
65 #include <fosa_mutexes_and_condvars.h>
66 /**
67  * State of a peer.
68  * 
69  */
70 enum forb_peer_state {
71         FORB_PEER_WANTED,       /**< Somebody is waiting for the peer to be discovered. In this state, only @a server_id field is set. */
72         FORB_PEER_DISCOVERED,   /**< The peer is already discovered, so it is addr is known and messages can be sent to him. */
73 };
74
75 /**
76  * Description of a peer FORB. We consider peers only in one-hop
77  * distance. For more distant forbs, we need to use a routing table.
78  *
79  * @note This structure is reference counted. Use forb_peer_get() when
80  * you copy a pointer to peer and forb_peer_put() when the pointer is
81  * not needed.
82  */
83 typedef struct forb_peer {
84          forb_server_id server_id; /**< Server_id of the peer */
85          forb_port_t *port;     /**< Port of this peer is connected to */
86          void *addr;            /**< Protocol specific address of the peer. */
87          char *orb_id;          /**< ORB ID of the peer */
88          void *proto_priv;      /**< Protocol private data (e.g. info about established connection) */
89          gavl_node_t gnode;     /**< Node of forb_t::peers tree */
90          ul_list_node_t lnode;  /**< Node of forb_port_t::peers list */
91          forb_ref_t ref;        /**< Reference count */
92          enum forb_peer_state state;
93          fosa_cond_t cond;      /**< Condition variable for waiting after the peer is discovered. Used together with forb_t::peer_mutex. */
94          fosa_mutex_t send_lock;
95 } forb_peer_t;
96
97
98 GAVL_CUST_NODE_INT_DEC(forb_peer_nolock,/* cust_prefix */
99                        forb_t,          /* cust_root_t */
100                        forb_peer_t,     /* cust_item_t */
101                        forb_server_id,  /* cust_key_t */
102                        peers,           /* cust_root_node */
103                        gnode,           /* cust_item_node */
104                        server_id,       /* cust_item_key */
105                        forb_server_id_cmp)/* cust_cmp_fnc */
106
107 UL_LIST_CUST_DEC(forb_port_peer, /* cust_prefix */
108                  forb_port_t,    /* cust_head_t */
109                  forb_peer_t,    /* cust_item_t */
110                  peers,          /* cust_head_field */
111                  lnode)          /* cust_node_field */
112
113 forb_peer_t *
114 forb_peer_new(void);
115
116 void
117 forb_peer_release(forb_ref_t *ref);
118
119 static inline forb_peer_t *
120 forb_peer_get(forb_peer_t *peer)
121 {
122         forb_ref_get(&peer->ref);
123         return peer;
124 }
125         
126 static inline void
127 forb_peer_put(forb_peer_t *peer)
128 {
129         forb_ref_put(&peer->ref, forb_peer_release);
130 }
131
132 static inline void
133 forb_peer_insert(forb_t *forb, forb_peer_t *peer)
134 {
135         fosa_mutex_lock(&forb->peer_mutex);
136         forb_peer_get(peer);
137         forb_peer_nolock_insert(forb, peer);
138         fosa_mutex_unlock(&forb->peer_mutex);
139 }
140
141 static inline void
142 forb_peer_delete(forb_t *forb, forb_peer_t *peer)
143 {
144         fosa_mutex_lock(&forb->peer_mutex);
145         forb_peer_nolock_delete(forb, peer);
146         forb_peer_put(peer);
147         fosa_mutex_unlock(&forb->peer_mutex);
148 }
149
150 void
151 forb_peer_delete_by_port(forb_t *forb, forb_port_t *port);
152
153 /** 
154  * Finds peer with given @a server_id.
155  * 
156  * @param forb 
157  * @param server_id 
158  * 
159  * @return The found peer or NULL if no peer is found. You have to
160  * call forb_peer_put() after you finish working with the non NULL
161  * retuned value.
162  */
163 static inline forb_peer_t *
164 forb_peer_find(forb_t *forb, forb_server_id *server_id)
165 {
166         forb_peer_t *ret;
167         fosa_mutex_lock(&forb->peer_mutex);
168         ret = forb_peer_nolock_find(forb, server_id);
169         if (ret) {
170                 forb_peer_get(ret);
171         }
172         fosa_mutex_unlock(&forb->peer_mutex);
173         return ret;
174 }
175
176 forb_peer_t *
177 forb_peer_find_timed(forb_t *forb, forb_server_id *server_id,
178                      fosa_abs_time_t *timeout);
179
180 #endif