1 | /****************************************************************************** |
---|
2 | * QUANTA - A toolkit for High Performance Data Sharing |
---|
3 | * Copyright (C) 2003 Electronic Visualization Laboratory, |
---|
4 | * University of Illinois at Chicago |
---|
5 | * |
---|
6 | * All rights reserved. |
---|
7 | * |
---|
8 | * Redistribution and use in source and binary forms, with or without |
---|
9 | * modification, are permitted provided that the following conditions are met: |
---|
10 | * |
---|
11 | * * Redistributions of source code must retain the above copyright |
---|
12 | * notice, this list of conditions and the following disclaimer. |
---|
13 | * * Redistributions in binary form must reproduce the above |
---|
14 | * copyright notice, this list of conditions and the following disclaimer |
---|
15 | * in the documentation and/or other materials provided with the distribution. |
---|
16 | * * Neither the name of the University of Illinois at Chicago nor |
---|
17 | * the names of its contributors may be used to endorse or promote |
---|
18 | * products derived from this software without specific prior written permission. |
---|
19 | * |
---|
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
---|
24 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
---|
25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
---|
26 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
---|
27 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
---|
28 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
---|
29 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
30 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
31 | * |
---|
32 | * Direct questions, comments etc about Quanta to cavern@evl.uic.edu |
---|
33 | *****************************************************************************/ |
---|
34 | |
---|
35 | #ifndef _QUANTAPLUS_PERF_DAEMON_C |
---|
36 | #define _QUANTAPLUS_PERF_DAEMON_C |
---|
37 | |
---|
38 | #ifndef __HAVE_STDIO_H |
---|
39 | #include <stdio.h> |
---|
40 | #define __HAVE_STDIO_H |
---|
41 | #endif |
---|
42 | |
---|
43 | #include "QUANTAnet_perfClient_c.hxx" |
---|
44 | |
---|
45 | class QUANTAnet_tcpServer_c; |
---|
46 | |
---|
47 | struct PerfDaemonClient |
---|
48 | { |
---|
49 | QUANTAnet_perfDaemonClient_c *Client; |
---|
50 | int IsPerfClient; |
---|
51 | int Started; |
---|
52 | }; |
---|
53 | |
---|
54 | /** QUANTA performance monitoring daemon class. This class treats TCP |
---|
55 | streams as discrete packets/messages that can be reflected to multiple |
---|
56 | connected performance monitoring clients. Clients must be created using |
---|
57 | QUANTAnet_perfDaemonClient_c. Hence do NOT attempt to use |
---|
58 | QUANTAnet_tcpClient_c to connect to a perf daemon class. This class has |
---|
59 | 2 main functions: checkForNewClients() and process(). checkForNewClients, |
---|
60 | as the name suggests, checks to see if any new clients wish to connect |
---|
61 | to the reflector. Process() does the actual work of data reflection. |
---|
62 | |
---|
63 | checkForNewClients is called everytime process() is called. If you want |
---|
64 | the check to be done more frequently you need to do it yourself. Similarly |
---|
65 | if you want the process() call done more frequently it is up to you to do it. |
---|
66 | I recommend threading those two off and setting up a mutex so that you do |
---|
67 | not do process() and checkForNewClients() at the same time. |
---|
68 | |
---|
69 | @author cavern@evl.uic.edu |
---|
70 | @version 3/28/2000 |
---|
71 | |
---|
72 | */ |
---|
73 | class QUANTAnet_perfDaemon_c |
---|
74 | { |
---|
75 | public: |
---|
76 | /** |
---|
77 | Constructor of performance monitoring daemon class. |
---|
78 | |
---|
79 | @param File |
---|
80 | Optional parameter. If file pointer is given here, it will be used |
---|
81 | to save all log information. |
---|
82 | */ |
---|
83 | QUANTAnet_perfDaemon_c(FILE* File = NULL); |
---|
84 | ~QUANTAnet_perfDaemon_c(); |
---|
85 | |
---|
86 | //@{ |
---|
87 | /// Status ok. |
---|
88 | static const int OK/* = 0*/; |
---|
89 | |
---|
90 | /// Status failed. |
---|
91 | static const int FAILED/* = 1*/; |
---|
92 | |
---|
93 | /// Memory allocation error. |
---|
94 | static const int MEM_ALLOC_ERR/* = 2*/; |
---|
95 | |
---|
96 | /// New client has been connected. |
---|
97 | static const int NEW_CONNECTION_ESTABLISHED/* = 4*/; |
---|
98 | |
---|
99 | /// Reflector cannot handle any more connections |
---|
100 | static const int TOO_MANY_CLIENTS/* = 5*/; |
---|
101 | |
---|
102 | /// No new connection. |
---|
103 | static const int NO_NEW_CONNECTION/* = 6*/; |
---|
104 | |
---|
105 | /// A non-blocking read had no data available to read. |
---|
106 | static const int NON_BLOCKING_HAS_NO_DATA/* = 7*/; |
---|
107 | |
---|
108 | /// Skip the data distribution process. Used in user callback. See intercept(). |
---|
109 | static const int SKIP_DISTRIBUTION/* = 8*/; |
---|
110 | |
---|
111 | static const int DEFAULT_PORT/* = 9500*/; |
---|
112 | //@} |
---|
113 | |
---|
114 | /** Initialize the reflector. |
---|
115 | @param incomingPort is listening port for incoming connections. Default is 7000. |
---|
116 | @param maxClients is the max number of clients the reflector will manage. |
---|
117 | @return Either QUANTAnet_perfDaemon_c::OK,FAILED,MEM_ALLOC_ERR. |
---|
118 | */ |
---|
119 | int init(int incomingPort=PERF_DAEMON_DEFAULT_PORT, int maxClients = 64); |
---|
120 | |
---|
121 | /** Call this within a while loop to let the reflector continuously |
---|
122 | do its processing. |
---|
123 | @return Either QUANTAnet_perfDaemon_c::OK,MEM_ALLOC_ERR |
---|
124 | */ |
---|
125 | int process(); |
---|
126 | |
---|
127 | /** Call this as often as you wish to check for new clients. |
---|
128 | Note. If you do this in a separate thread then you must set up a mutex |
---|
129 | so that you do not call the proces() call and this call at the same time. |
---|
130 | The process() call itself has imbedded in it 1 check for each time you |
---|
131 | call it. |
---|
132 | @return Either QUANTAnet_perfDaemon_c::NEW_CONNECTION_ESTABLISHED, NO_NEW_CONNECTION, TOO_MANY_CLIENTS. |
---|
133 | */ |
---|
134 | int checkForNewClients(); |
---|
135 | |
---|
136 | /** Intercept incoming messages and call a user-defined callback function. |
---|
137 | If you want you can also alter the buffer completely so that the reflector will reflect an |
---|
138 | entirely different message. You can do this by changing the contents of the buffer or |
---|
139 | by replacing the buffer entirely by allocating memory for a new buffer and stuffing it |
---|
140 | with your own data. If you choose to allocate a totally new buffer you must remember |
---|
141 | to deallocate memory for the original |
---|
142 | buffer before substituting it with yours. |
---|
143 | |
---|
144 | If after your callback function exits you do not wish the reflector to forward |
---|
145 | the contents of the buffer, return with QUANTA_tcpReflector_c::SKIP_DISTRIBUTION. Otherwise |
---|
146 | just return QUANTA_tcpReflector_c::OK. |
---|
147 | |
---|
148 | Note also that the callback function will also be given a pointer to a |
---|
149 | QUANTAnet_perfDaemonClient_c object that |
---|
150 | can then be used to send data directly to the client that originally sent the message. |
---|
151 | */ |
---|
152 | void intercept(int (*callback) (QUANTAnet_perfDaemonClient_c *client, char** buffer, |
---|
153 | int *bufferSize, void *userData), void* userData); |
---|
154 | |
---|
155 | /** Intercept any new connections that are formed. |
---|
156 | This allows you to send private data to the newly formed connection before it assumes its data |
---|
157 | reflection duties. |
---|
158 | Callback function will be given a pointer to the QUANTAnet_perfDaemonClient_c object that |
---|
159 | can then be used to send data directly to the client. |
---|
160 | */ |
---|
161 | void interceptNewConnection(void (*callback) (QUANTAnet_perfDaemonClient_c *newClient, void* userData), void *userData); |
---|
162 | |
---|
163 | int sendToAll(char* buf, int incomingSize); |
---|
164 | |
---|
165 | private: |
---|
166 | PerfDaemonClient **clients; |
---|
167 | QUANTAnet_tcpServer_c *server; |
---|
168 | unsigned short incomingPort; |
---|
169 | FILE* LogFile; |
---|
170 | |
---|
171 | int (*interceptCallback) (QUANTAnet_perfDaemonClient_c *newClient, char** buffer, |
---|
172 | int* bufsize, void *userData); |
---|
173 | void *interceptUserData; |
---|
174 | |
---|
175 | void (*interceptNewConnectionCallback) (QUANTAnet_perfDaemonClient_c *newClient, void *userData); |
---|
176 | void *interceptNewConnectionUserData; |
---|
177 | |
---|
178 | // Distribute the data to all connected performance monitoring clients. |
---|
179 | // Specify -1 if want to send to all. |
---|
180 | int distributeDataToPerfClients(char* buf, int incomingSize); |
---|
181 | |
---|
182 | // Remove a client from the client array. |
---|
183 | void removeClient(int clientNum); |
---|
184 | |
---|
185 | // Close all client sockets and remove them. Used by destructor. |
---|
186 | // Also closes down and deallocates the server. |
---|
187 | void closeDownSockets(); |
---|
188 | int maxNumClients; |
---|
189 | }; |
---|
190 | |
---|
191 | #endif |
---|