-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclientmain.go
121 lines (101 loc) · 2.04 KB
/
clientmain.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright (c) 2010 William R. Conant, WillConant.com
// Use of this source code is governed by the MIT licence:
// http://www.opensource.org/licenses/mit-license.php
package main
import (
"msglite"
"flag"
"fmt"
"os"
"strconv"
)
var client *msglite.Client
func main() {
var network, laddr string
flag.StringVar(&network, "network", "unix", "unix or tcp")
flag.StringVar(&laddr, "address", "", "listen address (either socket path, or ip:port)")
flag.Parse()
if laddr == "" {
switch network {
case "unix":
laddr = "/tmp/msglite.socket"
case "tcp":
laddr = "127.0.0.1:9813"
}
}
var err os.Error
client, err = msglite.NewClient(network, laddr)
if err != nil {
panic(err)
}
switch flag.Arg(0) {
case "send":
doSend()
case "ready":
doReady()
case "query":
doQuery()
default:
fmt.Println("command must be one of send, ready, or query")
os.Exit(1)
}
}
func doSend() {
body := flag.Arg(1)
timeoutStr := flag.Arg(2)
toAddr := flag.Arg(3)
var replyAddr = ""
if flag.NArg() > 3 {
replyAddr = flag.Arg(4)
}
timeout, err := strconv.Atoi64(timeoutStr)
if err != nil {
panic(err)
}
err = client.Send(body, timeout, toAddr, replyAddr)
if err != nil {
panic(err)
}
}
func doReady() {
timeoutStr := flag.Arg(1)
timeout, err := strconv.Atoi64(timeoutStr)
if err != nil {
panic(err)
}
var addrs [8]string
addrCount := 0
for i := 2; i <= flag.NArg(); i++ {
addrs[i-2] = flag.Arg(i)
addrCount++
}
msg, err := client.Ready(timeout, addrs[0:addrCount])
if err != nil {
panic(err)
}
printMsg(msg)
}
func doQuery() {
body := flag.Arg(1)
timeoutStr := flag.Arg(2)
toAddr := flag.Arg(3)
timeout, err := strconv.Atoi64(timeoutStr)
if err != nil {
panic(err)
}
msg, err := client.Query(body, timeout, toAddr)
if err != nil {
panic(err)
}
printMsg(msg)
}
func printMsg(msg *msglite.Message) {
if msg == nil {
fmt.Println("*")
return
}
fmt.Printf("> %v %v %v %v\n", len(msg.Body), msg.TimeoutSeconds, msg.ToAddress, msg.ReplyAddress)
if msg.Body != "" {
fmt.Println(msg.Body)
}
}