-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory.go
102 lines (78 loc) · 1.94 KB
/
factory.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
package gorpc
import (
)
type Factory struct {
}
type FactoryGetter struct {
factory IFactory
}
var instance IFactory
func init() {
instance = &Factory{}
}
func SetFactory(factory IFactory) {
instance = factory
}
func GetFactory() IFactory {
return instance
}
func (this *Factory) MakeAddress(src, dest string, options interface{}) IConnectionAddress {
return &ConnectionAddress{src: src, dest: dest, options: options}
}
func (this *Factory) MakeConnection(transport ITransport, addr IConnectionAddress) IConnection {
c := &Connection{}
c.Init(transport, addr)
return c
}
func (this *Factory) MakeController() IController {
return &Controller{}
}
func (this *Factory) MakeProtocol() IProtocol {
p := &Protocol{}
p.SetFactory(this)
return p
}
func (this *Factory) MakeRequest(id, method, params interface{}) IRequest {
r := &Request{}
if v, ok := id.(map[string]interface{}); ok {
r.Populate(v)
} else {
r.SetRequest(id, method, params)
}
return r
}
func (this *Factory) MakeRequestWrapper() IRequestWrapper {
return &RequestWrapper{}
}
func (this *Factory) MakeResponse(id, result, error interface{}) IRequest {
r := &Request{}
r.SetResponse(id, result, error)
return r
}
/*
Make a router takes a protocol validation function as an argument.
The validator can be nill if not needed.
*/
func (this *Factory) MakeRouter() IRouter {
r := &Router{}
r.SetFactory(this)
return r
}
func (this *Factory) MakeRpcError(code int, previous error) IRpcError {
return NewRpcError(code, previous)
}
func (this *Factory) MakeTransport(options ITransportOptions) ITransport {
t := &Transport{Options: options, Protocol: this.MakeProtocol()}
t.SetFactory(this)
t.Init(nil, nil)
return t
}
func (this *Factory) MakeTransportOptions() ITransportOptions {
return &TransportOptions{}
}
func (this *FactoryGetter) SetFactory(factory IFactory) {
this.factory = factory
}
func (this *FactoryGetter) Factory() IFactory {
return this.factory
}