WaitTillBootstrapped can now Error
This commit is contained in:
parent
5d4cf85d26
commit
384d59e9ef
2
acn.go
2
acn.go
|
@ -38,7 +38,7 @@ type ACN interface {
|
||||||
// On ACN error state it returns -2
|
// On ACN error state it returns -2
|
||||||
GetBootstrapStatus() (int, string)
|
GetBootstrapStatus() (int, string)
|
||||||
// WaitTillBootstrapped Blocks until underlying network is bootstrapped
|
// WaitTillBootstrapped Blocks until underlying network is bootstrapped
|
||||||
WaitTillBootstrapped()
|
WaitTillBootstrapped() error
|
||||||
// Sets the callback function to be called when ACN status changes
|
// Sets the callback function to be called when ACN status changes
|
||||||
SetStatusCallback(callback func(int, string))
|
SetStatusCallback(callback func(int, string))
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package connectivity
|
package connectivity
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
)
|
)
|
||||||
|
@ -19,7 +20,8 @@ func (e ErrorACN) GetBootstrapStatus() (int, string) {
|
||||||
return -1, "error initializing tor"
|
return -1, "error initializing tor"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e ErrorACN) WaitTillBootstrapped() {
|
func (e ErrorACN) WaitTillBootstrapped() error {
|
||||||
|
return errors.New("error initializing tor")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *ErrorACN) SetStatusCallback(callback func(int, string)) {
|
func (e *ErrorACN) SetStatusCallback(callback func(int, string)) {
|
||||||
|
|
|
@ -56,7 +56,8 @@ func (lp *localProvider) GetVersion() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// WaitTillBootstrapped Blocks until underlying network is bootstrapped
|
// WaitTillBootstrapped Blocks until underlying network is bootstrapped
|
||||||
func (lp *localProvider) WaitTillBootstrapped() {
|
func (lp *localProvider) WaitTillBootstrapped() error {
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (lp *localProvider) Listen(identity PrivateKey, port int) (ListenService, error) {
|
func (lp *localProvider) Listen(identity PrivateKey, port int) (ListenService, error) {
|
||||||
|
|
|
@ -37,8 +37,8 @@ func (p *ProxyACN) GetBootstrapStatus() (int, string) {
|
||||||
return p.acn.GetBootstrapStatus()
|
return p.acn.GetBootstrapStatus()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ProxyACN) WaitTillBootstrapped() {
|
func (p *ProxyACN) WaitTillBootstrapped() error {
|
||||||
p.acn.WaitTillBootstrapped()
|
return p.acn.WaitTillBootstrapped()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ProxyACN) SetStatusCallback(callback func(int, string)) {
|
func (p *ProxyACN) SetStatusCallback(callback func(int, string)) {
|
||||||
|
|
|
@ -32,7 +32,11 @@ func TestLaunchTor(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("tor failed to start: %v", err)
|
t.Fatalf("tor failed to start: %v", err)
|
||||||
} else {
|
} else {
|
||||||
acn.WaitTillBootstrapped()
|
err := acn.WaitTillBootstrapped()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("error bootstrapping tor %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
if pid, err := acn.GetPID(); err == nil {
|
if pid, err := acn.GetPID(); err == nil {
|
||||||
t.Logf("tor pid: %v", pid)
|
t.Logf("tor pid: %v", pid)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -72,6 +72,7 @@ type torProvider struct {
|
||||||
statusCallback func(int, string)
|
statusCallback func(int, string)
|
||||||
lastRestartTime time.Time
|
lastRestartTime time.Time
|
||||||
authenticator tor.Authenticator
|
authenticator tor.Authenticator
|
||||||
|
isClosed bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ols *onionListenService) AddressFull() string {
|
func (ols *onionListenService) AddressFull() string {
|
||||||
|
@ -157,14 +158,15 @@ func (tp *torProvider) GetVersion() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// WaitTillBootstrapped Blocks until underlying network is bootstrapped
|
// WaitTillBootstrapped Blocks until underlying network is bootstrapped
|
||||||
func (tp *torProvider) WaitTillBootstrapped() {
|
func (tp *torProvider) WaitTillBootstrapped() error {
|
||||||
for {
|
for !tp.isClosed {
|
||||||
progress, _ := tp.GetBootstrapStatus()
|
progress, _ := tp.GetBootstrapStatus()
|
||||||
if progress == 100 {
|
if progress == 100 {
|
||||||
break
|
return nil
|
||||||
}
|
}
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
}
|
}
|
||||||
|
return errors.New("close called before bootstrap")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tp *torProvider) Listen(identity connectivity.PrivateKey, port int) (connectivity.ListenService, error) {
|
func (tp *torProvider) Listen(identity connectivity.PrivateKey, port int) (connectivity.ListenService, error) {
|
||||||
|
@ -287,9 +289,9 @@ func (tp *torProvider) Close() {
|
||||||
for _, child := range tp.childListeners {
|
for _, child := range tp.childListeners {
|
||||||
child.Close()
|
child.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
tp.lock.Lock()
|
tp.lock.Lock()
|
||||||
defer tp.lock.Unlock()
|
defer tp.lock.Unlock()
|
||||||
|
tp.isClosed = true
|
||||||
tp.breakChan <- true
|
tp.breakChan <- true
|
||||||
if tp.t != nil {
|
if tp.t != nil {
|
||||||
tp.t.Close()
|
tp.t.Close()
|
||||||
|
|
Loading…
Reference in New Issue