| 418 | } |
| 419 | |
| 420 | func (n *server) messageDispatch() { |
| 421 | subscriptions := make(map[string]chan P2PMessage) |
| 422 | for { |
| 423 | select { |
| 424 | case msg, ok := <-n.peersFeed: |
| 425 | if ok { |
| 426 | if msg.Msg.Message == nil { |
| 427 | continue |
| 428 | } |
| 429 | messagetype := reflect.TypeOf(msg.Msg.Message).String() |
| 430 | if len(messagetype) > 0 && messagetype[0] == '*' { |
| 431 | messagetype = messagetype[1:] |
| 432 | } |
| 433 | if out := subscriptions[messagetype]; out != nil { |
| 434 | select { |
| 435 | case <-n.ctx.Done(): |
| 436 | case out <- msg: |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | case sub, ok := <-n.subscribeMsg: |
| 441 | if ok { |
| 442 | subscriptions[sub.msgType] = sub.msgCh |
| 443 | } |
| 444 | case msgType, ok := <-n.unscribeMsg: |
| 445 | if ok { |
| 446 | delete(subscriptions, msgType) |
| 447 | } |
| 448 | case <-n.ctx.Done(): |
| 449 | for _, outch := range subscriptions { |
| 450 | if outch != nil { |
| 451 | close(outch) |
| 452 | } |
| 453 | } |
| 454 | return |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | // SubscribeEvent is a message subscription operation binding the P2PMessage |
| 460 | func (n *server) SubscribeEvent() (subID int, outch chan discover.P2PEvent, err error) { |