File: netpolling.h

package info (click to toggle)
gps 0.9.4-1.woody2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 964 kB
  • ctags: 928
  • sloc: cpp: 10,822; sh: 381; makefile: 307; ansic: 227; perl: 17
file content (165 lines) | stat: -rw-r--r-- 4,417 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* $Id: netpolling.h,v 1.7 2000/08/08 01:07:21 bergo Exp $ */

#ifndef NETPOLLING_H
#define NETPOLLING_H

#include "polling.h"

#define MAX_NET_LINE 4096

/**
   network connection (TCP stream) manager. implements I/O
   for the network pollers (derivated classes that poll
   rgpsp remotely). This I/O is already oriented to the
   rpgps protocol (i.e.: lines beginning with '*' are errors).
 */
class NetworkActor {
 public:

  /// opens connection to hostname:port
  int hopen(char *hostname,int port);

  /// an fgets-like input method, returns 0 if ok, <0 on error.
  int hreadln(char *buffer,int max);

  /// closes connection
  int hclose();

  /// reads all available data (garbage?) from socket. Non-blocking.
  void read_dead_output();

  /** requests refresh with space (usually provides a system info
      header followed by non-detailed process list */
  void request_refresh();

  /** requests refresh with  details. when the remote node is
      marked as special, the sent character is Ctrl-L, and the
      process list has LOTS of details. Otherwise sends space. */
  void request_detailed_refresh();

  /** requests refresh but just for the system info header.
      If the remote node is marked as special, sends an 'h'
      character, and retrieves a header without process list.
      If the remote node does not announces itself as special,
      this will send the space character as usual. */
  void request_usage_header();

  /// returns whether the remote node is marked as special.
  int isSpecial();

 protected:

  /// 0 means the connection is ok.
  int ok;
 private:
  int thesocket;
  char privateb[128];
  int thedoor;

  int is_special;

  friend void *thread_network_blocking_stuff(void *p);
};

/// network watch poller, provides a process list from a remote host
class NetworkListPoller : public NetworkActor {
 public:

  /** constructor, connects to host hname on the given tcp port.
      hname may be a hostname or ip address in standard dot notation
      (if gethostbyname() accepts it, we do too) */
  NetworkListPoller(char *hname,int port);

  /// actual polling implementation
  virtual void poll();

  /// clean up, called on destruction.
  void terminate();

  /// returns positive if connection is up, 0 otherwise.
  int isOk();

  /// the process list, as a glib doubly linked list of (ProcessListItem *).
  GList *process_list;

 protected:
  /// clean up for the process list.
  void reset();

 private:
  char hostname[128];
  int tcpport;
};

/// network watch details poller, provides single process info.
class NetworkDetailsPoller : public NetworkActor {
 public:

  /** constructor, opens connection to hname : port.
      hname may be a hostname or ip address in standard dot notation
      (if gethostbyname() accepts it, we do too) */
  NetworkDetailsPoller(char *hname,int port);

  /// frees the buffer
  ~NetworkDetailsPoller();
  
  /// actual polling implementation. fills item with data about whichpid.
  virtual void poll(int whichpid);

  /// poll to an internal buffer
  void poll2buffer();

  /// query data in buffer instead of making new net traffic
  void queryBuffer(int whichpid);
  
  /// clean up, called on destruction.
  void terminate();

  /// returns positive if connection is up, 0 otherwise.
  int isOk();

  /** returns positive if this poller was initialized with hname equal to
      s, 0 otherwise. */
  int isHost(char *s);

  /// contains the details about the last process polled.
  ProcessItem *item;

 private:
  int nb_readln(char *dest,int lim);
  void nb_gotop();

  char *netbuffer;
  char hostname[128];
  int tcpport;

  char *nb_pos;
};

/// network watch poller, provides cpu and memory information.
class NetworkSystemInfoProvider :
 public SystemInfoProvider, NetworkActor {
 public:

  /** constructor, opens connection to hname : port.
      hname may be a hostname or ip address in standard dot notation
      (if gethostbyname() accepts it, we do too) */
  NetworkSystemInfoProvider(char *hname,int port);

  /** actual polling implementation. memory info may be bogus due
      to innacurate rgpsp implementations running on remote site. */
  void update();

  /// clean up called on destruction. closes connection.
  void terminate();

  /// returns the hostname on the other end of the connection.
  char *getHostname();

 private:
  char hostname[128];
  int tcpport;
  int mem_shift[256];
};

#endif