gPTP Documentation
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PeerList.hpp
Go to the documentation of this file.
1 /******************************************************************************
2 
3 Copyright (c) 2009-2012, Intel Corporation
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 
9 1. Redistributions of source code must retain the above copyright notice,
10 this list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15 
16 3. Neither the name of the Intel Corporation nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 POSSIBILITY OF SUCH DAMAGE.
31 
32 ******************************************************************************/
33 
34 #ifndef PEERLIST_HPP
35 #define PEERLIST_HPP
36 
37 #include <vector>
38 #include <ipcdef.hpp>
39 #include <Lockable.hpp>
40 
46 typedef struct {
48  void *state;
50 
54 typedef bool (*peer_init_handler)( void **, void *, PeerAddr addr );
58 typedef bool (*peer_rm_handler)( void *, void * );
59 
63 typedef std::vector<PeerAddrWithState> PeerVector;
64 
68 class PeerList : public Lockable {
69 private:
70  peer_init_handler init;
71  void *handler_arg;
72  peer_rm_handler rm;
73  PeerVector internal_vector;
74  bool ready;
75 public:
76  typedef PeerVector::const_iterator const_iterator;
77  typedef PeerVector::iterator PeerVectorIt;
81  PeerList() { rm = NULL; init = NULL; }
88  PeerVectorIt find( PeerAddr addr, bool *found ) {
89  PeerVectorIt it = internal_vector.begin();
90  *found = false;
91  if( internal_vector.size() == 0 ) {
92  goto done;
93  }
94  for( ;it < internal_vector.end(); ++it ) {
95  if( addr == (*it).addr ) {
96  *found = true;
97  break;
98  }
99  if( addr < (*it).addr ) {
100  break;
101  }
102  }
103 done:
104  return it;
105  }
111  bool add( PeerAddr addr ) {
112  bool found;
113  PeerVectorIt it;
114  it = find( addr, &found );
115  if( found ) {
116  return false;
117  } else {
118  PeerAddrWithState addr_state = { addr, NULL };
119  if( init ) {
120  if( !init( &addr_state.state, handler_arg, addr )) {
121  //DBGPRINT("Call to initialize peer state failed");
122  // return false?
123  } else {
124  internal_vector.insert( it, addr_state );
125  }
126  }
127  }
128  return true;
129  }
135  bool remove( PeerAddr addr ) {
136  bool found;
137  PeerVectorIt it;
138  it = find( addr, &found );
139  if( !found ) {
140  return false;
141  } else {
142  if( rm != NULL ) {
143  if( !rm( it->state, handler_arg ) ) {
144  fprintf( stderr, "Call to cleanup peer state failed\n" );
145  }
146  }
147  internal_vector.erase( it );
148  }
149  return true;
150  }
156  return internal_vector.begin();
157  }
163  return internal_vector.end();
164  }
171  void setInit( peer_init_handler init, void *init_arg ) {
172  this->init = init;
173  this->handler_arg = init_arg;
174  }
180  void setRm( peer_rm_handler rm ) {
181  this->rm = rm;
182  }
187  bool IsReady() { return ready; }
193  void setReady( bool ready ) { this->ready = ready; }
194 };
195 
196 #endif/*PEERLIST_HPP*/
PeerVector::const_iterator const_iterator
Definition: PeerList.hpp:76
Definition: Lockable.hpp:44
void * state
Definition: PeerList.hpp:48
const_iterator end()
Gets the end of the sequence container.
Definition: PeerList.hpp:162
Definition: windows/daemon_cl/ipcdef.hpp:275
PeerVector::iterator PeerVectorIt
Definition: PeerList.hpp:77
bool(* peer_rm_handler)(void *, void *)
Definition: PeerList.hpp:58
Definition: PeerList.hpp:46
std::vector< PeerAddrWithState > PeerVector
Definition: PeerList.hpp:63
bool(* peer_init_handler)(void **, void *, PeerAddr addr)
Definition: PeerList.hpp:54
void setInit(peer_init_handler init, void *init_arg)
Sets init handler.
Definition: PeerList.hpp:171
bool IsReady()
Gets ready flag.
Definition: PeerList.hpp:187
PeerVectorIt find(PeerAddr addr, bool *found)
Look for a peer address.
Definition: PeerList.hpp:88
PeerAddr addr
Definition: PeerList.hpp:47
Definition: PeerList.hpp:68
void setRm(peer_rm_handler rm)
Sets peer remove callback on the PeerList object.
Definition: PeerList.hpp:180
void setReady(bool ready)
Sets ready flag.
Definition: PeerList.hpp:193
const_iterator begin()
Gets the beginning of the sequence container.
Definition: PeerList.hpp:155
bool add(PeerAddr addr)
Add a new peer address.
Definition: PeerList.hpp:111
PeerList()
Definition: PeerList.hpp:81