]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - frontend/conn.go
Use SPDX license identifiers
[safcm/safcm.git] / frontend / conn.go
1 // Frontend: Connection functions for programs using the safcm library
2
3 // Copyright (C) 2021  Simon Ruderich
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 package frontend
19
20 import (
21         "fmt"
22
23         "ruderich.org/simon/safcm"
24         "ruderich.org/simon/safcm/rpc"
25 )
26
27 // SendRecv sends a message for host over conn and waits for the response. Any
28 // MsgLog messages received before the final (non MsgLog) response are passed
29 // to l.Log.
30 func (l *Loop) SendRecv(host Host, conn *rpc.Conn, msg safcm.Msg) (
31         safcm.Msg, error) {
32
33         err := conn.Send(msg)
34         if err != nil {
35                 return nil, err
36         }
37         for {
38                 x, err := conn.Recv()
39                 if err != nil {
40                         return nil, err
41                 }
42                 log, ok := x.(safcm.MsgLog)
43                 if ok {
44                         l.Log(host, log.Level, false, log.Text)
45                         continue
46                 }
47                 return x, nil
48         }
49 }
50
51 // HostInfoMsg sends a MsgInfoReq for host via conn and returns the resulting
52 // MsgInfoResp.
53 func (l *Loop) HostInfoMsg(host Host, conn *rpc.Conn, req safcm.MsgInfoReq) (
54         safcm.MsgInfoResp, error) {
55
56         var empty safcm.MsgInfoResp
57         x, err := l.SendRecv(host, conn, req)
58         if err != nil {
59                 return empty, err
60         }
61         resp, ok := x.(safcm.MsgInfoResp)
62         if !ok {
63                 return empty, fmt.Errorf("unexpected response %v", x)
64         }
65         return resp, nil
66 }
67
68 // HostSyncMsg sends a MsgSyncReq for host via conn and returns the resulting
69 // MsgSyncResp.
70 func (l *Loop) HostSyncMsg(host Host, conn *rpc.Conn, req safcm.MsgSyncReq) (
71         safcm.MsgSyncResp, error) {
72
73         var empty safcm.MsgSyncResp
74         x, err := l.SendRecv(host, conn, req)
75         if err != nil {
76                 return empty, err
77         }
78         resp, ok := x.(safcm.MsgSyncResp)
79         if !ok {
80                 return empty, fmt.Errorf("unexpected response %v", x)
81         }
82         return resp, nil
83 }