]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - frontend/conn.go
safcm: move sync.sendRecv to frontend package
[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         "ruderich.org/simon/safcm"
22         "ruderich.org/simon/safcm/rpc"
23 )
24
25 // SendRecv sends a message for host over conn and waits for the response. Any
26 // MsgLog messages received before the final (non MsgLog) response are passed
27 // to l.Log.
28 func (l *Loop) SendRecv(host Host, conn *rpc.Conn, msg safcm.Msg) (
29         safcm.Msg, error) {
30
31         err := conn.Send(msg)
32         if err != nil {
33                 return nil, err
34         }
35         for {
36                 x, err := conn.Recv()
37                 if err != nil {
38                         return nil, err
39                 }
40                 log, ok := x.(safcm.MsgLog)
41                 if ok {
42                         l.Log(host, log.Level, false, log.Text)
43                         continue
44                 }
45                 return x, nil
46         }
47 }