| 1 |
/* |
| 2 |
Copyright 2013-2014 Canonical Ltd. |
| 3 |
|
| 4 |
This program is free software: you can redistribute it and/or modify it |
| 5 |
under the terms of the GNU General Public License version 3, as published |
| 6 |
by the Free Software Foundation. |
| 7 |
|
| 8 |
This program is distributed in the hope that it will be useful, but |
| 9 |
WITHOUT ANY WARRANTY; without even the implied warranties of |
| 10 |
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 11 |
PURPOSE. See the GNU General Public License for more details. |
| 12 |
|
| 13 |
You should have received a copy of the GNU General Public License along |
| 14 |
with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
package server |
| 18 |
|
| 19 |
import ( |
| 20 |
"net" |
| 21 |
"syscall" |
| 22 |
"time" |
| 23 |
|
| 24 |
"gitlab.com/ubports/development/core/lomiri-push-service/config" |
| 25 |
"gitlab.com/ubports/development/core/lomiri-push-service/logger" |
| 26 |
"gitlab.com/ubports/development/core/lomiri-push-service/server/listener" |
| 27 |
) |
| 28 |
|
| 29 |
// A DevicesParsedConfig holds and can be used to parse the device server config. |
| 30 |
type DevicesParsedConfig struct { |
| 31 |
// session configuration |
| 32 |
ParsedPingInterval config.ConfigTimeDuration `json:"ping_interval"` |
| 33 |
ParsedExchangeTimeout config.ConfigTimeDuration `json:"exchange_timeout"` |
| 34 |
// broker configuration |
| 35 |
ParsedSessionQueueSize config.ConfigQueueSize `json:"session_queue_size"` |
| 36 |
ParsedBrokerQueueSize config.ConfigQueueSize `json:"broker_queue_size"` |
| 37 |
// device listener configuration |
| 38 |
ParsedAddr config.ConfigHostPort `json:"addr"` |
| 39 |
TLSParsedConfig |
| 40 |
} |
| 41 |
|
| 42 |
func (cfg *DevicesParsedConfig) PingInterval() time.Duration { |
| 43 |
return cfg.ParsedPingInterval.TimeDuration() |
| 44 |
} |
| 45 |
|
| 46 |
func (cfg *DevicesParsedConfig) ExchangeTimeout() time.Duration { |
| 47 |
return cfg.ParsedExchangeTimeout.TimeDuration() |
| 48 |
} |
| 49 |
|
| 50 |
func (cfg *DevicesParsedConfig) SessionQueueSize() uint { |
| 51 |
return cfg.ParsedSessionQueueSize.QueueSize() |
| 52 |
} |
| 53 |
|
| 54 |
func (cfg *DevicesParsedConfig) BrokerQueueSize() uint { |
| 55 |
return cfg.ParsedBrokerQueueSize.QueueSize() |
| 56 |
} |
| 57 |
|
| 58 |
func (cfg *DevicesParsedConfig) Addr() string { |
| 59 |
return cfg.ParsedAddr.HostPort() |
| 60 |
} |
| 61 |
|
| 62 |
// DevicesRunner returns a function to accept device connections. |
| 63 |
// If adoptLst is not nil it will be used as the underlying listener, instead |
| 64 |
// of creating one, wrapped in a TLS layer. |
| 65 |
func DevicesRunner(adoptLst net.Listener, session func(net.Conn) error, logger logger.Logger, resource listener.SessionResourceManager, parsedCfg *DevicesParsedConfig) func() { |
| 66 |
BootLogger.Debugf("PingInterval: %s, ExchangeTimeout %s", parsedCfg.PingInterval(), parsedCfg.ExchangeTimeout()) |
| 67 |
var rlim syscall.Rlimit |
| 68 |
err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlim) |
| 69 |
if err != nil { |
| 70 |
BootLogFatalf("getrlimit failed: %v", err) |
| 71 |
} |
| 72 |
BootLogger.Debugf("nofile soft: %d hard: %d", rlim.Cur, rlim.Max) |
| 73 |
lst, err := listener.DeviceListen(adoptLst, parsedCfg) |
| 74 |
if err != nil { |
| 75 |
BootLogFatalf("start device listening: %v", err) |
| 76 |
} |
| 77 |
BootLogListener("devices", lst) |
| 78 |
return func() { |
| 79 |
err = lst.AcceptLoop(session, resource, logger) |
| 80 |
if err != nil { |
| 81 |
BootLogFatalf("accepting device connections: %v", err) |
| 82 |
} |
| 83 |
} |
| 84 |
} |