Add ConnectionChange API call to backend for handling network state changes

This commit is contained in:
Ivan Danyliuk 2018-02-09 22:28:16 +02:00 committed by Frank Mueller
parent a2431b3e2b
commit 4982e1b8b8
5 changed files with 151 additions and 0 deletions

View File

@ -221,3 +221,15 @@ func (api *StatusAPI) NotifyUsers(message string, payload fcm.NotificationPayloa
return err
}
// ConnectionChange handles network state changes logic.
func (api *StatusAPI) ConnectionChange(typ string, expensive bool) {
state := ConnectionState{
Type: NewConnectionType(typ),
Expensive: expensive,
}
if typ == "none" {
state.Offline = true
}
api.b.ConnectionChange(state)
}

View File

@ -30,6 +30,7 @@ type StatusBackend struct {
txQueueManager *transactions.Manager
jailManager common.JailManager
newNotification common.NotificationConstructor
connectionState ConnectionState
}
// NewStatusBackend create a new NewStatusBackend instance
@ -226,3 +227,12 @@ func (b *StatusBackend) registerHandlers() error {
rpcClient.RegisterHandler("eth_sendTransaction", b.txQueueManager.SendTransactionRPCHandler)
return nil
}
// ConnectionChange handles network state changes logic.
func (m *StatusBackend) ConnectionChange(state ConnectionState) {
log.Info("Network state change", "old", m.connectionState, "new", state)
m.connectionState = state
// logic of handling state changes here
// restart node? force peers reconnect? etc
}

64
geth/api/connection.go Normal file
View File

@ -0,0 +1,64 @@
package api
import (
"fmt"
)
// ConnectionState represents device connection state and type,
// as reported by mobile framework.
//
// Zero value represents default assumption about network
// connection until first update — online, on cellular and not expensive.
type ConnectionState struct {
Offline bool `json:"offline"`
Type ConnectionType `json:"type"`
Expensive bool `json:"expensive"`
}
// ConnectionType represents description of available
// connection types as reported by React Native (see
// https://facebook.github.io/react-native/docs/netinfo.html)
// We're interested mainly in 'wifi' and 'cellular', but
// other types are also may be used.
type ConnectionType byte
// NewConnectionType creates new ConnectionType from string.
func NewConnectionType(s string) ConnectionType {
switch s {
case "cellular":
return ConnectionCellular
case "wifi":
return ConnectionWifi
}
return ConnectionUnknown
}
const (
ConnectionCellular = iota // default value, LTE, 4G, 3G, EDGE, etc.
ConnectionWifi // WIFI or iOS simulator
ConnectionUnknown
)
// String formats ConnectionState for logs. Implements Stringer.
func (c ConnectionState) String() string {
if c.Offline {
return "offline"
}
var typ string
switch c.Type {
case ConnectionWifi:
typ = "wifi"
case ConnectionCellular:
typ = "cellular"
default:
typ = "unknown"
}
if c.Expensive {
return fmt.Sprintf("%s (expensive)", typ)
}
return typ
}

View File

@ -0,0 +1,59 @@
package api
import "testing"
func TestConnectionType(t *testing.T) {
c := NewConnectionType("wifi")
if c != ConnectionWifi {
t.Fatalf("Wrong connection type: %s", c)
}
c = NewConnectionType("cellular")
if c != ConnectionCellular {
t.Fatalf("Wrong connection type: %s", c)
}
c = NewConnectionType("bluetooth")
if c != ConnectionUnknown {
t.Fatalf("Wrong connection type: %s", c)
}
}
func TestConnectionState(t *testing.T) {
tests := []struct {
name string
state ConnectionState
expected string
}{
{
"zero value",
ConnectionState{},
"cellular",
},
{
"offline",
ConnectionState{Offline: true},
"offline",
},
{
"wifi",
ConnectionState{Type: ConnectionWifi},
"wifi",
},
{
"wifi tethered",
ConnectionState{Type: ConnectionWifi, Expensive: true},
"wifi (expensive)",
},
{
"unknown",
ConnectionState{Type: ConnectionUnknown},
"unknown",
},
}
for _, test := range tests {
str := test.state.String()
if str != test.expected {
t.Fatalf("Expected String() to return '%s', got '%s'", test.expected, str)
}
}
}

View File

@ -449,3 +449,9 @@ func AddPeer(enode *C.char) *C.char {
err := statusAPI.NodeManager().AddPeer(C.GoString(enode))
return makeJSONResponse(err)
}
// ConnectionChange handles network state changes as reported
// by ReactNative (see https://facebook.github.io/react-native/docs/netinfo.html)
func ConnectionChange(typ *C.char, expensive C.int) {
statusAPI.ConnectionChange(C.GoString(typ), expensive == 1)
}