1 /*
2 * Copyright 2016-2017, Rene Gollent, rene@gollent.com.
3 * Distributed under the terms of the MIT License.
4 */
5 #include "NetworkTargetHostInterfaceInfo.h"
6
7 #include <AutoDeleter.h>
8
9 #include "NetworkTargetHostInterface.h"
10 #include "SettingsDescription.h"
11 #include "Settings.h"
12 #include "Setting.h"
13
14
15 static const char* kHostnameSetting = "hostname";
16 static const char* kPortSetting = "port";
17
18
NetworkTargetHostInterfaceInfo()19 NetworkTargetHostInterfaceInfo::NetworkTargetHostInterfaceInfo()
20 :
21 TargetHostInterfaceInfo("Network"),
22 fDescription(NULL)
23 {
24 }
25
26
~NetworkTargetHostInterfaceInfo()27 NetworkTargetHostInterfaceInfo::~NetworkTargetHostInterfaceInfo()
28 {
29 if (fDescription != NULL)
30 fDescription->ReleaseReference();
31 }
32
33
34 status_t
Init()35 NetworkTargetHostInterfaceInfo::Init()
36 {
37 fDescription = new(std::nothrow) SettingsDescription;
38 if (fDescription == NULL)
39 return B_NO_MEMORY;
40
41 Setting* setting = new(std::nothrow) StringSettingImpl(kHostnameSetting,
42 "Hostname", "");
43 BReference<Setting> settingsReference(setting, true);
44 if (setting == NULL)
45 return B_NO_MEMORY;
46 if (!fDescription->AddSetting(setting))
47 return B_NO_MEMORY;
48
49 setting = new(std::nothrow) BoundedSettingImpl(kPortSetting, "Port",
50 (uint16)1, (uint16)65535, (uint16)8305);
51 if (setting == NULL)
52 return B_NO_MEMORY;
53
54 settingsReference.SetTo(setting, true);
55 if (!fDescription->AddSetting(setting))
56 return B_NO_MEMORY;
57
58 return B_OK;
59 }
60
61
62 bool
IsLocal() const63 NetworkTargetHostInterfaceInfo::IsLocal() const
64 {
65 return false;
66 }
67
68
69 bool
IsConfigured(Settings * settings) const70 NetworkTargetHostInterfaceInfo::IsConfigured(Settings* settings) const
71 {
72 BVariant hostSetting = settings->Value(kHostnameSetting);
73 BVariant portSetting = settings->Value(kPortSetting);
74
75 if (hostSetting.Type() != B_STRING_TYPE || !portSetting.IsNumber())
76 return false;
77
78 if (strlen(hostSetting.ToString()) == 0)
79 return false;
80
81 return true;
82 }
83
84
85 SettingsDescription*
GetSettingsDescription() const86 NetworkTargetHostInterfaceInfo::GetSettingsDescription() const
87 {
88 return fDescription;
89 }
90
91
92 status_t
CreateInterface(Settings * settings,TargetHostInterface * & _interface) const93 NetworkTargetHostInterfaceInfo::CreateInterface(Settings* settings,
94 TargetHostInterface*& _interface) const
95 {
96 NetworkTargetHostInterface* interface
97 = new(std::nothrow) NetworkTargetHostInterface;
98 if (interface == NULL)
99 return B_NO_MEMORY;
100
101 status_t error = interface->Init(settings);
102 if (error != B_OK) {
103 delete interface;
104 return error;
105 }
106
107 _interface = interface;
108 return B_OK;
109 }
110
111