xref: /haiku/src/kits/debugger/target_host_interface/network/NetworkTargetHostInterfaceInfo.cpp (revision 3995592cdf304335132305e27c40cbb0b1ac46e3)
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 
19 NetworkTargetHostInterfaceInfo::NetworkTargetHostInterfaceInfo()
20 	:
21 	TargetHostInterfaceInfo("Network"),
22 	fDescription(NULL)
23 {
24 }
25 
26 
27 NetworkTargetHostInterfaceInfo::~NetworkTargetHostInterfaceInfo()
28 {
29 	delete fDescription;
30 }
31 
32 
33 status_t
34 NetworkTargetHostInterfaceInfo::Init()
35 {
36 	fDescription = new(std::nothrow) SettingsDescription;
37 	if (fDescription == NULL)
38 		return B_NO_MEMORY;
39 
40 	Setting* setting = new(std::nothrow) StringSettingImpl(kHostnameSetting,
41 		"Hostname", "");
42 	if (setting == NULL)
43 		return B_NO_MEMORY;
44 	ObjectDeleter<Setting> settingDeleter(setting);
45 	if (!fDescription->AddSetting(setting))
46 		return B_NO_MEMORY;
47 
48 	settingDeleter.Detach();
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 	if (!fDescription->AddSetting(setting)) {
54 		delete setting;
55 		return B_NO_MEMORY;
56 	}
57 
58 	return B_OK;
59 }
60 
61 
62 bool
63 NetworkTargetHostInterfaceInfo::IsLocal() const
64 {
65 	return false;
66 }
67 
68 
69 bool
70 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*
86 NetworkTargetHostInterfaceInfo::GetSettingsDescription() const
87 {
88 	return fDescription;
89 }
90 
91 
92 status_t
93 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