Code

34a0434a1eeda23886d00ad34565b97c1226c22f
[sysdb/go.git] / client / client.go
1 //
2 // Copyright (C) 2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions
7 // are met:
8 // 1. Redistributions of source code must retain the above copyright
9 //    notice, this list of conditions and the following disclaimer.
10 // 2. Redistributions in binary form must reproduce the above copyright
11 //    notice, this list of conditions and the following disclaimer in the
12 //    documentation and/or other materials provided with the distribution.
13 //
14 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 // ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
18 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 /*
27 Package client provides a SysDB client implementation.
29 The Connect function connects to a SysDB server as the specified user:
31         c, err := client.Connect("unix:/var/run/sysdbd.sock", "username")
32         if err != nil {
33                 // handle error
34         }
35         defer c.Close()
37 The github.com/sysdb/go/proto package provides support for handling requests
38 and responses. Use the Send and Receive functions to communicate with the
39 server:
41         m := &proto.Message{
42                 Type: proto.ConnectionQuery,
43                 Raw:  []byte{"LOOKUP hosts MATCHING attribute.architecture = 'amd64';"},
44         }
45         if err := c.Send(m); err != nil {
46                 // handle error
47         }
48         m, err := c.Receive()
49         if err != nil {
50                 // handle error
51         }
52         if m.Type == proto.ConnectionError {
53                 // handle failed query
54         }
55         // ...
56 */
57 package client
59 import (
60         "fmt"
61         "net"
62         "strings"
64         "github.com/sysdb/go/proto"
65 )
67 // A Conn is a connection to a SysDB server instance.
68 //
69 // Multiple goroutines may invoke methods on a Conn simultaneously but since
70 // the SysDB protocol requires a strict ordering of request and response
71 // messages, the communication with the server will usually happen
72 // sequentially.
73 type Conn struct {
74         c net.Conn
75 }
77 // Connect sets up a client connection to a SysDB server instance at the
78 // specified address using the specified user.
79 //
80 // The address may be a UNIX domain socket, either prefixed with 'unix:' or
81 // specifying an absolute file-system path.
82 func Connect(addr, user string) (*Conn, error) {
83         network := "tcp"
84         if strings.HasPrefix(addr, "unix:") {
85                 network = "unix"
86                 addr = addr[len("unix:"):]
87         } else if addr[0] == '/' {
88                 network = "unix"
89         }
91         c := &Conn{}
92         var err error
93         if c.c, err = net.Dial(network, addr); err != nil {
94                 return nil, err
95         }
97         m := &proto.Message{
98                 Type: proto.ConnectionStartup,
99                 Raw:  []byte(user),
100         }
101         if err := c.Send(m); err != nil {
102                 return nil, err
103         }
105         m, err = c.Receive()
106         if err != nil {
107                 return nil, err
108         }
109         if m.Type == proto.ConnectionError {
110                 return nil, fmt.Errorf("failed to startup session: %s", string(m.Raw))
111         }
112         if m.Type != proto.ConnectionOK {
113                 return nil, fmt.Errorf("failed to startup session: unsupported")
114         }
115         return c, nil
118 // Close closes the client connection.
119 //
120 // Any blocked Send or Receive operations will be unblocked and return errors.
121 func (c Conn) Close() { c.c.Close() }
123 // Send sends the specified raw message to the server.
124 //
125 // Send operations block until the full message could be written to the
126 // underlying sockets. This ensures that server and client don't get out of
127 // sync.
128 func (c Conn) Send(m *proto.Message) error {
129         return proto.Write(c.c, m)
132 // Receive waits for a reply from the server and returns the raw message.
133 //
134 // Receive operations block until a full message could be read from the
135 // underlying socket. This ensures that server and client don't get out of
136 // sync.
137 func (c Conn) Receive() (*proto.Message, error) {
138         return proto.Read(c.c)
141 // vim: set tw=78 sw=4 sw=4 noexpandtab :