github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/events/actor_event.go (about)

     1  /*
     2   * Copyright (C) 2018 The ontology Authors
     3   * This file is part of The ontology library.
     4   *
     5   * The ontology is free software: you can redistribute it and/or modify
     6   * it under the terms of the GNU Lesser General Public License as published by
     7   * the Free Software Foundation, either version 3 of the License, or
     8   * (at your option) any later version.
     9   *
    10   * The ontology is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU Lesser General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU Lesser General Public License
    16   * along with The ontology.  If not, see <http://www.gnu.org/licenses/>.
    17   */
    18  
    19  package events
    20  
    21  import (
    22  	"fmt"
    23  
    24  	"github.com/ontio/ontology-eventbus/actor"
    25  	"github.com/ontio/ontology-eventbus/eventhub"
    26  )
    27  
    28  var DefEvtHub *eventhub.EventHub
    29  var DefPublisherPID *actor.PID
    30  var DefActorPublisher *ActorPublisher
    31  var defPublisherProps *actor.Props
    32  
    33  func Init() {
    34  	DefEvtHub = eventhub.GlobalEventHub
    35  	defPublisherProps = actor.FromFunc(func(context actor.Context) {})
    36  	var err error
    37  	DefPublisherPID, err = actor.SpawnNamed(defPublisherProps, "DefPublisherActor")
    38  	if err != nil {
    39  		panic(fmt.Errorf("DefPublisherPID SpawnNamed error:%s", err))
    40  	}
    41  	DefActorPublisher = NewActorPublisher(DefPublisherPID)
    42  }
    43  
    44  func NewActorPublisher(publisher *actor.PID, evtHub ...*eventhub.EventHub) *ActorPublisher {
    45  	var hub *eventhub.EventHub
    46  	if len(evtHub) == 0 {
    47  		hub = DefEvtHub
    48  	} else {
    49  		hub = evtHub[0]
    50  	}
    51  	if publisher == nil {
    52  		publisher = DefPublisherPID
    53  	}
    54  	return &ActorPublisher{
    55  		EvtHub:    hub,
    56  		Publisher: publisher,
    57  	}
    58  }
    59  
    60  type ActorPublisher struct {
    61  	EvtHub    *eventhub.EventHub
    62  	Publisher *actor.PID
    63  }
    64  
    65  func (this *ActorPublisher) Publish(topic string, msg interface{}) {
    66  	event := &eventhub.Event{
    67  		Publisher: this.Publisher,
    68  		Message:   msg,
    69  		Topic:     topic,
    70  		Policy:    eventhub.PublishPolicyAll,
    71  	}
    72  	this.EvtHub.Publish(event)
    73  }
    74  
    75  func (this *ActorPublisher) PublishEvent(evt *eventhub.Event) {
    76  	this.EvtHub.Publish(evt)
    77  }
    78  
    79  type ActorSubscriber struct {
    80  	EvtHub     *eventhub.EventHub
    81  	Subscriber *actor.PID
    82  }
    83  
    84  func NewActorSubscriber(subscriber *actor.PID, evtHub ...*eventhub.EventHub) *ActorSubscriber {
    85  	var hub *eventhub.EventHub
    86  	if len(evtHub) == 0 {
    87  		hub = DefEvtHub
    88  	} else {
    89  		hub = evtHub[0]
    90  	}
    91  	return &ActorSubscriber{
    92  		EvtHub:     hub,
    93  		Subscriber: subscriber,
    94  	}
    95  }
    96  
    97  func (this *ActorSubscriber) Subscribe(topic string) {
    98  	this.EvtHub.Subscribe(topic, this.Subscriber)
    99  }
   100  
   101  func (this *ActorSubscriber) Unsubscribe(topic string) {
   102  	this.EvtHub.Unsubscribe(topic, this.Subscriber)
   103  }