44#include < boost/thread/once.hpp>
55#include < fstream>
66
7- // === implementation of the api_config class ===
8-
97using namespace lsl ;
108
11- // / Helper function: Substitute the "~" character by the full home directory (according to environment variables).
9+ // / Substitute the "~" character by the full home directory (according to environment variables).
1210std::string expand_tilde (const std::string &filename) {
1311 if (!filename.empty () && filename[0 ] == ' ~' ) {
1412 std::string homedir;
@@ -28,29 +26,25 @@ std::string expand_tilde(const std::string &filename) {
2826 return filename;
2927}
3028
31- // / Helper function: Parse a set specifier (a string of the form {a, b, c, ...}) into a vector of strings.
29+ // / Parse a set specifier (a string of the form {a, b, c, ...}) into a vector of strings.
3230static std::vector<std::string> parse_set (const std::string &setstr) {
3331 std::vector<std::string> result;
34- if ((setstr.size () > 2 ) && setstr[0 ] == ' {' && setstr[setstr.size ()- 1 ] == ' }' ) {
35- result = splitandtrim (setstr.substr (1 ,setstr.size ()- 2 ), ' ,' , false );
32+ if ((setstr.size () > 2 ) && setstr[0 ] == ' {' && setstr[setstr.size () - 1 ] == ' }' ) {
33+ result = splitandtrim (setstr.substr (1 , setstr.size () - 2 ), ' ,' , false );
3634 }
3735 return result;
3836}
3937
40- // Returns true if the file exists and is openable for reading
41- bool file_is_readable (const std::string& filename) {
38+ // / Returns true if the file exists and is openable for reading
39+ bool file_is_readable (const std::string & filename) {
4240 std::ifstream f (filename.c_str ());
4341 return f.good ();
4442}
4543
46- /* *
47- * Constructor.
48- * Applies default settings and overrides them based on a config file (if present).
49- */
5044api_config::api_config () {
5145 // for each config file location under consideration...
5246 std::vector<std::string> filenames;
53- if (getenv (" LSLAPICFG" )) {
47+ if (getenv (" LSLAPICFG" )) {
5448 std::string envcfg (getenv (" LSLAPICFG" ));
5549 if (!file_is_readable (envcfg))
5650 LOG_F (ERROR, " LSLAPICFG file %s not found" , envcfg.c_str ());
@@ -68,7 +62,7 @@ api_config::api_config() {
6862 // successful: finished
6963 return ;
7064 }
71- } catch (std::exception &e) {
65+ } catch (std::exception &e) {
7266 LOG_F (ERROR, " Error trying to load config file %s: %s" , filename.c_str (), e.what ());
7367 }
7468 }
@@ -77,29 +71,25 @@ api_config::api_config() {
7771}
7872
7973
80- /* *
81- * Load a configuration file (or use defaults if a filename is empty).
82- * Expects a proper platform-native file name. Throws if there's an error.
83- */
8474void api_config::load_from_file (const std::string &filename) {
8575 try {
8676 INI pt;
87- if (!filename.empty ()) {
77+ if (!filename.empty ()) {
8878 std::ifstream infile (filename);
89- if (infile.good ())
90- pt.load (infile);
79+ if (infile.good ()) pt.load (infile);
9180 }
9281
9382 // read out the [ports] parameters
94- multicast_port_ = pt.get (" ports.MulticastPort" ,16571 );
83+ multicast_port_ = pt.get (" ports.MulticastPort" , 16571 );
9584 base_port_ = pt.get (" ports.BasePort" , 16572 );
9685 port_range_ = pt.get (" ports.PortRange" , 32 );
9786 allow_random_ports_ = pt.get (" ports.AllowRandomPorts" , true );
9887 std::string ipv6_str = pt.get (" ports.IPv6" ,
9988#ifdef __APPLE__
100- " disable" ); // on Mac OS (10.7) there's a bug in the IPv6 implementation that breaks LSL when it tries to use both v4 and v6
89+ " disable" ); // on Mac OS (10.7) there's a bug in the IPv6 implementation that breaks LSL
90+ // when it tries to use both v4 and v6
10191#else
102- " allow" );
92+ " allow" );
10393#endif
10494 allow_ipv4_ = true ;
10595 allow_ipv6_ = true ;
@@ -114,110 +104,121 @@ void api_config::load_from_file(const std::string &filename) {
114104 throw std::runtime_error (" Unsupported setting for the IPv6 parameter." );
115105
116106 // read the [multicast] parameters
117- resolve_scope_ = pt.get (" multicast.ResolveScope" ," site" );
118- listen_address_ = pt.get (" multicast.ListenAddress" ," " );
107+ resolve_scope_ = pt.get (" multicast.ResolveScope" , " site" );
108+ listen_address_ = pt.get (" multicast.ListenAddress" , " " );
119109 // Note about multicast addresses: IPv6 multicast addresses should be
120110 // FF0x::1 (see RFC2373, RFC1884) or a predefined multicast group
121- std::string ipv6_multicast_group = pt.get (" multicast.IPv6MulticastGroup" , " 113D:6FDD:2C17:A643:FFE2:1BD1:3CD2" );
122- std::vector<std::string> machine_group = parse_set (pt.get (" multicast.MachineAddresses" ," {127.0.0.1}" ));
111+ std::string ipv6_multicast_group =
112+ pt.get (" multicast.IPv6MulticastGroup" , " 113D:6FDD:2C17:A643:FFE2:1BD1:3CD2" );
113+ std::vector<std::string> machine_group =
114+ parse_set (pt.get (" multicast.MachineAddresses" , " {127.0.0.1}" ));
123115 // 224.0.0.1 is the group for all directly connected hosts (RFC1112)
124- std::vector<std::string> link_group = parse_set (pt.get (" multicast.LinkAddresses" ," {255.255.255.255, 224.0.0.1, 224.0.0.183}" ));
116+ std::vector<std::string> link_group = parse_set (
117+ pt.get (" multicast.LinkAddresses" , " {255.255.255.255, 224.0.0.1, 224.0.0.183}" ));
125118 // Multicast groups defined by the organization (and therefore subject
126119 // to filtering / forwarding are in the 239.192.0.0/14 subnet (RFC2365)
127- std::vector<std::string> site_group = parse_set (pt.get (" multicast.SiteAddresses" ," {239.255.172.215}" ));
120+ std::vector<std::string> site_group =
121+ parse_set (pt.get (" multicast.SiteAddresses" , " {239.255.172.215}" ));
128122 // Organization groups use the same broadcast addresses (IPv4), but
129123 // have a larger TTL. On the network site, it requires the routers
130124 // to forward the broadcast packets (both IGMP and UDP)
131- std::vector<std::string> organization_group = parse_set (pt.get (" multicast.OrganizationAddresses" ," {}" ));
132- std::vector<std::string> global_group = parse_set (pt.get (" multicast.GlobalAddresses" ," {}" ));
133- enum {
134- machine = 0 ,
135- link,
136- site,
137- organization,
138- global
139- } scope;
125+ std::vector<std::string> organization_group =
126+ parse_set (pt.get (" multicast.OrganizationAddresses" , " {}" ));
127+ std::vector<std::string> global_group =
128+ parse_set (pt.get (" multicast.GlobalAddresses" , " {}" ));
129+ enum { machine = 0 , link, site, organization, global } scope;
140130 // construct list of addresses & TTL according to the ResolveScope.
141- if (resolve_scope_ == " machine" ) scope = machine;
142- else if (resolve_scope_ == " link" ) scope = link;
143- else if (resolve_scope_ == " site" ) scope = site;
144- else if (resolve_scope_ == " organization" ) scope = organization;
145- else if (resolve_scope_ == " global" ) scope = global;
146- else throw std::runtime_error (" This ResolveScope setting is unsupported." );
147-
148- multicast_addresses_.insert (multicast_addresses_.end (), machine_group.begin (), machine_group.end ());
131+ if (resolve_scope_ == " machine" )
132+ scope = machine;
133+ else if (resolve_scope_ == " link" )
134+ scope = link;
135+ else if (resolve_scope_ == " site" )
136+ scope = site;
137+ else if (resolve_scope_ == " organization" )
138+ scope = organization;
139+ else if (resolve_scope_ == " global" )
140+ scope = global;
141+ else
142+ throw std::runtime_error (" This ResolveScope setting is unsupported." );
143+
144+ multicast_addresses_.insert (
145+ multicast_addresses_.end (), machine_group.begin (), machine_group.end ());
149146 multicast_ttl_ = 0 ;
150147
151- if (scope >= link) {
152- multicast_addresses_.insert (multicast_addresses_.end (),link_group.begin (),link_group.end ());
148+ if (scope >= link) {
149+ multicast_addresses_.insert (
150+ multicast_addresses_.end (), link_group.begin (), link_group.end ());
153151 multicast_addresses_.push_back (" FF02:" + ipv6_multicast_group);
154152 multicast_ttl_ = 1 ;
155153 }
156- if (scope >= site) {
157- multicast_addresses_.insert (multicast_addresses_.end (),site_group.begin (),site_group.end ());
154+ if (scope >= site) {
155+ multicast_addresses_.insert (
156+ multicast_addresses_.end (), site_group.begin (), site_group.end ());
158157 multicast_addresses_.push_back (" FF05:" + ipv6_multicast_group);
159158 multicast_ttl_ = 24 ;
160159 }
161- if (scope >= organization) {
162- multicast_addresses_.insert (multicast_addresses_.end (),organization_group.begin (),organization_group.end ());
160+ if (scope >= organization) {
161+ multicast_addresses_.insert (
162+ multicast_addresses_.end (), organization_group.begin (), organization_group.end ());
163163 multicast_addresses_.push_back (" FF08:" + ipv6_multicast_group);
164164 multicast_ttl_ = 32 ;
165165 }
166- if (scope >= global) {
167- multicast_addresses_.insert (multicast_addresses_.end (),global_group.begin (),global_group.end ());
166+ if (scope >= global) {
167+ multicast_addresses_.insert (
168+ multicast_addresses_.end (), global_group.begin (), global_group.end ());
168169 multicast_addresses_.push_back (" FF0E:" + ipv6_multicast_group);
169170 multicast_ttl_ = 255 ;
170171 }
171172
172173 // apply overrides, if any
173- int ttl_override = pt.get (" multicast.TTLOverride" ,-1 );
174- std::vector<std::string> address_override = parse_set (pt.get (" multicast.AddressesOverride" ," {}" ));
175- if (ttl_override >= 0 )
176- multicast_ttl_ = ttl_override;
177- if (!address_override.empty ())
178- multicast_addresses_ = address_override;
174+ int ttl_override = pt.get (" multicast.TTLOverride" , -1 );
175+ std::vector<std::string> address_override =
176+ parse_set (pt.get (" multicast.AddressesOverride" , " {}" ));
177+ if (ttl_override >= 0 ) multicast_ttl_ = ttl_override;
178+ if (!address_override.empty ()) multicast_addresses_ = address_override;
179179
180180 // read the [lab] settings
181- known_peers_ = parse_set (pt.get (" lab.KnownPeers" ," {}" ));
182- session_id_ = pt.get (" lab.SessionID" ," default" );
181+ known_peers_ = parse_set (pt.get (" lab.KnownPeers" , " {}" ));
182+ session_id_ = pt.get (" lab.SessionID" , " default" );
183183
184184 // read the [tuning] settings
185- use_protocol_version_ = std::min (LSL_PROTOCOL_VERSION,pt.get (" tuning.UseProtocolVersion" ,LSL_PROTOCOL_VERSION));
186- watchdog_check_interval_ = pt.get (" tuning.WatchdogCheckInterval" ,15.0 );
187- watchdog_time_threshold_ = pt.get (" tuning.WatchdogTimeThreshold" ,15.0 );
188- multicast_min_rtt_ = pt.get (" tuning.MulticastMinRTT" ,0.5 );
189- multicast_max_rtt_ = pt.get (" tuning.MulticastMaxRTT" ,3.0 );
190- unicast_min_rtt_ = pt.get (" tuning.UnicastMinRTT" ,0.75 );
191- unicast_max_rtt_ = pt.get (" tuning.UnicastMaxRTT" ,5.0 );
192- continuous_resolve_interval_ = pt.get (" tuning.ContinuousResolveInterval" ,0.5 );
193- timer_resolution_ = pt.get (" tuning.TimerResolution" ,1 );
194- max_cached_queries_ = pt.get (" tuning.MaxCachedQueries" ,100 );
195- time_update_interval_ = pt.get (" tuning.TimeUpdateInterval" ,2.0 );
196- time_update_minprobes_ = pt.get (" tuning.TimeUpdateMinProbes" ,6 );
197- time_probe_count_ = pt.get (" tuning.TimeProbeCount" ,8 );
198- time_probe_interval_ = pt.get (" tuning.TimeProbeInterval" ,0.064 );
199- time_probe_max_rtt_ = pt.get (" tuning.TimeProbeMaxRTT" ,0.128 );
200- outlet_buffer_reserve_ms_ = pt.get (" tuning.OutletBufferReserveMs" ,5000 );
201- outlet_buffer_reserve_samples_ = pt.get (" tuning.OutletBufferReserveSamples" ,128 );
202- inlet_buffer_reserve_ms_ = pt.get (" tuning.InletBufferReserveMs" ,5000 );
203- inlet_buffer_reserve_samples_ = pt.get (" tuning.InletBufferReserveSamples" ,128 );
204- smoothing_halftime_ = pt.get (" tuning.SmoothingHalftime" ,90 .0f );
185+ use_protocol_version_ = std::min (
186+ LSL_PROTOCOL_VERSION, pt.get (" tuning.UseProtocolVersion" , LSL_PROTOCOL_VERSION));
187+ watchdog_check_interval_ = pt.get (" tuning.WatchdogCheckInterval" , 15.0 );
188+ watchdog_time_threshold_ = pt.get (" tuning.WatchdogTimeThreshold" , 15.0 );
189+ multicast_min_rtt_ = pt.get (" tuning.MulticastMinRTT" , 0.5 );
190+ multicast_max_rtt_ = pt.get (" tuning.MulticastMaxRTT" , 3.0 );
191+ unicast_min_rtt_ = pt.get (" tuning.UnicastMinRTT" , 0.75 );
192+ unicast_max_rtt_ = pt.get (" tuning.UnicastMaxRTT" , 5.0 );
193+ continuous_resolve_interval_ = pt.get (" tuning.ContinuousResolveInterval" , 0.5 );
194+ timer_resolution_ = pt.get (" tuning.TimerResolution" , 1 );
195+ max_cached_queries_ = pt.get (" tuning.MaxCachedQueries" , 100 );
196+ time_update_interval_ = pt.get (" tuning.TimeUpdateInterval" , 2.0 );
197+ time_update_minprobes_ = pt.get (" tuning.TimeUpdateMinProbes" , 6 );
198+ time_probe_count_ = pt.get (" tuning.TimeProbeCount" , 8 );
199+ time_probe_interval_ = pt.get (" tuning.TimeProbeInterval" , 0.064 );
200+ time_probe_max_rtt_ = pt.get (" tuning.TimeProbeMaxRTT" , 0.128 );
201+ outlet_buffer_reserve_ms_ = pt.get (" tuning.OutletBufferReserveMs" , 5000 );
202+ outlet_buffer_reserve_samples_ = pt.get (" tuning.OutletBufferReserveSamples" , 128 );
203+ inlet_buffer_reserve_ms_ = pt.get (" tuning.InletBufferReserveMs" , 5000 );
204+ inlet_buffer_reserve_samples_ = pt.get (" tuning.InletBufferReserveSamples" , 128 );
205+ smoothing_halftime_ = pt.get (" tuning.SmoothingHalftime" , 90 .0f );
205206 force_default_timestamps_ = pt.get (" tuning.ForceDefaultTimestamps" , false );
206207
207208 // read the [log] settings
208209 int log_level = pt.get (" log.level" , -1 );
209- if (log_level < -3 || log_level > 9 )
210+ if (log_level < -3 || log_level > 9 )
210211 throw std::runtime_error (" Invalid log.level (valid range: -3 to 9" );
211212
212213 std::string log_file = pt.get (" log.file" , " " );
213- if (!log_file.empty ()) {
214+ if (!log_file.empty ()) {
214215 loguru::add_file (log_file.c_str (), loguru::Append, log_level);
215216 // don't duplicate log to stderr
216217 loguru::g_stderr_verbosity = -9 ;
217218 } else
218219 loguru::g_stderr_verbosity = log_level;
219220
220- } catch (std::exception &e) {
221+ } catch (std::exception &e) {
221222 LOG_F (ERROR, " Error parsing config file '%s': '%s', rolling back to defaults" ,
222223 filename.c_str (), e.what ());
223224 // any error: assign defaults
@@ -227,13 +228,8 @@ void api_config::load_from_file(const std::string &filename) {
227228 }
228229}
229230
230-
231-
232- /* *
233- * Instantiate / retrieve singleton.
234- */
235231const api_config *api_config::get_instance () {
236- lslboost::call_once (&called_once,once_flag);
232+ lslboost::call_once (&called_once, once_flag);
237233 return get_instance_internal ();
238234}
239235
@@ -245,4 +241,3 @@ api_config *api_config::get_instance_internal() {
245241void api_config::called_once () { get_instance_internal (); }
246242
247243lslboost::once_flag api_config::once_flag = BOOST_ONCE_INIT;
248-
0 commit comments