-
Notifications
You must be signed in to change notification settings - Fork 2
[TS-716] Reworked serialforwarder to support multiple clients with si… #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: old-udp-wip
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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"` | ||
|
|
@@ -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 | ||
|
|
@@ -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 { | ||
|
|
||
| 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 { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
| } | ||
|
|
||
|
|
@@ -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 { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
@@ -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 { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
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.