xref: /haiku/src/add-ons/kernel/file_systems/netfs/shared/ServerInfo.cpp (revision 1deede7388b04dbeec5af85cae7164735ea9e70d)
1 // ServerInfo.cpp
2 
3 #include "RequestFlattener.h"
4 #include "RequestUnflattener.h"
5 #include "ServerInfo.h"
6 
7 // VisitString
8 static
9 void
10 VisitString(RequestMember* member, RequestMemberVisitor* visitor,
11 	HashString& string)
12 {
13 	StringData stringData;
14 	stringData.SetTo(string.GetString());
15 	visitor->Visit(member, stringData);
16 }
17 
18 
19 // #pragma mark -
20 // #pragma mark ----- ShareInfo -----
21 
22 // constructor
23 ShareInfo::ShareInfo()
24 	: FlattenableRequestMember(),
25 	  fShareName()
26 {
27 }
28 
29 // IsValid
30 bool
31 ShareInfo::IsValid() const
32 {
33 	return (fShareName.GetLength() > 0);
34 }
35 
36 // ShowAround
37 void
38 ShareInfo::ShowAround(RequestMemberVisitor* visitor)
39 {
40 	VisitString(this, visitor, fShareName);
41 }
42 
43 // Flatten
44 status_t
45 ShareInfo::Flatten(RequestFlattener* flattener)
46 {
47 	return flattener->WriteString(fShareName.GetString());
48 }
49 
50 // Unflatten
51 status_t
52 ShareInfo::Unflatten(RequestUnflattener* unflattener)
53 {
54 	return unflattener->ReadString(fShareName);
55 }
56 
57 // SetShareName
58 status_t
59 ShareInfo::SetShareName(const char* shareName)
60 {
61 	return (fShareName.SetTo(shareName) ? B_OK : B_NO_MEMORY);
62 }
63 
64 // GetShareName
65 const char*
66 ShareInfo::GetShareName() const
67 {
68 	return fShareName.GetString();
69 }
70 
71 
72 // #pragma mark -
73 // #pragma mark ----- ServerInfo -----
74 
75 // constructor
76 ServerInfo::ServerInfo()
77 	: FlattenableRequestMember(),
78 	  fServerName(),
79 	  fConnectionMethod(),
80 	  fShareInfos()
81 {
82 }
83 
84 // constructor
85 ServerInfo::ServerInfo(const ServerInfo& other)
86 	: FlattenableRequestMember(),
87 	  fServerName(),
88 	  fConnectionMethod(),
89 	  fShareInfos()
90 {
91 	(*this) = other;
92 }
93 
94 // ShowAround
95 void
96 ServerInfo::ShowAround(RequestMemberVisitor* visitor)
97 {
98 	VisitString(this, visitor, fServerName);
99 	VisitString(this, visitor, fConnectionMethod);
100 	int32 count = fShareInfos.Count();
101 	visitor->Visit(this, count);
102 	for (int32 i = 0; i < count; i++)
103 		visitor->Visit(this, fShareInfos.ElementAt(i));
104 }
105 
106 // Flatten
107 status_t
108 ServerInfo::Flatten(RequestFlattener* flattener)
109 {
110 	flattener->WriteString(fServerName.GetString());
111 	flattener->WriteString(fConnectionMethod.GetString());
112 
113 	int32 count = fShareInfos.Count();
114 	flattener->WriteInt32(count);
115 	for (int32 i = 0; i < count; i++)
116 		flattener->Visit(this, fShareInfos.ElementAt(i));
117 
118 	return flattener->GetStatus();
119 }
120 
121 // Unflatten
122 status_t
123 ServerInfo::Unflatten(RequestUnflattener* unflattener)
124 {
125 	unflattener->ReadString(fServerName);
126 	unflattener->ReadString(fConnectionMethod);
127 
128 	int32 count;
129 	if (unflattener->ReadInt32(count) != B_OK)
130 		return unflattener->GetStatus();
131 
132 	for (int32 i = 0; i < count; i++) {
133 		ShareInfo info;
134 		unflattener->Visit(this, info);
135 		if (info.IsValid())
136 			AddShare(info.GetShareName());
137 	}
138 
139 	return unflattener->GetStatus();
140 }
141 
142 // SetServerName
143 status_t
144 ServerInfo::SetServerName(const char* serverName)
145 {
146 	return (fServerName.SetTo(serverName) ? B_OK : B_NO_MEMORY);
147 }
148 
149 // GetServerName
150 const char*
151 ServerInfo::GetServerName() const
152 {
153 	return fServerName.GetString();
154 }
155 
156 // SetConnectionMethod
157 status_t
158 ServerInfo::SetConnectionMethod(const char* connectionMethod)
159 {
160 	return (fConnectionMethod.SetTo(connectionMethod) ? B_OK : B_NO_MEMORY);
161 }
162 
163 // GetConnectionMethod
164 const char*
165 ServerInfo::GetConnectionMethod() const
166 {
167 	return fConnectionMethod.GetString();
168 }
169 
170 // AddShare
171 status_t
172 ServerInfo::AddShare(const char* shareName)
173 {
174 	ShareInfo shareInfo;
175 	status_t error = shareInfo.SetShareName(shareName);
176 	if (error == B_OK)
177 		error = fShareInfos.PushBack(shareInfo);
178 	return error;
179 }
180 
181 // CountShares
182 int32
183 ServerInfo::CountShares() const
184 {
185 	return fShareInfos.Count();
186 }
187 
188 // ShareInfoAt
189 const ShareInfo&
190 ServerInfo::ShareInfoAt(int32 index) const
191 {
192 	return fShareInfos.ElementAt(index);
193 }
194 
195 // =
196 ServerInfo&
197 ServerInfo::operator=(const ServerInfo& other)
198 {
199 	fServerName = other.fServerName;
200 	fConnectionMethod = other.fConnectionMethod;
201 	fShareInfos.MakeEmpty();
202 	int32 count = other.fShareInfos.Count();
203 	for (int32 i = 0; i < count; i++)
204 		fShareInfos.PushBack(other.fShareInfos.ElementAt(i));
205 	return *this;
206 }
207 
208