Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 45 additions & 21 deletions sforwarder/sforwarder.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func main() {

var opts struct {
Positional struct {
ServerConnectionString string `description:"Connectionstring sf/udp@HOST:PORT" default:"[email protected]:9002"`
ClientConnectionString string `description:"Connectionstring sf@HOST:PORT or serial@PORT:BAUD" default:"serial@/dev/ttyUSB0:115200"`
Server string `description:"Connectionstring sf/udp@HOST:PORT" default:"[email protected]:9002"`
Source []string `description:"Connectionstring sf@HOST:PORT or serial@PORT:BAUD" default:"serial@/dev/ttyUSB0:115200"`
} `positional-args:"yes"`

Reconnect uint `long:"reconnect" default:"10" description:"Reconnect period, seconds"`
Expand Down Expand Up @@ -77,23 +77,30 @@ func main() {
}
os.Exit(0)
}

for i, sauce := range opts.Positional.Source {
fmt.Printf("Got argument source: %d %s\n", i, sauce)
}
// Applying both would effectively swap the ends, which makes no sense
if opts.ClientClient && opts.ServerServer {
fmt.Printf("ERROR: client-client, server-server or neither of them, NOT both!\n")
os.Exit(1)
}

sconn, scs, err := moteconnection.CreateConnection(opts.Positional.ServerConnectionString)
sconn, scs, err := moteconnection.CreateConnection(opts.Positional.Server)
if err != nil {
fmt.Printf("ERROR: %s\n", err)
os.Exit(1)
}

cconn, ccs, err := moteconnection.CreateConnection(opts.Positional.ClientConnectionString)
if err != nil {
fmt.Printf("ERROR: %s\n", err)
os.Exit(1)
ccons := make([]moteconnection.MoteConnection, len(opts.Positional.Source))
ccstrings := make([]string, len(opts.Positional.Source))
for i, sauce := range opts.Positional.Source {

var err2 error
ccons[i], ccstrings[i], err2 = moteconnection.CreateConnection(sauce)
if err2 != nil {
fmt.Printf("ERROR: %s\n", err)
os.Exit(1)
}
}

// Configure logging
Expand All @@ -106,23 +113,32 @@ func main() {
logger = log.New(os.Stdout, "INFO: ", logformat)
sconn.SetDebugLogger(log.New(os.Stdout, "DEBUG: ", logformat))
sconn.SetInfoLogger(logger)
cconn.SetDebugLogger(log.New(os.Stdout, "DEBUG: ", logformat))
cconn.SetInfoLogger(logger)
for i := range opts.Positional.Source {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it would make more sense to iterate over the cconn array directly.


var debugstring = fmt.Sprintf("DEBUG%d: ", i)
ccons[i].SetDebugLogger(log.New(os.Stdout, debugstring, logformat))
ccons[i].SetInfoLogger(logger)
}
} else {
logger = log.New(os.Stdout, "", logformat)
}
sconn.SetWarningLogger(log.New(os.Stdout, "WARN: ", logformat))
sconn.SetErrorLogger(log.New(os.Stdout, "ERROR: ", logformat))
cconn.SetWarningLogger(log.New(os.Stdout, "WARN: ", logformat))
cconn.SetErrorLogger(log.New(os.Stdout, "ERROR: ", logformat))

for i := range opts.Positional.Source {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it would make more sense to iterate over the cconn array directly.

var warningstring = fmt.Sprintf("WARN%d: ", i)
var errorstring = fmt.Sprintf("ERROR%d: ", i)
ccons[i].SetWarningLogger(log.New(os.Stdout, warningstring, logformat))
ccons[i].SetErrorLogger(log.New(os.Stdout, errorstring, logformat))
}
// Set up dispatchers for all possible dispatch IDs on both ends
var cdsps []moteconnection.Dispatcher
creceive := make(chan moteconnection.Packet)
for i := 0; i <= 255; i++ {
dsp := moteconnection.NewPacketDispatcher(moteconnection.NewRawPacket(byte(i)))
dsp.RegisterReceiver(creceive)
cconn.AddDispatcher(dsp)
for i := range opts.Positional.Source {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it would make more sense to iterate over the cconn array directly.

ccons[i].AddDispatcher(dsp)
}
cdsps = append(cdsps, dsp)
}

Expand All @@ -144,11 +160,15 @@ func main() {
sconn.Listen()
}
if opts.ServerServer {
logger.Printf("Listening on %s\n", ccs)
cconn.Listen()
for i := range opts.Positional.Source {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it would make more sense to iterate over the cconn array directly and skip the i's alltogether.

logger.Printf("Listening on %s\n", ccstrings[i])
ccons[i].Listen()
}
} else {
logger.Printf("Connecting to %s\n", ccs)
cconn.Autoconnect(time.Duration(opts.Reconnect) * time.Second)
for i := range opts.Positional.Source {
logger.Printf("Connecting to %s\n", ccstrings[i])
ccons[i].Autoconnect(time.Duration(opts.Reconnect) * time.Second)
}
}

// Set up signals to close nicely on Control+C
Expand All @@ -159,15 +179,19 @@ func main() {
select {
case p := <-sreceive:
logger.Printf("S d:%02X p:%s\n", p.Dispatch(), p)
cconn.Send(p)
for i := range opts.Positional.Source {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it would make more sense to iterate over the cconn array directly, no need for the i variable.

ccons[i].Send(p)
}
case p := <-creceive:
logger.Printf("C d:%02X p:%s\n", p.Dispatch(), p)
sconn.Send(p)
case sig := <-signals:
signal.Stop(signals)
logger.Printf("signal %s\n", sig)
sconn.Disconnect()
cconn.Disconnect()
for i := range opts.Positional.Source {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it would make more sense to iterate over the cconn array directly, no need for the i variable.

ccons[i].Disconnect()
}
interrupted = true
}
}
Expand Down