Skip to content

Commit

Permalink
Add auto-reconnecting to remote matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
☃ Elliot Shepherd committed Aug 11, 2015
1 parent 1beb8ce commit 80dc08a
Showing 1 changed file with 44 additions and 12 deletions.
56 changes: 44 additions & 12 deletions remote/RemoteMatrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ package remote

import (
"encoding/gob"
"fmt"
"image"
"io"
"net"
"time"

"github.com/ninjasphere/gestic-tools/go-gestic-sdk"
"github.com/ninjasphere/go-ninja/logger"
)

var log = logger.GetLogger("remote")

type pane interface {
IsEnabled() bool
KeepAwake() bool
Expand All @@ -31,23 +33,49 @@ type Matrix struct {
pane pane
}

func NewMatrix(pane pane, conn net.Conn) *Matrix {
func NewTCPMatrix(pane pane, host string) *Matrix {

matrix := NewMatrix(pane)

// Connect to the led controller remote pane interface

go func() {
for {
tcpAddr, err := net.ResolveTCPAddr("tcp", host)
if err != nil {
println("ResolveTCPAddr failed:", err.Error())
} else {

conn, err := net.DialTCP("tcp", nil, tcpAddr)
if err != nil {
log.Errorf("Dial failed: %s", err)
} else {

log.Infof("Connected")

go matrix.start(conn)

<-matrix.Disconnected

log.Infof("Disconnected")
}
}
log.Infof("Waiting to reconnect")
time.Sleep(time.Second / 2)
}
}()

return matrix
}

func NewMatrix(pane pane) *Matrix {

matrix := &Matrix{
conn: conn,
log: logger.GetLogger("Matrix"),
Disconnected: make(chan bool, 1),
incoming: gob.NewDecoder(conn),
outgoing: gob.NewEncoder(conn),
pane: pane,
}

defer func() {
matrix.outgoing.Encode(&Incoming{Err: fmt.Errorf("Goodbye!")})
}()

go matrix.start()

return matrix
}

Expand All @@ -58,7 +86,11 @@ func (m *Matrix) Close() {
m.Disconnected <- true
}

func (m *Matrix) start() {
func (m *Matrix) start(conn net.Conn) {

m.conn = conn
m.incoming = gob.NewDecoder(m.conn)
m.outgoing = gob.NewEncoder(m.conn)

for {
var msg Outgoing
Expand Down

0 comments on commit 80dc08a

Please sign in to comment.