-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsession.go
More file actions
136 lines (106 loc) · 3.08 KB
/
session.go
File metadata and controls
136 lines (106 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2025 Blindspot Software
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package dutagent
import (
"fmt"
"io"
"log"
"github.com/BlindspotSoftware/dutctl/internal/chanio"
)
// session implements the module.Session interface.
type session struct {
done <-chan struct{} // closed when broker workers shut down; unblocks pending session calls
printCh chan string
stdinCh chan []byte
stdoutCh chan []byte
stderrCh chan []byte
fileReqCh chan string
fileCh chan chan []byte // a single file is represented by a channel of bytes
// currentFile holds the name of the file currently being transferred.
// It is used for both, to indicate the file that was requested by the module
// and the file that is being sent back to the client.
currentFile string
}
func (s *session) Print(a ...any) {
select {
case s.printCh <- fmt.Sprint(a...):
case <-s.done:
}
}
func (s *session) Printf(format string, a ...any) {
select {
case s.printCh <- fmt.Sprintf(format, a...):
case <-s.done:
}
}
func (s *session) Println(a ...any) {
select {
case s.printCh <- fmt.Sprintln(a...):
case <-s.done:
}
}
//nolint:nonamedreturns
func (s *session) Console() (stdin io.Reader, stdout, stderr io.Writer) {
var (
stdinReader io.Reader
stdoutWriter, stderrWriter io.Writer
err error
)
stdinReader, err = chanio.NewChanReader(s.stdinCh)
if err != nil {
log.Fatalf("session.Console() failed to create stdinReader: %v", err)
}
stdoutWriter, err = chanio.NewChanWriter(s.stdoutCh)
if err != nil {
log.Fatalf("session.Console() failed to create stdoutWriter: %v", err)
}
stderrWriter, err = chanio.NewChanWriter(s.stderrCh)
if err != nil {
log.Fatalf("session.Console() failed to create stderrWriter: %v", err)
}
return stdinReader, stdoutWriter, stderrWriter
}
func (s *session) RequestFile(name string) (io.Reader, error) {
if s.fileReqCh == nil {
log.Fatal("session.RequestFile() called but session.fileReq is nil")
}
log.Printf("Module issued file request for: %q", name)
select {
case s.fileReqCh <- name:
case <-s.done:
return nil, fmt.Errorf("session closed before file request %q could be sent", name)
}
var file chan []byte
select {
case file = <-s.fileCh:
case <-s.done:
return nil, fmt.Errorf("session closed while waiting for file %q", name)
}
r, err := chanio.NewChanReader(file)
if err != nil {
log.Fatalf("session.RequestFile() failed to create reader: %v", err)
}
return r, nil
}
func (s *session) SendFile(name string, r io.Reader) error {
if s.currentFile != "" {
log.Fatal("session.SendFile() called during a ongoing file request")
}
content, err := io.ReadAll(r)
if err != nil {
return err
}
log.Printf("Module issued file transfer of : %q, with %d bytes", name, len(content))
s.currentFile = name
file := make(chan []byte, 1)
select {
case s.fileCh <- file:
case <-s.done:
s.currentFile = ""
return fmt.Errorf("session closed before file %q could be sent", name)
}
file <- content
close(file) // indicate EOF.
return nil
}