From d1f3d95c969b490010e550953fc82fd93c137b1b Mon Sep 17 00:00:00 2001 From: Audrius Butkevicius Date: Wed, 22 Jul 2015 22:34:05 +0100 Subject: [PATCH] Add ability to lookup relay status --- cmd/relaysrv/client/client.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/cmd/relaysrv/client/client.go b/cmd/relaysrv/client/client.go index 7169e6a8b..48e97b400 100644 --- a/cmd/relaysrv/client/client.go +++ b/cmd/relaysrv/client/client.go @@ -12,6 +12,7 @@ import ( syncthingprotocol "github.com/syncthing/protocol" "github.com/syncthing/relaysrv/protocol" + "github.com/syncthing/syncthing/internal/sync" ) type ProtocolClient struct { @@ -28,6 +29,9 @@ type ProtocolClient struct { stopped chan struct{} conn *tls.Conn + + mut sync.RWMutex + connected bool } func NewProtocolClient(uri *url.URL, certs []tls.Certificate, invitations chan protocol.SessionInvitation) *ProtocolClient { @@ -49,6 +53,9 @@ func NewProtocolClient(uri *url.URL, certs []tls.Certificate, invitations chan p stop: make(chan struct{}), stopped: make(chan struct{}), + + mut: sync.NewRWMutex(), + connected: false, } } @@ -82,6 +89,9 @@ func (c *ProtocolClient) Serve() { } defer c.cleanup() + c.mut.Lock() + c.connected = true + c.mut.Unlock() messages := make(chan interface{}) errors := make(chan error, 1) @@ -149,6 +159,13 @@ func (c *ProtocolClient) Stop() { <-c.stopped } +func (c *ProtocolClient) StatusOK() bool { + c.mut.RLock() + con := c.connected + c.mut.RUnlock() + return con +} + func (c *ProtocolClient) String() string { return fmt.Sprintf("ProtocolClient@%p", c) } @@ -187,6 +204,10 @@ func (c *ProtocolClient) cleanup() { l.Debugln(c, "cleaning up") } + c.mut.Lock() + c.connected = false + c.mut.Unlock() + c.conn.Close() }