/
rpc.go
97 lines (85 loc) · 2.33 KB
/
rpc.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
package main
// This file implements the Remote Procedure Call between
// the clip daemon and client front-end
import (
"errors"
"fmt"
"net"
"net/http"
"net/rpc"
"reflect"
"unicode"
)
// Start serving RPC calls from client instances.
func (player *Player) serveRPC() {
rpc.Register(player.RPC())
rpc.HandleHTTP()
conn, err := net.Listen("tcp", port)
if err != nil {
Err("listen error:", err)
}
Debug("Listening on port " + port)
http.Serve(conn, nil)
//TODO: log errors.
}
// Aliased type to define RPC methods on.
type RPC struct {
player *Player
}
// RPC-exported function used for normal operation mode.
// The command-line arguments are passed (e.g. "play jazz")
// and a response to the user is returned in *resp.
// Here, run-time reflection is used to match the user command
// to a method on the API type.
func (rpc RPC) Call(args []string, resp *string) (err error) {
Debug("PlayerRPC.Call", args)
if len(args) == 0 {
args = []string{"help"}
}
cmd := args[0] // first arg is command (e.g.: "play")
args = args[1:] // rest are arguments (e.g.: "jazz")
*resp, err = rpc.call(cmd, args)
return
}
func (rpc RPC) call(cmd string, args []string) (resp string, err error) {
// convert first character to uppercase
first := unicode.ToUpper(rune(cmd[0]))
Cmd := string(first) + cmd[1:] // (e.g.: Play)
// resolve the command using reflection
player := rpc.player
p := reflect.ValueOf(player.API())
m := p.MethodByName(Cmd)
Debug("MethodByName", Cmd, ":", m)
if m.Kind() == reflect.Invalid {
err = errors.New("clip: '" + cmd + "' is not a clip command. See clip help.")
return
}
// set up method arguments
ins := m.Type().NumIn()
var callArgs []reflect.Value
switch ins {
default:
err = errors.New(fmt.Sprint("Bug: wrong number of ins: ", ins))
return
case 0:
if len(args) > 0 {
err = errors.New(fmt.Sprint(cmd, ": does not take arugments"))
return
}
callArgs = []reflect.Value{}
case 1:
if len(args) == 0 {
err = errors.New(fmt.Sprint(cmd, ": need an argument"))
return
}
callArgs = []reflect.Value{reflect.ValueOf(args)}
}
// call the method
r := m.Call(callArgs)
resp = r[0].Interface().(string) // by convention, response is 1st return value
errStr := r[1].Interface().(string) // by convention, error is 2nd return value
if errStr != "" {
err = errors.New(errStr)
}
return
}