-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathedgerouter.go
149 lines (134 loc) · 3.17 KB
/
edgerouter.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package edgerouter
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"reflect"
"strings"
"context"
"github.com/BurntSushi/toml"
"github.com/golang/glog"
)
func Organize(name string, plugins ...interface{}) *EdgeRouter {
er := new(EdgeRouter)
er.Name = name
er.Plugins = make(map[string]interface{})
for _, plugin := range plugins {
rp := reflect.ValueOf(plugin)
if rp.Type().Kind() == reflect.Ptr {
rp = rp.Elem()
}
path := rp.Type().PkgPath()
paths := strings.Split(path, "/")
er.Plugins[paths[len(paths)-1]] = plugin
}
return er
}
type EdgeRouter struct {
Name string
Plugins map[string]interface{}
components map[string]Component
configged bool
}
func (er *EdgeRouter) ConfigBy(filename string) error {
bs, err := ioutil.ReadFile(filename)
if err == nil {
return er.ConfigByString(string(bs))
}
return err
}
func (er *EdgeRouter) ConfigByString(str string) error {
var tmp = make(map[string]toml.Primitive)
var err error
var meta toml.MetaData
if len(er.Plugins) == 0 {
return errors.New("no plugin for edge router")
}
if meta, err = toml.Decode(str, &tmp); err == nil {
er.components = make(map[string]Component)
for k, plugin := range er.Plugins {
glog.Infoln("start parse plugin", k)
rp := reflect.ValueOf(plugin)
var com Component
for i := 0; i < rp.NumField(); i++ {
fp := rp.Field(i)
ft := fp.Type()
val := reflect.New(ft).Interface()
if err := meta.PrimitiveDecode(tmp[k], val); err == nil {
if vc, ok := val.(Controller); ok {
com.Ctrl = vc
}
if vs, ok := val.(Server); ok {
com.Server = vs
}
if vt, ok := val.(Transport); ok {
com.Trans = vt
}
}
initCheck(val)
}
val := reflect.New(rp.Type()).Interface()
if err = meta.PrimitiveDecode(tmp[k], val); err != nil {
goto errHandle
}
com.Ctrl.SetTransport(com.Trans)
if err = com.Ctrl.SetHandler(val); err != nil {
goto errHandle
}
com.Trans.SetController(com.Ctrl)
er.components[k] = com
er.Plugins[k] = val
}
}
er.configged = true
return err
errHandle:
glog.Errorln(err)
return err
}
func (er *EdgeRouter) Run() {
if !er.configged {
cfgPath := flag.String("config", er.Name+".conf", "config file path for the edge router")
flag.Parse()
var err error
if err = er.ConfigBy(*cfgPath); err != nil {
glog.Errorln(err)
} else {
er.configged = true
}
}
if er.configged {
glog.Infoln("run edge router")
ctx := context.Background()
for name, component := range er.components {
glog.Infof("run server (%s) AS (%s)", name, component)
if component.Server != nil {
go component.Server.Run()
}
go component.Ctrl.Run()
// plugin := er.Plugins[name]
// for _, server := range servers {
// fmt.Println("start", server)
// if ctx, err = server.Run(ctx, plugin); err != nil || ctx == nil {
// log.Fatal(ctx, err)
// }
// }
}
for {
select {
case <-ctx.Done():
fmt.Println("-")
}
}
} else {
glog.Errorln("not configged correctly")
}
}
func initCheck(val interface{}) {
if iv, ok := val.(Initer); ok {
if err := iv.Init(); err != nil {
glog.Errorf("%T(%v) need init,occur error(%s)", val, val, err)
}
}
}