@@ -5,20 +5,106 @@ package server
5
5
6
6
import (
7
7
"context"
8
+ "encoding/json"
8
9
9
- pb "github.com/lima-vm/lima/pkg/driver/external"
10
+ "google.golang.org/grpc/codes"
11
+ "google.golang.org/grpc/status"
10
12
"google.golang.org/protobuf/types/known/emptypb"
13
+
14
+ pb "github.com/lima-vm/lima/pkg/driver/external"
15
+ "github.com/lima-vm/lima/pkg/store"
11
16
)
12
17
13
- // TODO: Add more 3 functions Start, SetConfig & GuestAgentConn
18
+ func (s * DriverServer ) Start (empty * emptypb.Empty , stream pb.Driver_StartServer ) error {
19
+ s .logger .Debug ("Received Start request" )
20
+ errChan , err := s .driver .Start (stream .Context ())
21
+ if err != nil {
22
+ s .logger .Errorf ("Start failed: %v" , err )
23
+ return nil
24
+ }
25
+
26
+ for {
27
+ select {
28
+ case err , ok := <- errChan :
29
+ if ! ok {
30
+ s .logger .Debug ("Start error channel closed" )
31
+ if err := stream .Send (& pb.StartResponse {Success : true }); err != nil {
32
+ s .logger .Errorf ("Failed to send success response: %v" , err )
33
+ return status .Errorf (codes .Internal , "failed to send success response: %v" , err )
34
+ }
35
+ return nil
36
+ }
37
+ if err != nil {
38
+ s .logger .Errorf ("Error during Start: %v" , err )
39
+ if err := stream .Send (& pb.StartResponse {Error : err .Error (), Success : false }); err != nil {
40
+ s .logger .Errorf ("Failed to send error response: %v" , err )
41
+ return status .Errorf (codes .Internal , "failed to send error response: %v" , err )
42
+ }
43
+ }
44
+ case <- stream .Context ().Done ():
45
+ s .logger .Debug ("Stream context done, stopping Start" )
46
+ return nil
47
+ }
48
+ }
49
+ }
50
+
51
+ func (s * DriverServer ) SetConfig (ctx context.Context , req * pb.SetConfigRequest ) (* emptypb.Empty , error ) {
52
+ s .logger .Debugf ("Received SetConfig request" )
53
+ var inst * store.Instance
54
+
55
+ if err := json .Unmarshal (req .InstanceConfigJson , inst ); err != nil {
56
+ s .logger .Errorf ("Failed to unmarshal InstanceConfigJson: %v" , err )
57
+ return & emptypb.Empty {}, err
58
+ }
59
+
60
+ s .driver .SetConfig (inst , int (req .SshLocalPort ))
61
+
62
+ return & emptypb.Empty {}, nil
63
+ }
64
+
65
+ // TODO
66
+ // func (s *DriverServer) GuestAgentConn(empty *emptypb.Empty, stream pb.Driver_GuestAgentConnServer) error {
67
+ // s.logger.Debug("Received GuestAgentConn request")
68
+ // conn, err := s.driver.GuestAgentConn(stream.Context())
69
+ // if err != nil {
70
+ // s.logger.Errorf("GuestAgentConn failed: %v", err)
71
+ // return err
72
+ // }
73
+
74
+ // defer conn.Close()
75
+
76
+ // buf := make([]byte, 1024)
77
+
78
+ // for {
79
+ // n, err := conn.Read(buf)
80
+ // if err != nil {
81
+ // if err == io.EOF {
82
+ // return nil
83
+ // }
84
+ // return status.Errorf(codes.Internal, "error reading: %v", err)
85
+ // }
86
+
87
+ // msg := &pb.GuestAgentConnResponse{NetConn: buf[:n]}
88
+ // if err := stream.Send(msg); err != nil {
89
+ // return err
90
+ // }
91
+ // }
92
+ // s.logger.Debug("GuestAgentConn succeeded")
93
+ // return nil
94
+ // }
14
95
15
96
func (s * DriverServer ) GetInfo (ctx context.Context , empty * emptypb.Empty ) (* pb.InfoResponse , error ) {
16
97
s .logger .Debug ("Received GetInfo request" )
98
+ info := s .driver .GetInfo ()
99
+
100
+ infoJson , err := json .Marshal (info )
101
+ if err != nil {
102
+ s .logger .Errorf ("Failed to marshal driver info: %v" , err )
103
+ return nil , status .Errorf (codes .Internal , "failed to marshal driver info: %v" , err )
104
+ }
105
+
17
106
return & pb.InfoResponse {
18
- DriverName : s .driver .GetInfo ().DriverName ,
19
- CanRunGui : s .driver .GetInfo ().CanRunGUI ,
20
- VsockPort : int64 (s .driver .GetInfo ().VsockPort ),
21
- VirtioPort : s .driver .GetInfo ().VirtioPort ,
107
+ InfoJson : infoJson ,
22
108
}, nil
23
109
}
24
110
0 commit comments