AgentX++  2.0.5
agentx.h
Go to the documentation of this file.
1 /*_############################################################################
2  _##
3  _## agentx.h
4  _##
5  _##
6  _## AgentX++ 2
7  _## -------------------------------------
8  _## Copyright (C) 2000-2013 - Frank Fock
9  _##
10  _## Use of this software is subject to the license agreement you received
11  _## with this software and which can be downloaded from
12  _## http://www.agentpp.com
13  _##
14  _## This is licensed software and may not be used in a commercial
15  _## environment, except for evaluation purposes, unless a valid
16  _## license has been purchased.
17  _##
18  _##########################################################################*/
19 
20 #ifndef _agentx_h_
21 #define _agentx_h_
22 
23 
24 #include <agent_pp/agent++.h>
25 #include <agentx_pp/agentx_def.h>
26 #include <agentx_pp/agentx_pdu.h>
27 #include <agentx_pp/agentx_queue.h>
28 #include <agentx_pp/agentx_peer.h>
31 
32 
33 #define AGENTX_UNIX_SOCKET_STALE 30
34 
35 #ifndef WIN32
36 #define AX_UNIX_SOCKET
37 #endif
38 #define AX_TCP_SOCKET
39 
40 // Define this if sequential access to sockets is needed
41 //#define LOCK_AGENTX_SOCKET
42 
43 
44 #ifdef AGENTPP_NAMESPACE
45 namespace Agentpp {
46 #endif
47 
48 /*-------------------------- class AgentX -----------------------*/
49 
56 #if !defined (AGENTXPP_DECL_TEMPL_LIST_AGENTXPEER)
57 #define AGENTXPP_DECL_TEMPL_LIST_AGENTXPEER
59 #endif
60 
62 
63  public:
64 
68  AgentX();
69 
73  virtual ~AgentX();
74 
84  void set_connect_mode(int m) { connectMode = m; }
85 
94  int get_connect_mode() { return connectMode; }
95 
96 #ifdef AX_UNIX_SOCKET
97 
103  void set_unix_port_loc(const NS_SNMP OctetStr&);
104 #endif
105 
106 #ifdef AX_TCP_SOCKET
107 
113  void set_tcp_port(int);
114 #endif
115 
121  virtual bool quit() { return stopit; }
122 
128  virtual void reinit() { stopit = FALSE; }
129 
130 
142  virtual int send_agentx(int, const AgentXPdu&);
143 
155  virtual int receive_agentx(int, AgentXPdu&);
156 
163  virtual ListCursor<AgentXPeer> peers();
164 
175  virtual AgentXPeer* remove_peer(const AgentXPeer&);
176 
177 #ifdef AX_UNIX_SOCKET
178 
184  virtual bool bind_unix() = 0;
185 
192  int get_unix_socket();
193 #endif
194 
195 #ifdef AX_TCP_SOCKET
196 
202  virtual bool bind_tcp() = 0;
203 
210  int get_tcp_socket();
211 
212 #endif
213 
214  int set_file_descriptors(fd_set*);
215  bool check_peer_closed(fd_set*);
216 
217  void lock_socket();
218  void unlock_socket();
219  //virtual bool bind_tcp(int);
220 
221  void lock_queue() { axQueue.lock(); }
222  void unlock_queue() { axQueue.unlock(); }
223 
224  void lock_peers() { axPeersLock.lock(); }
225  void unlock_peers() { axPeersLock.unlock(); }
226 
227  AgentXQueue* get_queue() { return &axQueue; }
228 
240  virtual bool is_response(const AgentXPdu&,
241  const AgentXPdu&) = 0;
242 
253  void close_session(const AgentXSession&, u_int,
254  u_char);
255 
264  void ping_session(const AgentXSession&, u_int);
265 
280  static time_t compute_timeout(u_int, u_int = 0);
281 
282  bool stopit;
283 
284  protected:
285 
286 
287 #ifdef AX_TCP_SOCKET
290 #endif
291 #ifdef AX_UNIX_SOCKET
292  int axSocket;
293  NS_SNMP OctetStr axUnixSocketLocation;
294 #endif
297  Synchronized axPeersLock;
298 
299  Synchronized axSocketLock;
300 
302 };
303 
304 
305 
307 
308  public:
309  AgentXMaster();
310 
311  virtual ~AgentXMaster();
312 
319  void set_timeout(int t) { timeout = t; }
320 
321 #ifdef AX_UNIX_SOCKET
322  virtual bool bind_unix();
323 #endif
324 
325 #ifdef AX_TCP_SOCKET
326  virtual bool bind_tcp();
327 #endif
328 
337  virtual void connect_request(fd_set*, int*);
338 
350  virtual bool is_response(const AgentXPdu&,
351  const AgentXPdu&);
352 
353  protected:
354 
362 #ifdef AX_UNIX_SOCKET
363  virtual int accept_unix();
364 #endif
365  int timeout;
366 
373  virtual bool validate_address(const NS_SNMP OctetStr&) const;
374 };
375 
376 
377 
379 
380  public:
381 
382  AgentXSlave();
383  virtual ~AgentXSlave();
384 
385 #ifdef AX_UNIX_SOCKET
386  virtual bool bind_unix();
387 #endif
388 
389 #ifdef AX_TCP_SOCKET
390 
397  void set_master_address(const NS_SNMP UdpAddress& addr)
398  { masterAddress = addr; }
399 
400  virtual bool bind_tcp();
401 #endif
402 
416  virtual AgentXPdu* receive(const u_int, int&);
417 
426  virtual int send(const AgentXPdu&);
427 
438  virtual int send_synch(AgentXPdu&, AgentXPdu&);
439 
449  virtual int open_session(AgentXSession&);
450 
462  virtual bool is_response(const AgentXPdu&,
463  const AgentXPdu&);
464 
465  void lock_rcv_socket();
466  void unlock_rcv_socket();
467 
475  int get_socket() { return axSlaveSocket; }
476 
477  protected:
478 
480 
481  typedef enum { UNCONNECTED, UNIX, TCP } ConnectionType;
482 
484 
485 #ifdef AX_TCP_SOCKET
486  NS_SNMP UdpAddress masterAddress;
487 #endif
488 
489 };
490 
491 #ifdef AGENTPP_NAMESPACE
492 }
493 #endif
494 
495 #endif