Skip to content
Snippets Groups Projects
Commit aa44f972 authored by Jake Taylor's avatar Jake Taylor
Browse files

Merge branch 'release' into XX-3134/GwHostPool

# Conflicts:
#	go.mod
#	go.sum
parents 5214c9fe 2f658496
No related branches found
No related tags found
No related merge requests found
...@@ -96,3 +96,17 @@ func newRoundListUnregister(rounds []id.Round, ec []*dataStructures.EventCallbac ...@@ -96,3 +96,17 @@ func newRoundListUnregister(rounds []id.Round, ec []*dataStructures.EventCallbac
type ClientError interface { type ClientError interface {
Report(source, message, trace string) Report(source, message, trace string)
} }
type LogWriter interface{
Log(string)
}
type writerAdapter struct{
lw LogWriter
}
func (wa *writerAdapter)Write(p []byte) (n int, err error){
wa.lw.Log(string(p))
return len(p), nil
}
...@@ -19,9 +19,13 @@ import ( ...@@ -19,9 +19,13 @@ import (
"gitlab.com/elixxir/comms/mixmessages" "gitlab.com/elixxir/comms/mixmessages"
"gitlab.com/elixxir/primitives/states" "gitlab.com/elixxir/primitives/states"
"gitlab.com/xx_network/primitives/id" "gitlab.com/xx_network/primitives/id"
"sync"
"time" "time"
) )
var extantClient bool
var loginMux sync.Mutex
// sets the log level // sets the log level
func init() { func init() {
jww.SetLogThreshold(jww.LevelInfo) jww.SetLogThreshold(jww.LevelInfo)
...@@ -76,6 +80,14 @@ func NewPrecannedClient(precannedID int, network, storageDir string, password [] ...@@ -76,6 +80,14 @@ func NewPrecannedClient(precannedID int, network, storageDir string, password []
// Login does not block on network connection, and instead loads and // Login does not block on network connection, and instead loads and
// starts subprocesses to perform network operations. // starts subprocesses to perform network operations.
func Login(storageDir string, password []byte, parameters string) (*Client, error) { func Login(storageDir string, password []byte, parameters string) (*Client, error) {
loginMux.Lock()
defer loginMux.Unlock()
if extantClient{
return nil, errors.New("cannot login when another session " +
"already exists")
}
// check if a client is already logged in, refuse to login if one is
p, err := params.GetNetworkParameters(parameters) p, err := params.GetNetworkParameters(parameters)
if err != nil { if err != nil {
return nil, errors.New(fmt.Sprintf("Failed to login: %+v", err)) return nil, errors.New(fmt.Sprintf("Failed to login: %+v", err))
...@@ -85,6 +97,7 @@ func Login(storageDir string, password []byte, parameters string) (*Client, erro ...@@ -85,6 +97,7 @@ func Login(storageDir string, password []byte, parameters string) (*Client, erro
if err != nil { if err != nil {
return nil, errors.New(fmt.Sprintf("Failed to login: %+v", err)) return nil, errors.New(fmt.Sprintf("Failed to login: %+v", err))
} }
extantClient=true
return &Client{api: *client}, nil return &Client{api: *client}, nil
} }
...@@ -127,6 +140,11 @@ func LogLevel(level int) error { ...@@ -127,6 +140,11 @@ func LogLevel(level int) error {
return nil return nil
} }
//RegisterLogWriter registers a callback on which logs are written.
func RegisterLogWriter(writer LogWriter){
jww.SetLogOutput(&writerAdapter{lw:writer})
}
//Unmarshals a marshaled contact object, returns an error if it fails //Unmarshals a marshaled contact object, returns an error if it fails
func UnmarshalContact(b []byte) (*Contact, error) { func UnmarshalContact(b []byte) (*Contact, error) {
c, err := contact.Unmarshal(b) c, err := contact.Unmarshal(b)
......
...@@ -9,8 +9,8 @@ import ( ...@@ -9,8 +9,8 @@ import (
"sync" "sync"
) )
const unknownRoundStorageKey = "unknownRoundStorage" const earliestRoundStorageKey = "unknownRoundStorage"
const unknownRoundStorageVersion = 0 const earliestRoundStorageVersion = 0
type EarliestRound struct { type EarliestRound struct {
stored bool stored bool
...@@ -36,14 +36,14 @@ func LoadEarliestRound(kv *versioned.KV) *EarliestRound { ...@@ -36,14 +36,14 @@ func LoadEarliestRound(kv *versioned.KV) *EarliestRound {
rid: 0, rid: 0,
} }
obj, err := kv.Get(unknownRoundStorageKey, unknownRoundStorageVersion) obj, err := kv.Get(earliestRoundStorageKey, earliestRoundStorageVersion)
if err != nil { if err != nil {
jww.FATAL.Panicf("Failed to get the unknown round: %+v", err) jww.FATAL.Panicf("Failed to get the earlest round: %+v", err)
} }
err = json.Unmarshal(obj.Data, &ur.rid) err = json.Unmarshal(obj.Data, &ur.rid)
if err != nil { if err != nil {
jww.FATAL.Panicf("Failed to unmarshal the unknown round: %+v", err) jww.FATAL.Panicf("Failed to unmarshal the earliest round: %+v", err)
} }
return ur return ur
} }
...@@ -52,20 +52,20 @@ func (ur *EarliestRound) save() { ...@@ -52,20 +52,20 @@ func (ur *EarliestRound) save() {
if ur.stored { if ur.stored {
urStr, err := json.Marshal(&ur.rid) urStr, err := json.Marshal(&ur.rid)
if err != nil { if err != nil {
jww.FATAL.Panicf("Failed to marshal the unknown round: %+v", err) jww.FATAL.Panicf("Failed to marshal the earliest round: %+v", err)
} }
// Create versioned object with data // Create versioned object with data
obj := &versioned.Object{ obj := &versioned.Object{
Version: unknownRoundStorageVersion, Version: earliestRoundStorageVersion,
Timestamp: netTime.Now(), Timestamp: netTime.Now(),
Data: urStr, Data: urStr,
} }
err = ur.kv.Set(unknownRoundStorageKey, err = ur.kv.Set(earliestRoundStorageKey,
unknownRoundStorageVersion, obj) earliestRoundStorageVersion, obj)
if err != nil { if err != nil {
jww.FATAL.Panicf("Failed to store the unknown round: %+v", err) jww.FATAL.Panicf("Failed to store the earliest round: %+v", err)
} }
} }
...@@ -92,8 +92,8 @@ func (ur *EarliestRound) Get() id.Round { ...@@ -92,8 +92,8 @@ func (ur *EarliestRound) Get() id.Round {
func (ur *EarliestRound) delete() { func (ur *EarliestRound) delete() {
ur.mux.Lock() ur.mux.Lock()
defer ur.mux.Unlock() defer ur.mux.Unlock()
err := ur.kv.Delete(unknownRoundStorageKey, unknownRoundStorageVersion) err := ur.kv.Delete(earliestRoundStorageKey, earliestRoundStorageVersion)
if err != nil { if err != nil {
jww.FATAL.Panicf("Failed to delete unknownRound storage: %+v", err) jww.FATAL.Panicf("Failed to delete earliest storage: %+v", err)
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment