@@ -16,6 +16,7 @@ package mock
16
16
17
17
import (
18
18
"encoding/json"
19
+ "fmt"
19
20
"net"
20
21
"os"
21
22
"sync"
@@ -153,6 +154,72 @@ func stdinShimHandler(data []byte, userData interface{}, response *handlerRespon
153
154
proxy .StdinReceived <- true
154
155
}
155
156
157
+ func registerVMHandler (data []byte , userData interface {}, response * handlerResponse ) {
158
+ client := userData .(* client )
159
+ proxy := client .proxy
160
+
161
+ proxy .log ("Register VM" )
162
+
163
+ payload := api.RegisterVM {}
164
+ if err := json .Unmarshal (data , & payload ); err != nil {
165
+ response .SetError (err )
166
+ return
167
+ }
168
+
169
+ // Generate fake tokens
170
+ var tokens []string
171
+ for i := 0 ; i < payload .NumIOStreams ; i ++ {
172
+ tokens = append (tokens , fmt .Sprintf ("%d" , i ))
173
+ }
174
+
175
+ io := & api.IOResponse {
176
+ Tokens : tokens ,
177
+ }
178
+
179
+ response .AddResult ("io" , io )
180
+ }
181
+
182
+ func unregisterVMHandler (data []byte , userData interface {}, response * handlerResponse ) {
183
+ client := userData .(* client )
184
+ proxy := client .proxy
185
+
186
+ proxy .log ("Unregister VM" )
187
+ }
188
+
189
+ func attachVMHandler (data []byte , userData interface {}, response * handlerResponse ) {
190
+ client := userData .(* client )
191
+ proxy := client .proxy
192
+
193
+ proxy .log ("Attach VM" )
194
+
195
+ payload := api.AttachVM {}
196
+ if err := json .Unmarshal (data , & payload ); err != nil {
197
+ response .SetError (err )
198
+ return
199
+ }
200
+
201
+ // Generate fake tokens
202
+ var tokens []string
203
+ for i := 0 ; i < payload .NumIOStreams ; i ++ {
204
+ tokens = append (tokens , fmt .Sprintf ("%d" , i ))
205
+ }
206
+
207
+ io := & api.IOResponse {
208
+ Tokens : tokens ,
209
+ }
210
+
211
+ response .AddResult ("io" , io )
212
+ }
213
+
214
+ func hyperCmdHandler (data []byte , userData interface {}, response * handlerResponse ) {
215
+ client := userData .(* client )
216
+ proxy := client .proxy
217
+
218
+ proxy .log ("Hyper command" )
219
+
220
+ response .SetData ([]byte {})
221
+ }
222
+
156
223
// SendStdoutStream sends a Stdout Stream Frame to connected client
157
224
func (proxy * Proxy ) SendStdoutStream (payload []byte ) {
158
225
err := api .WriteStream (proxy .cl , api .StreamStdout , payload )
@@ -198,19 +265,34 @@ func (proxy *Proxy) serveClient(proto *protocol, newConn net.Conn) {
198
265
func (proxy * Proxy ) serve () {
199
266
proto := newProtocol ()
200
267
268
+ // shim handlers
201
269
proto .Handle (FrameKey {api .TypeCommand , int (api .CmdConnectShim )}, connectShimHandler )
202
270
proto .Handle (FrameKey {api .TypeCommand , int (api .CmdDisconnectShim )}, disconnectShimHandler )
203
- proto .Handle (FrameKey {api .TypeCommand , int (api .CmdSignal )}, signalShimHandler )
204
- proto .Handle (FrameKey {api .TypeCommand , int (api .CmdConnectShim )}, connectShimHandler )
205
-
206
271
proto .Handle (FrameKey {api .TypeStream , int (api .StreamStdin )}, stdinShimHandler )
207
272
273
+ // runtime handlers
274
+ proto .Handle (FrameKey {api .TypeCommand , int (api .CmdRegisterVM )}, registerVMHandler )
275
+ proto .Handle (FrameKey {api .TypeCommand , int (api .CmdUnregisterVM )}, unregisterVMHandler )
276
+ proto .Handle (FrameKey {api .TypeCommand , int (api .CmdAttachVM )}, attachVMHandler )
277
+ proto .Handle (FrameKey {api .TypeCommand , int (api .CmdHyper )}, hyperCmdHandler )
278
+
279
+ // Shared handler between shim and runtime
280
+ proto .Handle (FrameKey {api .TypeCommand , int (api .CmdSignal )}, signalShimHandler )
281
+
208
282
//Wait for a single client connection
209
283
conn , err := proxy .listener .Accept ()
210
- assert .Nil (proxy .t , err )
284
+ if err != nil {
285
+ // Ending up into this case when the listener is closed, which
286
+ // is still a valid case. We don't want to throw an error in
287
+ // this case.
288
+ return
289
+ }
290
+
211
291
assert .NotNil (proxy .t , conn )
212
292
proxy .log ("Client connected" )
213
293
294
+ proxy .wg .Add (1 )
295
+
214
296
proxy .cl = conn
215
297
216
298
proxy .serveClient (proto , conn )
@@ -219,8 +301,11 @@ func (proxy *Proxy) serve() {
219
301
// Start invokes mock proxy instance to start listening.
220
302
func (proxy * Proxy ) Start () {
221
303
proxy .startListening ()
222
- proxy .wg .Add (1 )
223
- go proxy .serve ()
304
+ go func () {
305
+ for {
306
+ proxy .serve ()
307
+ }
308
+ }()
224
309
}
225
310
226
311
// Stop causes mock proxy instance to stop listening,
0 commit comments