generated from OtusGolang/home_work
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Pavel Pogodaev <[email protected]>
- Loading branch information
Pavel Pogodaev
committed
Nov 19, 2024
1 parent
411b524
commit 878312f
Showing
4 changed files
with
153 additions
and
6 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,96 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"context" | ||
"flag" | ||
"fmt" | ||
"io" | ||
"os" | ||
"os/signal" | ||
"sync" | ||
"syscall" | ||
"time" | ||
) | ||
|
||
func main() { | ||
// Place your code here, | ||
// P.S. Do not rush to throw context down, think think if it is useful with blocking operation? | ||
flag.Parse() | ||
port := os.Args[len(os.Args)-1] | ||
host := os.Args[len(os.Args)-2] | ||
duration := os.Args[len(os.Args)-3] | ||
|
||
durVal, err := time.ParseDuration(duration) | ||
if err != nil { | ||
panic("wrong duration value " + duration) | ||
} | ||
|
||
in := &bytes.Buffer{} | ||
|
||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT, os.Interrupt) | ||
defer stop() | ||
|
||
client := NewTelnetClient(host+":"+port, durVal, io.NopCloser(in), os.Stdout) | ||
defer client.Close() | ||
|
||
err = client.Connect() | ||
if err != nil { | ||
fmt.Printf("connection failed: %s\n", err) | ||
return | ||
} | ||
|
||
var wg sync.WaitGroup | ||
|
||
wg.Add(2) | ||
go sendByTcp(ctx, stop, &wg, in, client) | ||
go connectionHandler(ctx, &wg, client) | ||
|
||
wg.Wait() | ||
} | ||
|
||
func sendByTcp(ctx context.Context, stop context.CancelFunc, wg *sync.WaitGroup, in *bytes.Buffer, t TelnetClient) { | ||
go func() { | ||
<-ctx.Done() | ||
err := t.Close() | ||
if err != nil { | ||
fmt.Printf("failed to colse connection %s", err) | ||
} | ||
wg.Done() | ||
}() | ||
|
||
reader := bufio.NewReader(os.Stdin) | ||
for { | ||
resp, err := reader.ReadString('\n') | ||
if err != nil { | ||
stop() | ||
return | ||
} | ||
|
||
in.WriteString(resp) | ||
err = t.Send() | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
} | ||
} | ||
|
||
func connectionHandler(ctx context.Context, wg *sync.WaitGroup, t TelnetClient) { | ||
errCh := make(chan error) | ||
defer func() { | ||
wg.Done() | ||
}() | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case errCh <- t.Receive(): | ||
err := <-errCh | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters