-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventrw.go
77 lines (64 loc) · 2.5 KB
/
eventrw.go
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
// Actress Copyright (C) 2024 Bjørn Tore Svinningen
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package actress
import (
"fmt"
"io"
"log"
)
type EventRW struct {
P *Process
Ev *Event
Info string
Pos int
}
// NewEventRW will return a type that adds Read and Write methods
// to the Event type.
func NewEventRW(p *Process, ev *Event, info string) *EventRW {
m := EventRW{
P: p,
Ev: ev,
Info: info,
Pos: 0,
}
return &m
}
// Write the data into Event.Data, and put the event into the EventCh to be processed.
func (m *EventRW) Write(b []byte) (int, error) {
fmt.Printf(" *2 DEBUG WRITE: ev nr: %v, info: %v , eventType: %v, PREPARING for Writing to erw, len(b): %v, %v\n", m.Ev.Nr, m.Info, m.Ev.
EventType, len(b), string(b))
if len(b) > 0 {
ev := m.Ev
ev.Data = b
// Todo: Make this one generic to cath both standard, custom, and dynamic event?
m.P.AddDynEvent(*ev)
fmt.Printf(" *2 DEBUG WRITE: ev nr: %v, info: %v , eventType: %v, DONE Writing to erw, len(b): %v, %v\n", m.Ev.Nr, m.Info, m.Ev.
EventType, len(b), string(b))
}
log.Printf("%v, len: %v when writing, not adding an event\n", m.Info, len(b))
return len(b), nil
}
// Read the data into b.
func (m *EventRW) Read(b []byte) (int, error) {
if m.Pos >= len(m.Ev.Data) {
fmt.Printf(" *1 DEBUG READ: ev nr: %v, info: %v, eventType: %v, EOF reading from erw, len(m.ev.Data): %v, %v\n", m.Ev.Nr, m.Info, m.Ev.EventType, len(m.Ev.Data), string(m.Ev.Data))
return 0, io.EOF
}
fmt.Printf(" *1 DEBUG READ: ev nr: %v, info: %v, eventType: %v, PREPARING reading from erw, len(m.ev.Data): %v, %v\n", m.Ev.Nr, m.Info, m.Ev.EventType, len(m.Ev.Data), string(m.Ev.Data))
n := copy(b, m.Ev.Data[m.Pos:])
fmt.Printf(" *1 DEBUG READ: ev nr: %v, info: %v, eventType: %v, DONE reading from erw, len(m.ev.Data): %v, %v\n", m.Ev.Nr, m.Info, m.Ev.EventType, len(m.Ev.Data), string(m.Ev.Data))
m.Pos += n
return n, nil
}