LEFT | RIGHT |
(no file at all) | |
1 package jsoncodec | 1 package jsoncodec |
2 | 2 |
3 import ( | 3 import ( |
4 "code.google.com/p/go.net/websocket" | 4 "code.google.com/p/go.net/websocket" |
5 "encoding/json" | 5 "encoding/json" |
6 "launchpad.net/juju-core/rpc" | |
7 "net" | 6 "net" |
8 ) | 7 ) |
9 | 8 |
10 // NewWebsocket returns an rpc codec that uses the given websocket | 9 // NewWebsocket returns an rpc codec that uses the given websocket |
11 // connection to send and receive messages. | 10 // connection to send and receive messages. |
12 func NewWebsocket(conn *websocket.Conn) rpc.Codec { | 11 func NewWebsocket(conn *websocket.Conn) *Codec { |
13 return New(wsJSONConn{conn}) | 12 return New(wsJSONConn{conn}) |
14 } | 13 } |
15 | 14 |
16 type wsJSONConn struct { | 15 type wsJSONConn struct { |
17 conn *websocket.Conn | 16 conn *websocket.Conn |
18 } | 17 } |
19 | 18 |
20 func (conn wsJSONConn) Send(msg interface{}) error { | 19 func (conn wsJSONConn) Send(msg interface{}) error { |
21 return websocket.JSON.Send(conn.conn, msg) | 20 return websocket.JSON.Send(conn.conn, msg) |
22 } | 21 } |
23 | 22 |
24 func (conn wsJSONConn) Receive(msg interface{}) error { | 23 func (conn wsJSONConn) Receive(msg interface{}) error { |
25 return websocket.JSON.Receive(conn.conn, msg) | 24 return websocket.JSON.Receive(conn.conn, msg) |
26 } | 25 } |
27 | 26 |
28 func (conn wsJSONConn) Close() error { | 27 func (conn wsJSONConn) Close() error { |
29 return conn.conn.Close() | 28 return conn.conn.Close() |
30 } | 29 } |
31 | 30 |
32 // NewNet returns an rpc codec that uses the given net | 31 // NewNet returns an rpc codec that uses the given net |
33 // connection to send and receive messages. | 32 // connection to send and receive messages. |
34 func NewNet(conn net.Conn) rpc.Codec { | 33 func NewNet(conn net.Conn) *Codec { |
35 return New(&netConn{ | 34 return New(&netConn{ |
36 enc: json.NewEncoder(conn), | 35 enc: json.NewEncoder(conn), |
37 dec: json.NewDecoder(conn), | 36 dec: json.NewDecoder(conn), |
38 conn: conn, | 37 conn: conn, |
39 }) | 38 }) |
40 } | 39 } |
41 | 40 |
42 type netConn struct { | 41 type netConn struct { |
43 enc *json.Encoder | 42 enc *json.Encoder |
44 dec *json.Decoder | 43 dec *json.Decoder |
45 conn net.Conn | 44 conn net.Conn |
46 } | 45 } |
47 | 46 |
48 func (conn *netConn) Send(msg interface{}) error { | 47 func (conn *netConn) Send(msg interface{}) error { |
49 return conn.enc.Encode(msg) | 48 return conn.enc.Encode(msg) |
50 } | 49 } |
51 | 50 |
52 func (conn *netConn) Receive(msg interface{}) error { | 51 func (conn *netConn) Receive(msg interface{}) error { |
53 return conn.dec.Decode(msg) | 52 return conn.dec.Decode(msg) |
54 } | 53 } |
55 | 54 |
56 func (conn *netConn) Close() error { | 55 func (conn *netConn) Close() error { |
57 return conn.conn.Close() | 56 return conn.conn.Close() |
58 } | 57 } |
LEFT | RIGHT |