Example #1
0
func NewClientTracker(address string, port int) (p *ClientTracker) {
	p = new(ClientTracker)
	p.address = address
	p.port = port
	p.clients = map[string]time.Time{}
	p.new = eventual2go.NewStreamController()
	p.gone = eventual2go.NewStreamController()
	p.logger = log.New(os.Stdout, "clienttracker ", log.Lshortfile)
	return
}
Example #2
0
func NewOutgoing(uuid string, targetAddress string, targetPort int) (out *eventual2go.StreamController, err error) {

	var o Outgoing

	o.Mutex = new(sync.Mutex)

	o.skt, err = zmq4.NewSocket(zmq4.DEALER)

	if err != nil {
		return
	}

	err = o.skt.SetIdentity(uuid)
	if err != nil {
		return
	}

	err = o.skt.Connect(fmt.Sprintf("tcp://%s:%d", targetAddress, targetPort))

	if err != nil {
		return
	}
	out = eventual2go.NewStreamController()
	out.Stream.Listen(o.send)
	out.Stream.Closed().Then(o.Close)
	return
}
Example #3
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
}
Example #4
0
func newEventHandler() (eh eventHandler) {
	eh.stream = eventual2go.NewStreamController()
	eh.join = eh.stream.Where(isJoin).Transform(toMember)
	eh.leave = eh.stream.Where(isLeave).Transform(toMember)
	eh.query = eh.stream.Where(isQuery)
	return
}
Example #5
0
func (a *Announcer) announce(eventual2go.Data) {
	a.logger.Println("announcing")
	results := eventual2go.NewStreamController()
	c := results.AsChan()
	a.node.Query(a.servicetype, nil, results)
	go a.collectAnnounceResponses(c)
	return
}
Example #6
0
func (b *Beacon) init() {
	b.in = eventual2go.NewStreamController()
	b.stop = make(chan struct{})
	b.outConns = []*net.UDPConn{}
	b.silence = eventual2go.NewCompleter()
	b.silent = true
	b.logger = log.New(b.conf.Logger, fmt.Sprintf("beacon %v ", b.payload[1]), log.Lshortfile)
}
Example #7
0
func NewIncoming(addr string) (i *Incoming, err error) {
	i = new(Incoming)
	i.m = new(sync.Mutex)
	i.addr = addr
	i.in = eventual2go.NewStreamController()
	err = i.setupSocket()
	if err == nil {
		go i.listen()
	}
	return
}
Example #8
0
func (a *Announcer) replyToExport(d eventual2go.Data) {
	q := d.(node.QueryEvent)
	a.logger.Println("found exporting service: ", q.Query.Payload)
	a.logger.Println(q)

	results := eventual2go.NewStreamController()
	c := results.AsChan()
	go a.collectAnnounceResponses(c)

	a.node.QueryUuid(fmt.Sprintf("%s", q.Query.Payload), IMPORTING, nil, results)
}
Example #9
0
func (a *Announcer) announce(eventual2go.Data) {
	a.logger.Println("announcing")
	var results *eventual2go.StreamController
	if a.servicetype == IMPORTING {
		results = eventual2go.NewStreamController()
		c := results.AsChan()
		go a.collectAnnounceResponses(c)
	} else {
		a.announced.Complete(nil)
	}
	a.node.Query(a.servicetype, a.node.UUID, results)
	return
}
Example #10
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
}
Example #11
0
func New(uuid string, cfg *config.Config, tags map[string]string) (node *Node) {

	node = new(Node)
	node.logger = log.New(cfg.Logger(), fmt.Sprintf("node %s ", uuid), log.Lshortfile)

	node.logger.Println("Initializing")

	node.cfg = cfg
	node.tags = tags

	node.UUID = uuid
	node.beacons = []*beacon.Beacon{}
	node.logger.Println("Launching Serf Agents")

	node.join = eventual2go.NewStreamController()
	node.join.First().Then(node.silenceBeacons)
	node.leave = eventual2go.NewStreamController()
	node.query = eventual2go.NewStreamController()

	node.createSerfAgents()

	return node
}
Example #12
0
func NewAnnouncer(uuid string, servicetype string, desc *servicedescriptor.ServiceDescriptor, cfg *config.Config, ports []int) (a *Announcer) {

	a = &Announcer{
		announced:   eventual2go.NewCompleter(),
		clientPorts: map[string]int{},
		logger:      log.New(cfg.Logger(), fmt.Sprintf("announcer %s ", uuid), log.Lshortfile),
		new:         eventual2go.NewStreamController(),
		node:        node.New(uuid, cfg, desc.AsTagSet()),
		servicetype: servicetype,
	}

	a.setPorts(cfg, ports)

	a.initReactor()

	return
}
Example #13
0
func (p *ClientTracker) listenUdp() *eventual2go.Stream {
	addr := &net.UDPAddr{IP: net.ParseIP(p.address), Port: p.port}
	p.logger.Println("Starting to listen on", addr)
	conn, err := net.ListenUDP("udp4", addr)
	if err != nil {
		fmt.Println(err)
		panic(err)
	}
	s := eventual2go.NewStreamController()
	go func(stream *eventual2go.StreamController) {
		for {
			data := make([]byte, 128)
			read, _, _ := conn.ReadFromUDP(data)
			stream.Add(data[:read])
		}
	}(s)

	return s.Stream
}
func NewResultStreamController() *ResultStreamController {
	return &ResultStreamController{eventual2go.NewStreamController()}
}
func NewNodeStreamController() *NodeStreamController {
	return &NodeStreamController{eventual2go.NewStreamController()}
}
func NewIntStreamController() *IntStreamController {
	return &IntStreamController{eventual2go.NewStreamController()}
}
func NewUUIDStreamController() *UUIDStreamController {
	return &UUIDStreamController{eventual2go.NewStreamController()}
}
func NewMessageStreamController() *MessageStreamController {
	return &MessageStreamController{eventual2go.NewStreamController()}
}
func NewStringSliceStreamController() *StringSliceStreamController {
	return &StringSliceStreamController{eventual2go.NewStreamController()}
}
Example #20
0
func TestCallOne2Many(t *testing.T) {
	i := aursir4go.NewImport(TEST_APP_DESCRIPTOR, config.DefaultLocalhost())
	defer i.Remove()
	i.Run()
	e1 := aursir4go.NewExport(TEST_APP_DESCRIPTOR, config.DefaultLocalhost())
	defer e1.Remove()
	c1 := e1.Requests().AsChan()
	e1.Run()
	e2 := aursir4go.NewExport(TEST_APP_DESCRIPTOR, config.DefaultLocalhost())
	defer e2.Remove()
	c2 := e2.Requests().AsChan()
	e2.Run()

	i.Connected().WaitUntilComplete()
	e1.Connected().WaitUntilComplete()
	e2.Connected().WaitUntilComplete()

	params := []byte{4, 5, 63, 4}
	params1 := []byte{3}
	params2 := []byte{6}
	s := eventual2go.NewStreamController()
	s1, s2 := s.Split(func(d eventual2go.Data) bool { return d.(*messages.Result).Exporter == e1.UUID() })
	rc1 := s1.AsChan()
	rc2 := s2.AsChan()
	i.CallAll("SayHello", params, s)
	select {
	case <-time.After(5 * time.Second):
		t.Fatal("Didnt Got Request 1")
	case d := <-c1:
		r := d.(*messages.Request)
		if r.Importer != i.UUID() {
			t.Error("Wrong Import UUID 1", r.Importer, i.UUID())
		}

		var res []byte
		r.Decode(&res)
		if !bytes.Equal(res, params) {
			t.Error("Wrong Params 1", r.Parameter(), params)
		}
		e1.Reply(r, params1)
	}

	select {
	case <-time.After(2 * time.Second):
		t.Fatal("Didnt Got Request 2")
	case d := <-c2:
		r := d.(*messages.Request)
		if r.Importer != i.UUID() {
			t.Error("Wrong Import UUID 2", r.Importer, i.UUID())
		}

		var res []byte
		r.Decode(&res)
		if !bytes.Equal(res, params) {
			t.Error("Wrong Params 2", r.Parameter(), params)
		}
		e2.Reply(r, params2)
	}

	select {
	case <-time.After(2 * time.Second):
		t.Fatal("Didnt Got Result 1")
	case d := <-rc1:
		r := d.(*messages.Result)
		if r.Exporter != e1.UUID() {
			t.Error("Wrong Export UUID", r.Exporter, e1.UUID())
		}

		var res []byte
		r.Decode(&res)
		if !bytes.Equal(res, params1) {
			t.Error("Wrong Params", r.Parameter(), params1)
		}
	}
	select {
	case <-time.After(2 * time.Second):
		t.Fatal("Didnt Got Result 2")
	case d := <-rc2:
		r := d.(*messages.Result)
		if r.Exporter != e2.UUID() {
			t.Error("Wrong Export UUID", r.Exporter, e2.UUID())
		}

		var res []byte
		r.Decode(&res)
		if !bytes.Equal(res, params2) {
			t.Error("Wrong Params", r.Parameter(), params2)
		}
	}

}
func NewSignalStreamController() *SignalStreamController {
	return &SignalStreamController{eventual2go.NewStreamController()}
}
func NewPeerStreamController() *PeerStreamController {
	return &PeerStreamController{eventual2go.NewStreamController()}
}
func NewBoolStreamController() *BoolStreamController {
	return &BoolStreamController{eventual2go.NewStreamController()}
}