xref: /haiku/src/add-ons/kernel/file_systems/nfs4/RPCServer.h (revision b35311f1ba07855a6152bb248b1758567f52e224)
1 /*
2  * Copyright 2012 Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Paweł Dziepak, pdziepak@quarnos.org
7  */
8 #ifndef RPCSERVER_H
9 #define RPCSERVER_H
10 
11 
12 #include <condition_variable.h>
13 #include <lock.h>
14 
15 #include "Connection.h"
16 #include "RPCCall.h"
17 #include "RPCReply.h"
18 
19 
20 namespace RPC {
21 
22 struct Request {
23 	uint32				fXID;
24 	ConditionVariable	fEvent;
25 	Reply**				fReply;
26 
27 	Request*			fNext;
28 };
29 
30 class RequestManager {
31 public:
32 						RequestManager();
33 						~RequestManager();
34 
35 			void		AddRequest(Request* req);
36 			Request*	FindRequest(uint32 xid);
37 
38 private:
39 			mutex		fLock;
40 			Request*	fQueueHead;
41 			Request*	fQueueTail;
42 
43 };
44 
45 class Server {
46 public:
47 							Server(Connection* conn);
48 	virtual					~Server();
49 
50 			status_t		SendCall(Call* call, Reply** reply);
51 	inline	void			CancelCall(uint32 xid);
52 
53 protected:
54 	inline	uint32			_GetXID();
55 			status_t		_StartListening();
56 
57 private:
58 			status_t		_Listener();
59 	static	status_t		_ListenerThreadStart(void* ptr);
60 
61 			thread_id		fThread;
62 			bool			fThreadCancel;
63 
64 			RequestManager	fRequests;
65 			Connection*		fConnection;
66 
67 			vint32			fXID;
68 	static	const bigtime_t	kWaitTime	= 1000000;
69 };
70 
71 struct ServerNode {
72 	ServerAddress	fID;
73 	Server*		fServer;
74 	int			fRefCount;
75 
76 	ServerNode*	fLeft;
77 	ServerNode* fRight;
78 };
79 
80 class ServerManager {
81 public:
82 						ServerManager();
83 						~ServerManager();
84 
85 			status_t	Acquire(Server** pserv, uint32 ip, uint16 port,
86 								Transport proto);
87 			void		Release(Server* serv);
88 
89 private:
90 
91 
92 			ServerNode*	_Find(ServerAddress& id);
93 			void		_Delete(ServerNode* node);
94 			ServerNode*	_Insert(ServerNode* node);
95 
96 			ServerNode*	fRoot;
97 			mutex		fLock;
98 };
99 
100 }		// namespace RPC
101 
102 
103 #endif	// RPCSERVER_H
104 
105