-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
76 lines (66 loc) · 1.61 KB
/
client.go
File metadata and controls
76 lines (66 loc) · 1.61 KB
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
package main
// This file implements the client main function.
// Invoked whenever the user types executes "clip".
// The client merely forwards the CLI arguments
// to the clip daemon and returns the response to
// the user.
import (
"fmt"
"net/rpc"
"os"
"os/exec"
"strings"
"time"
)
// RPC port
const port = ":2527"
// Main loop for "client" mode (the normal mode).
// Simply passes the arguments to the daemon and
// displays the result.
func MainClient(args []string) {
client := dialDaemon()
var resp string
err := client.Call("RPC.Call", args, &resp)
if err != nil {
fmt.Fprint(os.Stderr, cleanup(err.Error()))
}
fmt.Print(cleanup(resp))
}
// cleanup newlines so string can be printed to stdout without redundant/missing newlines
func cleanup(str string) string {
str = strings.Trim(str, "\n")
if str != "" {
return str + "\n"
}
return str
}
// Connect to the clip daemon for RPC communication.
// Starts the daemon if he's not yet running.
func dialDaemon() *rpc.Client {
// try to call the daemon
client, err := rpc.DialHTTP("tcp", "localhost"+port)
// if daemon does not seem to be running, start him.
const SLEEP = 10e6 // nanoseconds
if err != nil {
forkDaemon()
time.Sleep(SLEEP)
}
// try again to call the daemon,
// give him some time to come up.
trials := 0
for err != nil && trials < 10 {
client, err = rpc.DialHTTP("tcp", "localhost"+port)
time.Sleep(SLEEP)
trials++
}
Check(err)
return client
}
// Start the clip daemon.
func forkDaemon() {
executable, err := os.Readlink("/proc/self/exe")
Check(err)
cmd := exec.Command(executable, "-d")
err = cmd.Start()
Check(err)
}