-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from ffdo/multi-interface
Multi interface
- Loading branch information
Showing
10 changed files
with
208 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
src/github.com/dereulenspiegel/node-informant/gluon-collector/receiver.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
|
||
log "github.com/Sirupsen/logrus" | ||
conf "github.com/dereulenspiegel/node-informant/gluon-collector/config" | ||
cfg "github.com/olebedev/config" | ||
|
||
"github.com/dereulenspiegel/node-informant/announced" | ||
) | ||
|
||
type MultiReceiver struct { | ||
packetChan chan announced.Response | ||
childReceiver []announced.AnnouncedPacketReceiver | ||
} | ||
|
||
func NewMultiReceiver(receivers ...announced.AnnouncedPacketReceiver) *MultiReceiver { | ||
mr := &MultiReceiver{make(chan announced.Response, 100), make([]announced.AnnouncedPacketReceiver, 0, 2)} | ||
mr.childReceiver = append(mr.childReceiver, receivers...) | ||
for _, receiver := range receivers { | ||
go mr.singleReceive(receiver) | ||
} | ||
return mr | ||
} | ||
|
||
func (m *MultiReceiver) Query(queryString string) { | ||
for _, receiver := range m.childReceiver { | ||
receiver.Query(queryString) | ||
} | ||
} | ||
|
||
func (m *MultiReceiver) QueryUnicast(addr *net.UDPAddr, queryString string) { | ||
for _, receiver := range m.childReceiver { | ||
receiver.QueryUnicast(addr, queryString) | ||
} | ||
} | ||
|
||
func (m *MultiReceiver) singleReceive(receiver announced.AnnouncedPacketReceiver) { | ||
receiver.Receive(func(response announced.Response) { | ||
m.packetChan <- response | ||
}) | ||
} | ||
|
||
func (m *MultiReceiver) Receive(rFunc func(announced.Response)) { | ||
for packet := range m.packetChan { | ||
rFunc(packet) | ||
} | ||
} | ||
|
||
func (m *MultiReceiver) Close() error { | ||
for _, receiver := range m.childReceiver { | ||
receiver.Close() | ||
} | ||
return nil | ||
} | ||
|
||
func buildReceiver() announced.AnnouncedPacketReceiver { | ||
receiverConfigList, err := conf.Global.List("receiver") | ||
if err != nil { | ||
log.Fatalf("Receiver don't seem to be configured: %v", err) | ||
} | ||
receiverCount := len(receiverConfigList) | ||
receiverSlice := make([]announced.AnnouncedPacketReceiver, 0, receiverCount) | ||
for i := 0; i < receiverCount; i++ { | ||
receiverConfig, err := conf.Global.Get(fmt.Sprintf("receiver.%d", i)) | ||
if err != nil { | ||
log.Fatalf("Error retrieving config for %dth receiver: %v", i, err) | ||
} | ||
receiver := receiverFactory(receiverConfig) | ||
receiverSlice = append(receiverSlice, receiver) | ||
} | ||
|
||
return NewMultiReceiver(receiverSlice...) | ||
} | ||
|
||
func receiverFactory(receiverConfig *cfg.Config) announced.AnnouncedPacketReceiver { | ||
receiverType, err := receiverConfig.String("type") | ||
if err != nil { | ||
log.Fatalf("Can't retrieve type of receiver: %v", err) | ||
} | ||
|
||
switch receiverType { | ||
case "announced": | ||
return buildAnnouncedReceiver(receiverConfig) | ||
default: | ||
log.Fatalf("Unknown receiver type %s", receiverType) | ||
return nil | ||
} | ||
} | ||
|
||
func buildAnnouncedReceiver(announcedConfig *cfg.Config) announced.AnnouncedPacketReceiver { | ||
iface, err := announcedConfig.String("interface") | ||
if err != nil { | ||
log.Fatalf("Can't determine interface for announced receiver") | ||
} | ||
|
||
port, err := announcedConfig.Int("port") | ||
if err != nil { | ||
log.Fatalf("Can't determine port for announced receiver") | ||
} | ||
requester, err := announced.NewRequester(iface, port) | ||
if err != nil { | ||
log.Fatalf("Error creating requester") | ||
} | ||
return requester | ||
} |
50 changes: 50 additions & 0 deletions
50
src/github.com/dereulenspiegel/node-informant/gluon-collector/receiver_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package main | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
"time" | ||
|
||
"github.com/dereulenspiegel/node-informant/announced" | ||
"github.com/dereulenspiegel/node-informant/gluon-collector/test" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var ( | ||
additionalData = []announced.Response{ | ||
announced.Response{ | ||
ClientAddr: &net.UDPAddr{ | ||
IP: net.ParseIP("fe80::1001:bcad"), | ||
Port: 4242, | ||
}, | ||
Payload: []byte("Additional payload"), | ||
}, | ||
} | ||
) | ||
|
||
func TestMultiReceiver(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
testReceiver1 := &TestDataReceiver{test.TestData} | ||
testReceiver2 := &TestDataReceiver{additionalData} | ||
|
||
multiReceiver := NewMultiReceiver(testReceiver1, testReceiver2) | ||
|
||
totalPacketCount := len(test.TestData) + len(additionalData) | ||
packetFound := false | ||
i := 0 | ||
go multiReceiver.Receive(func(packet announced.Response) { | ||
i = i + 1 | ||
payloadString := string(packet.Payload) | ||
if payloadString == "Additional payload" { | ||
packetFound = true | ||
} | ||
}) | ||
testReceiver1.Close() | ||
testReceiver2.Close() | ||
for i < totalPacketCount { | ||
time.Sleep(time.Millisecond * 1) | ||
} | ||
assert.Equal(totalPacketCount, i, "Received less packets than we fed through 2 receiver") | ||
assert.True(packetFound, "Didn't found the additional payload") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters