Exemple #1
0
func NewAnnouncer(uuid string, addresses []string, servicetype string, desc *appdescriptor.AppDescriptor) (a *Announcer) {

	cfg := config.DefaultLocalhost()

	a = new(Announcer)
	a.announced = eventual2go.NewCompleter()
	a.logger = log.New(cfg.Logger(), fmt.Sprintf("announcer %s ", uuid), log.Lshortfile)

	a.new = eventual2go.NewStreamController()
	a.servicetype = servicetype
	addrs := []string{}
	a.clientPorts = map[string]int{}

	for _, addr := range addresses {
		as := strings.Split(addr, ":")
		addrs = append(addrs, as[0])
		p, _ := strconv.ParseInt(as[1], 0, 0)
		a.clientPorts[as[0]] = int(p)
		a.logger.Println("adding address", as[0], int(p))
	}

	cfg.NetworkInterfaces = addrs
	a.node = node.New(uuid, cfg, desc.AsTagSet())

	a.r = eventual2go.NewReactor()
	a.r.React("first_join", a.announce)
	a.r.AddFuture("first_join", a.node.Join().First())
	a.r.React("service_found", a.replyToServiceQuery)
	a.r.AddStream("service_found", a.node.Queries().WhereNot(isService(a.servicetype)))
	a.logger.Println("setup finished")
	return
}
Exemple #2
0
func New(addr string, port int) (d *Daemon) {

	d = new(Daemon)

	d.clients = map[string]*eventual2go.StreamController{}
	d.announcer = map[string]*service.Announcer{}

	d.logger = log.New(os.Stdout, "daemon ", log.Lshortfile)
	d.logger.Println("starting up")

	incoming, _ := connection.NewIncoming(addr)
	d.logger.Println("launched incoming at", incoming.Port())

	msg := incoming.In().Where(ValidMessage)

	ct := NewClientTracker(addr, port)

	r := eventual2go.NewReactor()

	r.React("new_client", d.newClient(incoming.Port()))
	r.AddStream("new_client", ct.new.Stream)

	r.React("client_gone", d.clientGone)
	r.AddStream("client_gone", ct.gone.Stream)

	r.React("client_service", d.clientService)
	r.AddStream("client_service", msg.Where(IsMessage(EXPORT)).Transform(ToNewServiceMessage))
	r.AddStream("client_service", msg.Where(IsMessage(IMPORT)).Transform(ToNewServiceMessage))

	d.logger.Println("starting tracker")
	ct.Run()

	d.logger.Println("started")
	return
}
Exemple #3
0
// NewInputFromConfig creates a new Input instance for a given configuration.
func NewInputFromConfig(cfg *config.Config) (i *Input, err error) {
	m, err := manager.New(cfg)
	if err != nil {
		return
	}
	i = &Input{
		m:       m,
		cfg:     cfg,
		r:       eventual2go.NewReactor(),
		listen:  map[string]interface{}{},
		logger:  log.New(cfg.Logger(), fmt.Sprintf("%s INPUT ", cfg.UUID()), 0),
		results: &messages.ResultStream{m.MessagesOfType(messages.RESULT).Transform(connection.ToMessage)},
	}

	i.logger.Println("Launching with tagset", cfg.Tags())

	i.r.React(connectionEvent{}, i.onConnection)
	i.r.AddStream(connectionEvent{}, m.Connected().Stream)

	i.r.React(arriveEvent{}, i.sendListenFunctions)
	i.r.AddStream(arriveEvent{}, m.PeerArrive().Stream)

	i.r.React(startListenEvent{}, i.startListen)
	i.r.React(stopListenEvent{}, i.stopListen)

	return
}
func (a *Announcer) initReactor() {
	a.r = eventual2go.NewReactor()
	a.r.React("first_join", a.announce)
	a.r.AddFuture("first_join", a.node.Join().First())
	a.r.React("service_found", a.replyToService(a.servicetype))
	a.r.AddStream("service_found", a.node.Queries().WhereNot(isService(a.servicetype)))
	a.logger.Println("setup finished")
}
func (p *ClientTracker) Run() {
	p.logger.Println("starting")
	r := eventual2go.NewReactor()
	r.React("ping", p.checkInclient)
	r.AddStream("ping", p.listenUdp())
	r.React("check", p.checkclients)
	go func(reactor *eventual2go.Reactor) {
		for range time.Tick(1 * time.Second) {
			reactor.Fire("check", nil)
		}
	}(r)
}
Exemple #6
0
func NewExport(a *appdescriptor.AppDescriptor, cfg *config.Config) (e *Export) {
	e = new(Export)
	e.Service = service.NewService(a, service.EXPORTING, cfg, []byte{0})
	e.logger = log.New(cfg.Logger(), fmt.Sprintf("export %s ", e.UUID()), log.Lshortfile)
	e.requests = e.Messages(messages.REQUEST)
	e.listeners = map[string][]string{}
	e.r = eventual2go.NewReactor()
	e.r.React("listen", e.newListener)
	e.r.AddStream("listen", e.IncomingMessages(messages.LISTEN))
	e.r.React("listen_stop", e.stopListener)
	e.r.AddStream("listen", e.IncomingMessages(messages.STOP_LISTEN))
	e.r.React("reply", e.deliverResult)
	return
}
func New() (sr *SerialReactor, err error) {
	c := &serial.Config{Name: "/dev/ttyUSB1", Baud: 9600}
	s, err := serial.OpenPort(c)
	if err != nil {
		return
	}

	sr = &SerialReactor{
		Reactor: eventual2go.NewReactor(),
		s:       s,
	}
	go sr.listen()
	return
}
func New(cfg *config.Config) (m *Manager, err error) {
	m = &Manager{
		r:                  eventual2go.NewReactor(),
		connected:          typed_events.NewBoolStreamController(),
		peers:              map[config.UUID]*peer.Peer{},
		arrive:             config.NewUUIDStreamController(),
		leave:              config.NewUUIDStreamController(),
		guaranteedMessages: map[messages.Message]*peer.Peer{},
		messageIn:          connection.NewMessageStreamController(),
		logger:             log.New(cfg.Logger(), fmt.Sprintf("%s MANAGER ", cfg.UUID()), 0),
		shutdown:           eventual2go.NewShutdown(),
		shutdownComplete:   eventual2go.NewCompleter(),
	}

	m.r.React(peerLeave{}, m.peerLeave)
	m.r.React(peerConnected{}, m.peerConnected)
	m.r.OnShutdown(m.onShutdown)

	for _, iface := range cfg.Interfaces() {

		var i *connection.Incoming
		i, err = connection.NewIncoming(iface)
		if err != nil {
			m.logger.Println("ERROR setting up incoming:", err)
			m.Shutdown()
			return
		}
		m.shutdown.Register(i)
		m.messageIn.Join(i.In().Where(filterMessages))

		var t *tracker.Tracker
		t, err = tracker.New(iface, i.Port(), cfg)
		if err != nil {
			m.logger.Println("ERROR setting up tracker:", err)
			m.Shutdown()
			return
		}
		m.tracker = append(m.tracker, t)
		m.shutdown.Register(t)
		m.r.AddStream(peerLeave{}, t.Leave().Stream)

		var d *discoverer.Discoverer
		d = discoverer.New(t.Join(), i, cfg)
		m.r.AddStream(peerConnected{}, d.ConnectedPeers().Stream)
	}

	return
}
Exemple #9
0
func NewService(dsc *servicedescriptor.ServiceDescriptor, servicetype string, cfg *config.Config, codecs []byte) (s *Service) {
	s = new(Service)

	if cfg.UUID == "" {
		id, _ := uid.NewV4()
		cfg.UUID = id.String()
	}
	s.uuid = cfg.UUID
	s.logger = log.New(cfg.Logger(), fmt.Sprintf("service %s  ", s.uuid), log.Lshortfile)
	s.serviceDescriptor = dsc

	s.logger.Println("ServiceDesctiptor TagsSet is", dsc.AsTagSet())

	s.servicetype = servicetype
	s.codecs = codecs
	s.newpeers = eventual2go.NewStreamController()
	s.gonepeers = eventual2go.NewStreamController()

	s.incoming = map[string]*connection.Incoming{}
	s.in = eventual2go.NewStreamController()

	s.connectedServices = map[string]*ServiceConnection{}
	s.connected = eventual2go.NewCompleter()
	s.disconnected = eventual2go.NewCompleter()
	s.remove = eventual2go.NewCompleter()

	s.r = eventual2go.NewReactor()
	s.r.React("service_arrived", s.serviceArrived)
	s.r.React("service_gone", s.serviceGone)
	s.r.React("announce_finish", s.announced)
	s.r.React("service_shake_hand", s.serviceHandshake)
	s.r.AddStream("service_shake_hand", s.in.Where(messages.Is(messages.HELLO)))
	s.r.React("service_shake_hand_reply", s.serviceHandShakeReply)
	s.r.AddStream("service_shake_hand_reply", s.in.Where(messages.Is(messages.HELLO_OK)))
	s.createIncoming(cfg)
	s.createAnnouncer(cfg)
	return
}
Exemple #10
0
// NewOutputFromConfig creates a new Output instance for a given configuration.
func NewOutputFromConfig(cfg *config.Config) (o *Output, err error) {
	m, err := manager.New(cfg)
	o = &Output{
		cfg:       cfg,
		m:         m,
		requests:  &messages.RequestStream{m.MessagesOfType(messages.REQUEST).Transform(connection.ToMessage)},
		listeners: map[string]map[config.UUID]interface{}{},
		logger:    log.New(cfg.Logger(), fmt.Sprintf("%s OUTPUT ", cfg.UUID()), 0),
		r:         eventual2go.NewReactor(),
	}

	o.logger.Println("Launching with tagset", cfg.Tags())

	o.r.React(startListenEvent{}, o.onListen)
	o.r.AddStream(startListenEvent{}, m.MessagesOfType(messages.LISTEN).Stream)

	o.r.React(stopListenEvent{}, o.onStopListen)
	o.r.AddStream(stopListenEvent{}, m.MessagesOfType(messages.STOPLISTEN).Stream)

	o.r.React(leaveEvent{}, o.onPeerGone)

	o.r.React(replyEvent{}, o.deliverResult)
	return
}