forked from chrislusf/glow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchannels.go
93 lines (79 loc) · 2.31 KB
/
channels.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
// Package netchan creates network channels. The network channels are managed by
// glow agent.
package netchan
import (
"bytes"
"encoding/gob"
"flag"
"log"
"reflect"
"strconv"
"sync"
"github.com/chrislusf/glow/netchan/receiver"
"github.com/chrislusf/glow/netchan/sender"
"github.com/chrislusf/glow/util"
)
type NetworkOption struct {
AgentPort int
}
var networkOption NetworkOption
func init() {
flag.IntVar(&networkOption.AgentPort, "glow.agent.port", 8931, "agent port")
}
func GetLocalSendChannel(name string, wg *sync.WaitGroup) (chan []byte, error) {
return sender.NewSendChannel(name, networkOption.AgentPort, wg)
}
func GetLocalReadChannel(name string, chanBufferSize int) (chan []byte, error) {
return GetDirectReadChannel(name, "localhost:"+strconv.Itoa(networkOption.AgentPort), chanBufferSize)
}
func GetDirectReadChannel(name, location string, chanBufferSize int) (chan []byte, error) {
rc := receiver.NewReceiveChannel(name, 0)
return rc.GetDirectChannel(location, chanBufferSize)
}
func GetDirectSendChannel(name string, target string, wg *sync.WaitGroup) (chan []byte, error) {
return sender.NewDirectSendChannel(name, target, wg)
}
func ConnectRawReadChannelToTyped(c chan []byte, out chan reflect.Value, t reflect.Type, wg *sync.WaitGroup) (status *util.ChannelStatus) {
status = util.NewChannelStatus()
wg.Add(1)
go func() {
defer wg.Done()
status.ReportStart()
for data := range c {
dec := gob.NewDecoder(bytes.NewBuffer(data))
v := reflect.New(t)
if err := dec.DecodeValue(v); err != nil {
log.Fatal("data type:", v.Kind(), " decode error:", err)
} else {
out <- reflect.Indirect(v)
status.ReportAdd(1)
}
}
close(out)
status.ReportClose()
}()
return status
}
func ConnectTypedWriteChannelToRaw(writeChan reflect.Value, c chan []byte, wg *sync.WaitGroup) (status *util.ChannelStatus) {
status = util.NewChannelStatus()
wg.Add(1)
go func() {
defer wg.Done()
status.ReportStart()
var t reflect.Value
for ok := true; ok; {
if t, ok = writeChan.Recv(); ok {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
if err := enc.EncodeValue(t); err != nil {
log.Fatal("data type:", t.Type().String(), " ", t.Kind(), " encode error:", err)
}
c <- buf.Bytes()
status.ReportAdd(1)
}
}
close(c)
status.ReportClose()
}()
return status
}