aboutsummaryrefslogtreecommitdiffstats
path: root/rpc/comms/comms.go
blob: bfe625758271080a2c66c19ad63f1035a761538b (plain) (blame)
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
package comms

import (
    "io"
    "net"

    "fmt"
    "strings"

    "strconv"

    "github.com/ethereum/go-ethereum/logger"
    "github.com/ethereum/go-ethereum/logger/glog"
    "github.com/ethereum/go-ethereum/rpc/codec"
    "github.com/ethereum/go-ethereum/rpc/shared"
)

const (
    maxHttpSizeReqLength = 1024 * 1024 // 1MB
)

var (
    // List with all API's which are offered over the in proc interface by default
    DefaultInProcApis = shared.AllApis

    // List with all API's which are offered over the IPC interface by default
    DefaultIpcApis = shared.AllApis

    // List with API's which are offered over thr HTTP/RPC interface by default
    DefaultHttpRpcApis = strings.Join([]string{
        shared.DbApiName, shared.EthApiName, shared.NetApiName, shared.Web3ApiName,
    }, ",")
)

type EthereumClient interface {
    // Close underlaying connection
    Close()
    // Send request
    Send(interface{}) error
    // Receive response
    Recv() (interface{}, error)
    // List with modules this client supports
    SupportedModules() (map[string]string, error)
}

func handle(conn net.Conn, api shared.EthereumApi, c codec.Codec) {
    codec := c.New(conn)

    for {
        req, err := codec.ReadRequest()
        if err == io.EOF {
            codec.Close()
            return
        } else if err != nil {
            glog.V(logger.Error).Infof("comms recv err - %v\n", err)
            codec.Close()
            return
        }

        var rpcResponse interface{}
        res, err := api.Execute(req)

        rpcResponse = shared.NewRpcResponse(req.Id, req.Jsonrpc, res, err)
        err = codec.WriteResponse(rpcResponse)
        if err != nil {
            glog.V(logger.Error).Infof("comms send err - %v\n", err)
            codec.Close()
            return
        }
    }
}

// Endpoint must be in the form of:
// ${protocol}:${path}
// e.g. ipc:/tmp/geth.ipc
//      rpc:localhost:8545
func ClientFromEndpoint(endpoint string, c codec.Codec) (EthereumClient, error) {
    if strings.HasPrefix(endpoint, "ipc:") {
        cfg := IpcConfig{
            Endpoint: endpoint[4:],
        }
        return NewIpcClient(cfg, codec.JSON)
    }

    if strings.HasPrefix(endpoint, "rpc:") {
        parts := strings.Split(endpoint, ":")
        addr := "http://localhost"
        port := uint(8545)
        if len(parts) >= 3 {
            addr = parts[1] + ":" + parts[2]
        }

        if len(parts) >= 4 {
            p, err := strconv.Atoi(parts[3])

            if err != nil {
                return nil, err
            }
            port = uint(p)
        }

        cfg := HttpConfig{
            ListenAddress: addr,
            ListenPort:    port,
        }

        return NewHttpClient(cfg, codec.JSON), nil
    }

    return nil, fmt.Errorf("Invalid endpoint")
}