Skip to content

Commit

Permalink
lesson 11
Browse files Browse the repository at this point in the history
  • Loading branch information
bodrovis committed Aug 4, 2024
1 parent 316ffe5 commit 2543c0d
Show file tree
Hide file tree
Showing 14 changed files with 672 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lesson_11/cancellation/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module cancellation

go 1.22.2
46 changes: 46 additions & 0 deletions lesson_11/cancellation/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"fmt"
"os"
"time"
)

func main() {
abort := make(chan struct{})

go func() {
var b = make([]byte, 1)

for {
os.Stdin.Read(b)

if b[0] == 'Q' || b[0] == 'q' {
abort <- struct{}{}
return
}
}
}()

fmt.Println("Commencing countdown. Press 'Q' to abort.")

tick := time.Tick(1 * time.Second)

for countdown := 10; countdown > 0; countdown-- {
fmt.Println(countdown)

select {
case <-tick:
fmt.Println("...")
case <-abort:
fmt.Println("Launch aborted!")
return
}
}

launch()
}

func launch() {
fmt.Println("Lift off!")
}
3 changes: 3 additions & 0 deletions lesson_11/cancellation_complex/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module cancellation_complex

go 1.22.2
71 changes: 71 additions & 0 deletions lesson_11/cancellation_complex/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main

import (
"fmt"
"os"
"sync"
"time"
)

var done = make(chan struct{})
var wg sync.WaitGroup

func main() {
go func() {
os.Stdin.Read(make([]byte, 1)) // read a single byte
close(done)
}()

fmt.Println("Press enter to cancel the tasks.")

completion := make(chan int)

for i := 0; i < 5; i++ {
if cancelled() {
fmt.Println("Main loop cancelled!")
break
}

wg.Add(1)
go longRunningTask(i, completion)
}

go func() {
wg.Wait()
close(completion)
}()

for result := range completion {
fmt.Printf("Task %d completed\n", result)
}

fmt.Println("Main function completed.")
}

func cancelled() bool {
select {
case <-done:
return true
default:
return false
}
}

func longRunningTask(id int, completion chan<- int) {
defer wg.Done()

for i := 0; i < 3; i++ {
select {
case <-done:
fmt.Printf("Task %d cancelled during execution!\n", id)
return
default:
// Simulate some work with a sleep
fmt.Printf("Task %d working... %d\n", id, i)
time.Sleep(1 * time.Second)
}
}

completion <- id
fmt.Printf("Task %d completed!\n", id)
}
3 changes: 3 additions & 0 deletions lesson_11/chat_client/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module chat_client

go 1.22.2
81 changes: 81 additions & 0 deletions lesson_11/chat_client/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package main

import (
"bufio"
"crypto/tls"
"flag"
"fmt"
"log"
"os"
)

func main() {
// Define command-line arguments
serverAddr := flag.String("server", "localhost:8000", "server address in the format ip:port")
flag.Parse()

conn, err := makeConn(*serverAddr)
if err != nil {
log.Fatal(err)
}
defer conn.Close()

fmt.Printf("Connected to %s\n", *serverAddr)

// Channel to signal when the server connection is closed
done := make(chan struct{})

// Start a goroutine to read messages from the server and print them
go readMessages(conn, done)

// Read input from the user and send it to the server
go sendMessage(conn, done)

// Wait for the done signal indicating the server connection is closed
<-done
fmt.Println("Disconnected from server.")
}

// makeConn establishes a TLS connection to the server.
func makeConn(serverAddr string) (*tls.Conn, error) {
conf := &tls.Config{
InsecureSkipVerify: true, // Insecure, should be used with caution
}

return tls.Dial("tcp", serverAddr, conf)
}

func readMessages(conn *tls.Conn, done chan<- struct{}) {
scanner := bufio.NewScanner(conn)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Println("Error reading from server:", err)
}
close(done)
}

func sendMessage(conn *tls.Conn, done chan<- struct{}) {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
text := scanner.Text()

if text == "/exit" {
fmt.Println("Exiting chat...")
conn.Close()
close(done)
return
}

_, err := fmt.Fprintln(conn, text)
if err != nil {
log.Println("Error sending to server:", err)
break
}
}

if err := scanner.Err(); err != nil {
log.Println("Error reading from stdin:", err)
}
}
31 changes: 31 additions & 0 deletions lesson_11/chat_server/cert.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
-----BEGIN CERTIFICATE-----
MIIFazCCA1OgAwIBAgIUZtZwShEazDgSpdACfRmoSSMTOHowDQYJKoZIhvcNAQEL
BQAwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM
GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0yNDA4MDMxNjU0NDNaFw0yNTA4
MDMxNjU0NDNaMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEw
HwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwggIiMA0GCSqGSIb3DQEB
AQUAA4ICDwAwggIKAoICAQCYnSKui3dmcUuDjoWZwJehxOFqLmI2U022lNP5T37O
OiLEl02oGqnB//wt0OMnCLNvUAz0piCd5gGLqasHE6wSzn86aHJvB3gg9Qv2VZFS
QF9/RzqGF/bc1hN37F6p6bjMaKxHBIgh69KPznUrByDq5jni6UZauZwVJ7s8mdKm
gKD2jPlp/tIxviZVGGIb/bYSRUpkiecBwogEZeBcCq1pP6auAj3IF+fPCiwVfl94
LxEuyk/i8PbCf5clS1ZhQZeDdtnmMhh+2FdFcpC+T/LnRUVX5czvFvC+Rd4JVVbx
qnvKPdkTOdMHx42fJNydfx6eBoOW3OifHVzsvh5BtwH7D9b5Pv1wYfRzrjonY2iZ
6rloHfBObc7TrL0wbBLLl1JsTQjL0GPlXa+Fr2gXuojnDsxIdYoTgRY4w3SlcXKm
JC3TpLEpuhfUf48HG8OkEwYXWt/1gFuU9uLxlv1dap/eL9GVxNiAYyCmhjS2bWBq
SenphYYTIoERdY73Fk1IyoifNfxIveWhBZF+xVMGJ4zm9LOG/mIya36nhFHUW9tb
2338lUot+gpYw5cCDHUGr4XlbRiyKSLvETAP6aMaBtCqTurynUOAOdM3iloP4ex7
tUbckLRPzPSMwbbXuY6Vcl3DlB5iPIcUWDLIOFeyHXJnWVDHjJnombNoYzp2n0Qc
UwIDAQABo1MwUTAdBgNVHQ4EFgQUbLGaGgRGiEWQ8UzlB114Zyo3vx8wHwYDVR0j
BBgwFoAUbLGaGgRGiEWQ8UzlB114Zyo3vx8wDwYDVR0TAQH/BAUwAwEB/zANBgkq
hkiG9w0BAQsFAAOCAgEAGoc8a6x232G3UEmKTocfpX+B7/wYwiFTSO0JpDcYm/Fw
fMJbuUGxiUprwB7Ab0Stp+FH3u+/MQxf69fALt26YpzHuX/QUx2RJoe43tmzh95x
3bUQai8C3J2SsyqVnZ+IlssnBah2EQmuScesNtLej9QXZHp4pY8IV0OBE+tyW4mf
UTYkqg9iGs0MEsKgIgrcC0MjhI8Jc0OtlSw75W3r77W79GVDnMe5hFEYgvXcDMJo
/VpsdxQSBuupUD8sOHbP8ERu6+V8pU/UV+fqNiE7/QkyMubqz71zNt46ezMYZ6VS
/1JAGfkgwd3JQ03D+7DsLt6IWMuF3p5X/l2V8v3r7OIjDl/MKq7NO/bUdH+JN6RU
+cYAJmoZd83BJaG/I1X0NAVLCi1PwKTszkN6IXwb4NrDMSiknidsScIR/EA42Hy+
V/45lPZKJ5XlPqpXWf/Wz3bpPH+CCzldXhH3+1SV59596uJjMW895aIS0ZMaZ3W2
c+KYtzeqLz3tDvv9Gdq5kl0F1zqPBwzi9d/PiBLXhEU8AwbLv9VA6kD6IoPaxwQv
TwyMunOEpBC3iR45a2H5Jbs1PYRC2ZVg0fEy8d9qzR341ABD6njouwLmziWYoOA0
o0AGppO+GBDegXiJPmWUU8TU4qdKgpsaXZIdgR60C/28T4NqwBGO6FnM8rtMrzU=
-----END CERTIFICATE-----
10 changes: 10 additions & 0 deletions lesson_11/chat_server/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module chat_server

go 1.22.2

require (
github.com/fatih/color v1.17.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
golang.org/x/sys v0.18.0 // indirect
)
11 changes: 11 additions & 0 deletions lesson_11/chat_server/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
52 changes: 52 additions & 0 deletions lesson_11/chat_server/key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
-----BEGIN PRIVATE KEY-----
MIIJQgIBADANBgkqhkiG9w0BAQEFAASCCSwwggkoAgEAAoICAQCYnSKui3dmcUuD
joWZwJehxOFqLmI2U022lNP5T37OOiLEl02oGqnB//wt0OMnCLNvUAz0piC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1Pt6BXMJC
RqG9T1DuQUHebZinrzalkTUOJ63zcOVaLZekiemmgQieMELMyghBsa72wCEPZmgD
KmbL4HnPHTHhnBUDKuv9MNA+NVCf1k52UvYPhRKIbHBh7p/lUcsskLO0Tn3lykOh
jrYN2mMlv06SyqYAI/Vro9sQ4Wa2geF3OxNxUo6J/f0aAFvU/k9pPALs+U2uJYSM
+QsKZcq681+XYaCB2xgdnpJLPtnoDG4/CX5/GG2RAoIBAFsFS/kluSHSTG1P0TVX
103hiq33QcfJbba1EARB4y2i8w1fBqDFrvkTdqCfBiXUqbakPNU1BxrkXBqxwVt1
30iA0jjUlA0WJiYRDEFPwmbkdAz7ORyrUHHFECFv3mBDjYKB+571UgFq5Rn6Cm/t
MRDDAqmInWoVIKHyZbnWEChS51aDkYXbFHApx0MBmu9jN/6BnrlzYQzye9PHeWRg
xdPL+bIwJQDXzcgcNVaU9JuwcMLB8VJla4m/eJXMIQSzUS9beDGtAFUAYJXaTi6j
CU3nYdaZxgwkO7a2CzWFsGrRxgRGFhtYORHFthCYLtgj52mk5MO8618z8++pbntQ
S6kCggEAZuwVyCU+J5mx37o/F1dlmpbQunZWcwbbr9qjD/aPM320huVLSzPQK8xp
9TmCgLfQeRodsDy8Aaqx/Nmh6aKy6fyJ/+WazvzE/Bidt42f5XA4AAU/f8TdjysQ
eI97MpCXQlq1Ox6JVoQW05aj+NUnsVpVDFYKfgf1NvJBhtXDRrTQNpFrOA3HNugy
+kscsMruV6as5U1hsvyzo6cQwwg2sAC93esHbMaI6YHJ0u5siOcPaRVc2PVdGdPV
SZzVKcZJhdxCEpD+peQ5QCzorKa/SuPNsQELvhXK0DSZXOOAhAgtgmVir74YlybB
xi04d/VCFz6MHTeFI9WTJ/jtyjN7VA==
-----END PRIVATE KEY-----
Loading

0 comments on commit 2543c0d

Please sign in to comment.