generated from OtusGolang/home_work
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from DimVlas/hw11_telnet_client
Hw11 telnet client
- Loading branch information
Showing
9 changed files
with
344 additions
and
24 deletions.
There are no files selected for viewing
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
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
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
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,92 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"log" | ||
"os" | ||
"os/signal" | ||
"path" | ||
"syscall" | ||
"time" | ||
) | ||
|
||
var ( | ||
ErrParseArgs = errors.New("parse args error") | ||
// соединение не установлено. | ||
ErrConnNotEstablish = errors.New("connection is not established") | ||
// завершение клиента. | ||
ErrClientExit = errors.New("connection was closed by client") | ||
// завершение сервера. | ||
ErrServerExit = errors.New("connection was closed by peer") | ||
) | ||
|
||
func main() { | ||
// Place your code here, | ||
// P.S. Do not rush to throw context down, think think if it is useful with blocking operation? | ||
var ( | ||
timeout time.Duration | ||
host string | ||
port int | ||
) | ||
|
||
if err := parseArgs(os.Args[1:], &timeout, &host, &port); err != nil { | ||
log.Println(err) | ||
log.Println("Usage: go-telnet [--timeout=<timeout>] <host> <port>") | ||
os.Exit(1) | ||
} | ||
|
||
addr := fmt.Sprintf("%s:%d", host, port) | ||
client := NewTelnetClient(addr, timeout, os.Stdin, os.Stdout) | ||
|
||
if err := client.Connect(); err != nil { | ||
log.Fatalln(err) | ||
} | ||
|
||
log.Printf("...Connected to %s", addr) | ||
|
||
c := make(chan os.Signal, 1) | ||
clientErr := make(chan error) | ||
|
||
defer func() { | ||
client.Close() | ||
close(c) | ||
close(clientErr) | ||
}() | ||
|
||
signal.Notify(c, syscall.SIGINT) | ||
|
||
go func() { | ||
err := client.Send() | ||
if err != nil { | ||
clientErr <- err | ||
return | ||
} | ||
|
||
clientErr <- ErrClientExit | ||
}() | ||
|
||
go func() { | ||
err := client.Receive() | ||
if err != nil { | ||
clientErr <- err | ||
return | ||
} | ||
clientErr <- ErrServerExit | ||
}() | ||
|
||
select { | ||
case <-c: | ||
log.Println("...Connection was closed by", path.Base(os.Args[0])) | ||
return | ||
case err := <-clientErr: | ||
if errors.Is(err, ErrClientExit) { | ||
log.Println("...EOF") | ||
return | ||
} | ||
if errors.Is(err, ErrServerExit) { | ||
log.Println("...Connection was closed by peer") | ||
return | ||
} | ||
log.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"regexp" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
func parseArgs(args []string, timeout *time.Duration, host *string, port *int) error { | ||
fs := flag.NewFlagSet("telnet", flag.ContinueOnError) | ||
fs.SetOutput(bytes.NewBuffer(nil)) | ||
|
||
fs.DurationVar(timeout, "timeout", 10*time.Second, "Connection timeout") | ||
if err := fs.Parse(args); err != nil { | ||
return fmt.Errorf("%w: %w", ErrParseArgs, err) | ||
} | ||
|
||
if fs.NArg() != 2 { | ||
return fmt.Errorf("%w: %w", ErrParseArgs, errors.New("not enough arguments")) | ||
} | ||
|
||
h := fs.Arg(0) | ||
b, _ := regexp.MatchString(`^[A-Za-z0-9-.]+$`, h) | ||
if !b { | ||
return fmt.Errorf("%w %w", ErrParseArgs, errors.New("host: string \""+h+"\" cannot be a host name or address")) | ||
} | ||
*host = h | ||
|
||
p, err := strconv.Atoi(fs.Arg(1)) | ||
if err != nil { | ||
return fmt.Errorf("%w %w", ErrParseArgs, fmt.Errorf("port: %w", err)) | ||
} | ||
*port = p | ||
|
||
return nil | ||
} |
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
Oops, something went wrong.