xref: /haiku/src/add-ons/kernel/file_systems/netfs/client/ServerConnectionProvider.cpp (revision 17889a8c70dbb3d59c1412f6431968753c767bab)
1 // ServerConnectionProvider.cpp
2 
3 #include "ServerConnectionProvider.h"
4 
5 #include <AutoLocker.h>
6 
7 #include "ExtendedServerInfo.h"
8 #include "ServerConnection.h"
9 
10 // constructor
11 ServerConnectionProvider::ServerConnectionProvider(VolumeManager* volumeManager,
12 	ExtendedServerInfo* serverInfo,
13 	vnode_id connectionBrokenTarget)
14 	:
15 	BReferenceable(),
16 	fLock("server connection provider"),
17 	fVolumeManager(volumeManager),
18 	fServerInfo(serverInfo),
19 	fServerConnection(NULL),
20 	fConnectionBrokenTarget(connectionBrokenTarget)
21 {
22 	if (fServerInfo)
23 		fServerInfo->AcquireReference();
24 }
25 
26 // destructor
27 ServerConnectionProvider::~ServerConnectionProvider()
28 {
29 	AutoLocker<Locker> _(fLock);
30 	if (fServerConnection) {
31 		fServerConnection->Close();
32 		fServerConnection->ReleaseReference();
33 	}
34 
35 	if (fServerInfo)
36 		fServerInfo->ReleaseReference();
37 }
38 
39 // Init
40 status_t
41 ServerConnectionProvider::Init()
42 {
43 	return B_OK;
44 }
45 
46 // GetServerConnection
47 status_t
48 ServerConnectionProvider::GetServerConnection(
49 	ServerConnection** serverConnection)
50 {
51 	AutoLocker<Locker> _(fLock);
52 
53 	// if there is no server connection yet, create one
54 	if (!fServerConnection) {
55 		fServerConnection = new(std::nothrow) ServerConnection(fVolumeManager,
56 			fServerInfo);
57 		if (!fServerConnection)
58 			return B_NO_MEMORY;
59 		status_t error = fServerConnection->Init(fConnectionBrokenTarget);
60 		if (error != B_OK)
61 			return error;
62 	}
63 
64 	if (!fServerConnection->IsConnected())
65 		return B_ERROR;
66 
67 	fServerConnection->AcquireReference();
68 	*serverConnection = fServerConnection;
69 	return B_OK;
70 }
71 
72 // GetExistingServerConnection
73 ServerConnection*
74 ServerConnectionProvider::GetExistingServerConnection()
75 {
76 	AutoLocker<Locker> _(fLock);
77 
78 	// if there is no server connection yet, create one
79 	if (!fServerConnection || !fServerConnection->IsConnected())
80 		return NULL;
81 
82 	fServerConnection->AcquireReference();
83 	return fServerConnection;
84 }
85 
86 // CloseServerConnection
87 void
88 ServerConnectionProvider::CloseServerConnection()
89 {
90 	AutoLocker<Locker> _(fLock);
91 	if (fServerConnection)
92 		fServerConnection->Close();
93 }
94 
95