BPicture: Fix archive constructor.
[haiku.git] / src / add-ons / kernel / file_systems / nfs4 / RPCServer.h
blob50e218b813c44fc38b5073123eed6fde8ad070d9
1 /*
2 * Copyright 2012 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Paweł Dziepak, pdziepak@quarnos.org
7 */
8 #ifndef RPCSERVER_H
9 #define RPCSERVER_H
12 #include <condition_variable.h>
13 #include <lock.h>
15 #include "Connection.h"
16 #include "RPCCall.h"
17 #include "RPCCallback.h"
18 #include "RPCReply.h"
21 namespace RPC {
23 struct Request {
24 uint32 fXID;
25 ConditionVariable fEvent;
27 bool fDone;
28 Reply** fReply;
29 status_t fError;
31 Request* fNext;
34 class RequestManager {
35 public:
36 RequestManager();
37 ~RequestManager();
39 void AddRequest(Request* request);
40 Request* FindRequest(uint32 xid);
42 private:
43 mutex fLock;
45 // Neither SinglyLinkedList nor DoublyLinkedList is what we want
46 // here. And DoublyLinkedQueue is not even a queue.
47 Request* fQueueHead;
48 Request* fQueueTail;
52 class ProgramData {
53 public:
54 virtual status_t ProcessCallback(CallbackRequest* request,
55 Connection* connection) = 0;
57 virtual ~ProgramData() { }
60 class Server {
61 public:
62 Server(Connection* connection,
63 PeerAddress* address);
64 virtual ~Server();
66 status_t SendCallAsync(Call* call, Reply** reply,
67 Request** request);
68 status_t ResendCallAsync(Call* call,
69 Request* request);
71 inline status_t WaitCall(Request* request,
72 bigtime_t time);
73 inline status_t CancelCall(Request* request);
74 status_t WakeCall(Request* request);
76 status_t Repair();
78 inline const PeerAddress& ID() const;
79 inline PeerAddress LocalID() const;
81 inline ProgramData* PrivateData();
82 inline void SetPrivateData(ProgramData* privateData);
84 Callback* GetCallback();
86 private:
87 inline uint32 _GetXID();
89 status_t _StartListening();
91 status_t _Listener();
92 static status_t _ListenerThreadStart(void* object);
94 thread_id fThread;
95 bool fThreadCancel;
96 status_t fThreadError;
98 RequestManager fRequests;
99 Connection* fConnection;
100 const PeerAddress* fAddress;
102 ProgramData* fPrivateData;
104 mutex fCallbackLock;
105 Callback* fCallback;
107 uint32 fRepairCount;
108 mutex fRepairLock;
110 int32 fXID;
114 inline status_t
115 Server::WaitCall(Request* request, bigtime_t time)
117 if (request->fDone)
118 return B_OK;
119 return request->fEvent.Wait(B_RELATIVE_TIMEOUT, time);
123 inline status_t
124 Server::CancelCall(Request* request)
126 fRequests.FindRequest(request->fXID);
127 return B_OK;
131 inline const PeerAddress&
132 Server::ID() const
134 return *fAddress;
138 inline PeerAddress
139 Server::LocalID() const
141 PeerAddress addr;
142 memset(&addr, 0, sizeof(addr));
143 fConnection->GetLocalAddress(&addr);
144 return addr;
148 inline ProgramData*
149 Server::PrivateData()
151 return fPrivateData;
155 inline void
156 Server::SetPrivateData(ProgramData* privateData)
158 delete fPrivateData;
159 fPrivateData = privateData;
163 struct ServerNode {
164 PeerAddress fID;
165 Server* fServer;
166 int fRefCount;
168 ServerNode* fLeft;
169 ServerNode* fRight;
172 class ServerManager {
173 public:
174 ServerManager();
175 ~ServerManager();
177 status_t Acquire(Server** _server, AddressResolver* resolver,
178 ProgramData* (*createPrivateData)(Server*));
179 void Release(Server* server);
181 private:
182 status_t _Acquire(Server** _server, const PeerAddress& address,
183 ProgramData* (*createPrivateData)(Server*));
185 ServerNode* _Find(const PeerAddress& address);
186 void _Delete(ServerNode* node);
187 ServerNode* _Insert(ServerNode* node);
189 ServerNode* fRoot;
190 mutex fLock;
193 } // namespace RPC
196 #endif // RPCSERVER_H