-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
209 lines (165 loc) · 4.29 KB
/
main.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"bufio"
"context"
"encoding/json"
"errors"
"flag"
"io"
"log"
"net"
"os"
"sync/atomic"
"github.com/pojntfx/panrpc/go/pkg/rpc"
)
type local struct{}
type remote struct {
Iterate func(
ctx context.Context,
length int,
onIteration func(ctx context.Context, i int, b string) (string, error),
) (int, error)
}
func Iterate(callee remote, ctx context.Context, length int, onIteration func(ctx context.Context, i int, b string) (string, error)) (int, error) {
return callee.Iterate(ctx, length, onIteration)
}
func main() {
addr := flag.String("addr", "localhost:1337", "Listen or remote address")
listen := flag.Bool("listen", false, "Whether to allow connecting to remotes by listening or dialing")
flag.Parse()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var clients atomic.Int64
registry := rpc.NewRegistry[remote, json.RawMessage](
&local{},
&rpc.RegistryHooks{
OnClientConnect: func(remoteID string) {
log.Printf("%v clients connected", clients.Add(1))
},
OnClientDisconnect: func(remoteID string) {
log.Printf("%v clients connected", clients.Add(-1))
},
},
)
go func() {
log.Println(`Enter one of the following letters followed by <ENTER> to run a function on the remote(s):
- a: Iterate over 5
- b: Iterate over 10`)
stdin := bufio.NewReader(os.Stdin)
for {
line, err := stdin.ReadString('\n')
if err != nil {
panic(err)
}
if err := registry.ForRemotes(func(remoteID string, remote remote) error {
log.Println("Calling functions for remote with ID", remoteID)
switch line {
case "a\n":
length, err := Iterate(remote, ctx, 5, func(ctx context.Context, i int, b string) (string, error) {
log.Println("In iteration", i, b)
return "This is from the caller", nil
})
if err != nil {
log.Println("Got error for Iterate func:", err)
return nil
}
log.Println(length)
case "b\n":
length, err := Iterate(remote, ctx, 10, func(ctx context.Context, i int, b string) (string, error) {
log.Println("In iteration", i, b)
return "This is from the caller", nil
})
if err != nil {
log.Println("Got error for Iterate func:", err)
return nil
}
log.Println(length)
default:
log.Printf("Unknown letter %v, ignoring input", line)
return nil
}
return nil
}); err != nil {
panic(err)
}
}
}()
if *listen {
lis, err := net.Listen("tcp", *addr)
if err != nil {
panic(err)
}
defer lis.Close()
log.Println("Listening on", lis.Addr())
for {
conn, err := lis.Accept()
if err != nil {
if errors.Is(err, net.ErrClosed) {
break
}
log.Println("could not accept connection, continuing:", err)
continue
}
go func() {
defer conn.Close()
linkCtx, cancelLinkCtx := context.WithCancel(ctx)
defer cancelLinkCtx()
encoder := json.NewEncoder(conn)
decoder := json.NewDecoder(conn)
if err := registry.LinkStream(
linkCtx,
func(v rpc.Message[json.RawMessage]) error {
return encoder.Encode(v)
},
func(v *rpc.Message[json.RawMessage]) error {
return decoder.Decode(v)
},
func(v any) (json.RawMessage, error) {
b, err := json.Marshal(v)
if err != nil {
return nil, err
}
return json.RawMessage(b), nil
},
func(data json.RawMessage, v any) error {
return json.Unmarshal([]byte(data), v)
},
nil,
); err != nil && !errors.Is(err, io.EOF) {
log.Println("Client disconnected with error:", err)
}
}()
}
} else {
conn, err := net.Dial("tcp", *addr)
if err != nil {
panic(err)
}
defer conn.Close()
log.Println("Connected to", conn.RemoteAddr())
encoder := json.NewEncoder(conn)
decoder := json.NewDecoder(conn)
if err := registry.LinkStream(
ctx,
func(v rpc.Message[json.RawMessage]) error {
return encoder.Encode(v)
},
func(v *rpc.Message[json.RawMessage]) error {
return decoder.Decode(v)
},
func(v any) (json.RawMessage, error) {
b, err := json.Marshal(v)
if err != nil {
return nil, err
}
return json.RawMessage(b), nil
},
func(data json.RawMessage, v any) error {
return json.Unmarshal([]byte(data), v)
},
nil,
); err != nil {
panic(err)
}
}
}