From b1c8ed363bdab0d3618077e93a79df227559539c Mon Sep 17 00:00:00 2001 From: FUJITA Tomonori Date: Thu, 18 Dec 2014 17:23:22 -0800 Subject: server: add message delivery between peers a peer notifies other peers of its event (join,leave,bgp state change, updates, etc) It would have better performance for a peer to send a message to a channel of another peer directly. However, for the code simplicity, the hub-and-spoke model is used for now. Signed-off-by: FUJITA Tomonori --- server/server.go | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) (limited to 'server/server.go') diff --git a/server/server.go b/server/server.go index 56fe5960..3f7885a6 100644 --- a/server/server.go +++ b/server/server.go @@ -24,12 +24,20 @@ import ( "strings" ) +type message struct { + src string + dst string + event int + data interface{} +} + type BgpServer struct { bgpConfig config.BgpType globalTypeCh chan config.GlobalType addedPeerCh chan config.NeighborType deletedPeerCh chan config.NeighborType listenPort int + peerMap map[string]*Peer } func NewBgpServer(port int) *BgpServer { @@ -65,14 +73,15 @@ func (server *BgpServer) Serve() { } }() - peerMap := make(map[string]*Peer) + server.peerMap = make(map[string]*Peer) + broadcastCh := make(chan *message) for { f, _ := l.File() select { case conn := <-acceptCh: fmt.Println(conn) remoteAddr := strings.Split(conn.RemoteAddr().String(), ":")[0] - peer, found := peerMap[remoteAddr] + peer, found := server.peerMap[remoteAddr] if found { fmt.Println("found neighbor", remoteAddr) peer.PassConn(conn) @@ -84,20 +93,22 @@ func (server *BgpServer) Serve() { fmt.Println(peer) addr := peer.NeighborAddress.String() SetTcpMD5SigSockopts(int(f.Fd()), addr, peer.AuthPassword) - p := NewPeer(server.bgpConfig.Global, peer) - peerMap[peer.NeighborAddress.String()] = p + p := NewPeer(server.bgpConfig.Global, peer, broadcastCh) + server.peerMap[peer.NeighborAddress.String()] = p case peer := <-server.deletedPeerCh: fmt.Println(peer) addr := peer.NeighborAddress.String() SetTcpMD5SigSockopts(int(f.Fd()), addr, "") - p, found := peerMap[addr] + p, found := server.peerMap[addr] if found { fmt.Println("found neighbor", addr) p.Stop() - delete(peerMap, addr) + delete(server.peerMap, addr) } else { fmt.Println("can't found neighbor", addr) } + case msg := <-broadcastCh: + server.broadcast(msg) } } } @@ -113,3 +124,15 @@ func (server *BgpServer) PeerAdd(peer config.NeighborType) { func (server *BgpServer) PeerDelete(peer config.NeighborType) { server.deletedPeerCh <- peer } + +func (server *BgpServer) broadcast(msg *message) { + for key := range server.peerMap { + if key == msg.src { + continue + } + if msg.dst == "" || msg.dst == key { + peer := server.peerMap[key] + peer.SendMessage(msg) + } + } +} -- cgit v1.2.3