summaryrefslogtreecommitdiffstats
path: root/pki/base/tps/src/include/httpClient/httpc/ServerConnection.h
blob: bc33aa21667bd8e575b0fe13e86a184c5af6ebfb (plain)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 */
/** BEGIN COPYRIGHT BLOCK
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301  USA 
 * 
 * Copyright (C) 2007 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/

#ifndef __SERVER_CONNECTION_H
#define __SERVER_CONNECTION_H

#ifdef HAVE_CONFIG_H
#ifndef AUTOTOOLS_CONFIG_H
#define AUTOTOOLS_CONFIG_H

/* Eliminate warnings when using Autotools */
#undef PACKAGE_BUGREPORT
#undef PACKAGE_NAME
#undef PACKAGE_STRING
#undef PACKAGE_TARNAME
#undef PACKAGE_VERSION

#include <config.h>
#endif /* AUTOTOOLS_CONFIG_H */
#endif /* HAVE_CONFIG_H */

/**
 * ServerConnection.h	1.000 06/12/2002
 * 
 * This class handles server side connections. The accept happens on 
 * a separate thread and newly accepted connection are polled for 
 * read ready state. Once data is available on one or more connections
 * the listeners are notified about it.
 *
 * @author  Surendra Rajam
 * @version 1.000, 06/12/2002
 */

class EXPORT_DECL ServerConnection {
	friend class PollThread;
	friend class AcceptThread;
public:
	/**
	 *	Constructor
	 */
	ServerConnection();

	/**
	 *	Destructor
	 */
	virtual ~ServerConnection();

public:
	/**
	 * Registers a listener interface to notify on the connections
	 * 
	 * @param	listener	listener object
	 * @return				0 on success, negative error code otherwise
	 */
	int RegisterListener(ConnectionListener* listener);

	/**
	 * Listens for connections on a specified socket
	 *
	 * @param	host	host name / ip
	 * @param	port	listen port
	 * @return			0 on success, negative error code otherwise
	 */

	int Start(char* host, int port);

	/**
	 * Listens for connections on a specified socket for SSL connections
	 *
	 * @param	host		host name / ip
	 * @param	port		listen port
	 * @param	nickename	name of the server cert
	 * @param	password	password for DB
	 * @param	requestCert	request client certficate for authentication
	 * @return			0 on success, negative error code otherwise
	 */
	int Start( char* host, 
			   int port, 
			   const char* nickname, 
			   int requestcert);

	/**
	 * Closes the server connection
	 *
	 * @return	0 on success, negative error code otherwise
	 */
    int Shutdown();

	/**
	 * Releases the connection to the read pool. 
	 *
	 * @param	conn	a connection object
	 */
	void PollRead(Connection* conn);

	/**
	 * Releases the connection to the write pool. 
	 *
	 * @param	conn	a connection object
	 */
	void Release(Connection* conn);

	/**
	 * Gets a connection from the write pool. This connection should be 
	 * returned to the pool after writing.
	 *
	 * @return 0 on success, negative error code otherwise
	 */
	Connection* GetConnection();

	/**
	 * Returns the number of connections 
	 *
	 * @return	number of connections
	 */
	int GetCount();

	static void Poll(void* arg);
	static void Accept(void* arg);

protected:
	/**
	 * Protocol specific implementations should implement this
	 * function and return their own connection object
	 * 
	 * @return	a newly created connection
	 */
	virtual Connection* AcceptedConnection();

	const char* GetPeerHost(Connection* conn);
	int GetPeerPort(Connection* conn);

private:
	int InternalStart();
	void SetServerFlag(Connection* conn);
	PRFileDesc* GetFD( Connection* conn );
	void SetSocket(Connection* conn, Socket* socket);
	int UpdateWritePool(Connection* conn);

private:
	ServerSocket* m_server;
	ConnectionListener*	m_connectionListener;

	Pool* m_readPool;
	Pool* m_writePool;

	PRLock* m_readLock;
	PRLock* m_writeLock;

	PRBool m_threadInitialized;
	PRLock*	m_threadLock;
	PRCondVar* m_threadCondv;

	int	m_totalConnections;
	bool m_serverRunning;
};

#endif // __SERVER_CONNECTION_H