Exemplo n.º 1
0
// New creates a new Telegraph instance
func New(userHash string, hostnames []string, checkInterval time.Duration,
	maxConnectionCount int, keyPair cryptochain.KeyPair, debug bool) (Telegraph, error) {
	cc := cryptochain.NewCryptoChain(userHash, keyPair)
	// Protocol input will be used to process Newton protocol messages.
	pi := make(chan store.ProtocolInput)
	// Dispatcher routes messages and runs appropriate functions to process messages correctly.
	dp, err := dispatcher.New(userHash, hostnames, checkInterval, maxConnectionCount, cc, pi, debug)
	if err != nil {
		return nil, err
	}

	l := logger.New("telegraph")

	// Get a message store
	ms := store.NewMessageStore(1000 * time.Millisecond)
	t := &telegraph{
		dispatcher:     dp,
		pool:           dp.GetPool(),
		log:            l,
		protocolOutput: make(chan ProtocolMessage, 100),
		protocolInput:  pi,
		messageStore:   ms,
		cryptoChain:    cc,
		waitGroup:      &sync.WaitGroup{},
		done:           make(chan struct{}),
	}

	t.waitGroup.Add(3)
	go t.processCryptoOutput()
	go t.processProtocolOutput()
	go t.processProtocolInput()
	return t, nil
}
Exemplo n.º 2
0
// setupPool creates a new Pool instance for test purposes
func setupPool(numberOfServers, maxConnCount int, checkInterval time.Duration, t *testing.T, userHash string) (Pool, *httpServers, error) {
	hostnames := []string{}
	c := 0
	hs := &httpServers{
		h: make(map[string]*cstServer),
	}
	for {
		c++
		s := newServer(t)
		u := strings.Trim(s.URL, "ws://")
		hostnames = append(hostnames, u)
		hs.h[u] = s
		if c >= numberOfServers {
			break
		}

	}
	publicKey := "84c9ec38f2201a604fba5a7f8137d6d612fb557ce1b852ddd880fe98ba135b4a"
	privateKey := "3af3587afe7b3f719c333a6ec03c956c1aa04a1c245e750565bdd81a0c4498d6"
	keyPair, err := PrepareKeyPair(publicKey, privateKey)
	if err != nil {
		return nil, nil, err
	}
	cc := cryptochain.NewCryptoChain(userHash, *keyPair)
	p, err := New(userHash, hostnames, checkInterval, maxConnCount, cc.ConnectionStore, false)
	return p, hs, err
}
Exemplo n.º 3
0
// setupDispatcher creates a new Pool instance for test purposes
func setupDispatcher(t *testing.T) (Dispatcher, error) {
	hostnames := []string{}
	s := newServer(t)
	u := strings.Trim(s.URL, "ws://")
	hostnames = append(hostnames, u)
	userHash := "94f5f8dd64b297708de84ee2bec9d198987082f7"
	checkInterval := 60000 * time.Millisecond

	publicKey := "84c9ec38f2201a604fba5a7f8137d6d612fb557ce1b852ddd880fe98ba135b4a"
	privateKey := "3af3587afe7b3f719c333a6ec03c956c1aa04a1c245e750565bdd81a0c4498d6"
	keyPair, err := pool.PrepareKeyPair(publicKey, privateKey)
	if err != nil {
		return nil, err
	}
	cc := cryptochain.NewCryptoChain(userHash, *keyPair)
	pi := make(chan store.ProtocolInput)
	d, err := New(userHash, hostnames, checkInterval, 5, cc, pi, true)
	return d, err
}