Example #1
0
func (b *broker) Init(c *component.Component) error {
	b.Component = c
	b.InitStatus()
	err := b.Component.UpdateTokenKey()
	if err != nil {
		return err
	}
	err = b.Component.Announce()
	if err != nil {
		return err
	}
	b.Discovery.GetAll("handler") // Update cache
	var conn *grpc.ClientConn
	if b.nsCert == "" {
		conn, err = api.Dial(b.nsAddr)
	} else {
		conn, err = api.DialWithCert(b.nsAddr, b.nsCert)
	}
	if err != nil {
		return err
	}
	b.nsConn = conn
	b.ns = networkserver.NewNetworkServerClient(conn)
	b.checkPrefixAnnouncements()
	b.Component.SetStatus(component.StatusHealthy)
	return nil
}
Example #2
0
// NewClient returns a new Client
func NewClient(server string, announcement *Announcement, tokenFunc func() string) (Client, error) {
	conn, err := api.Dial(server)
	if err != nil {
		return nil, err
	}
	client := &DefaultClient{
		lists:        make(map[string][]*Announcement),
		listsUpdated: make(map[string]time.Time),
		self:         announcement,
		tokenFunc:    tokenFunc,
		conn:         conn,
		client:       NewDiscoveryClient(conn),
	}
	client.cache = gcache.
		New(CacheSize).
		Expiration(CacheExpiration).
		LRU().
		LoaderFunc(func(k interface{}) (interface{}, error) {
			key, ok := k.(cacheKey)
			if !ok {
				return nil, fmt.Errorf("wrong type for cacheKey: %T", k)
			}
			return client.get(key.serviceName, key.id)
		}).
		Build()
	return client, nil
}
Example #3
0
// Dial dials the component represented by this Announcement
func (a *Announcement) Dial() (*grpc.ClientConn, error) {
	if a.NetAddress == "" {
		return nil, errors.New("Can not dial this component")
	}
	if a.Certificate == "" {
		return api.Dial(strings.Split(a.NetAddress, ",")[0])
	}
	return api.DialWithCert(strings.Split(a.NetAddress, ",")[0], a.Certificate)
}
Example #4
0
// GetDiscovery gets the Discovery client for ttnctl
func GetDiscovery(ctx log.Interface) (*grpc.ClientConn, discovery.DiscoveryClient) {
	path := path.Join(GetDataDir(), "/ca.cert")
	cert, err := ioutil.ReadFile(path)
	if err == nil && !api.RootCAs.AppendCertsFromPEM(cert) {
		ctx.Warnf("Could not add root certificates from %s", path)
	}

	conn, err := api.Dial(viper.GetString("discovery-address"))
	if err != nil {
		ctx.WithError(err).Fatal("Could not connect to Discovery server")
	}
	return conn, discovery.NewDiscoveryClient(conn)
}
Example #5
0
func (cl *Client) open() (err error) {
	addr := cl.addr
	ctx := cl.Ctx.WithField("addr", addr)

	defer func() {
		if err != nil {
			ctx.Warn("Failed to open monitor connection")
		} else {
			ctx.Info("Monitor connection opened")
		}
	}()

	ctx.Debug("Opening monitor connection...")

	cl.conn, err = api.Dial(addr)
	if err != nil {
		ctx.WithError(errors.FromGRPCError(err)).Warn("Failed to establish connection to gRPC service")
		return err
	}

	cl.client = NewMonitorClient(cl.conn)
	return nil
}
func TestHandlerBrokerCommunication(t *testing.T) {
	a := New(t)

	ctx := GetLogger(t, "TestHandlerBrokerCommunication")
	log.Set(apex.Wrap(ctx))

	brk := newTestBroker()
	rand.Seed(time.Now().UnixNano())
	port := rand.Intn(1000) + 10000
	go brk.Serve(port)

	conn, _ := api.Dial(fmt.Sprintf("localhost:%d", port))

	{
		brk.HandlerPublishChanFunc = func(md metadata.MD) (chan *DownlinkMessage, error) {
			ch := make(chan *DownlinkMessage, 1)
			go func() {
				ctx.Info("[SERVER] Channel opened")
				for message := range ch {
					ctx.WithField("Message", message).Info("[SERVER] Received Downlink")
				}
				ctx.Info("[SERVER] Channel closed")
			}()
			return ch, nil
		}

		brkClient := NewBrokerClient(conn)
		downlink := NewMonitoredHandlerPublishStream(brkClient, func() context.Context {
			return context.Background()
		})

		err := downlink.Send(&DownlinkMessage{
			Payload: []byte{1, 2, 3, 4},
		})

		a.So(err, ShouldBeNil)

		time.Sleep(10 * time.Millisecond)

		downlink.Close()

		time.Sleep(10 * time.Millisecond)
	}

	{
		brk.HandlerSubscribeChanFunc = func(md metadata.MD) (<-chan *DeduplicatedUplinkMessage, func(), error) {
			ch := make(chan *DeduplicatedUplinkMessage, 1)
			stop := make(chan struct{})
			cancel := func() {
				ctx.Info("[SERVER] Canceling uplink")
				close(stop)
			}
			go func() {
			loop:
				for {
					select {
					case <-stop:
						break loop
					case <-time.After(5 * time.Millisecond):
						ctx.Info("[SERVER] Sending Uplink")
						ch <- &DeduplicatedUplinkMessage{
							Payload: []byte{1, 2, 3, 4},
						}
					}
				}
				close(ch)
				ctx.Info("[SERVER] Closed Uplink")
			}()
			return ch, cancel, nil
		}

		brkClient := NewBrokerClient(conn)
		uplink := NewMonitoredHandlerSubscribeStream(brkClient, func() context.Context {
			return context.Background()
		})

		ch := uplink.Channel()

		go func() {
			for uplink := range ch {
				ctx.WithField("Uplink", uplink).Info("[CLIENT] Received Uplink")
			}
			ctx.Info("[CLIENT] Closed Uplink")
		}()

		time.Sleep(10 * time.Millisecond)

		uplink.Close()

		time.Sleep(10 * time.Millisecond)
	}

}
	Use:   "register-prefix [prefix ...]",
	Short: "Register a prefix to this Broker",
	Long:  `ttn broker register prefix registers a prefix to this Broker`,
	Run: func(cmd *cobra.Command, args []string) {
		if len(args) == 0 {
			cmd.UsageFunc()(cmd)
			return
		}

		path := filepath.Clean(viper.GetString("key-dir") + "/ca.cert")
		cert, err := ioutil.ReadFile(path)
		if err == nil && !api.RootCAs.AppendCertsFromPEM(cert) {
			ctx.Warnf("Could not add root certificates from %s", path)
		}

		conn, err := api.Dial(viper.GetString("discovery-address"))
		if err != nil {
			ctx.WithError(err).Fatal("Could not connect to Discovery server")
		}
		client := discovery.NewDiscoveryClient(conn)

		client.GetAll(context.Background(), &discovery.GetServiceRequest{})

		md := metadata.Pairs(
			"service-name", "broker",
			"id", viper.GetString("id"),
			"token", viper.GetString("auth-token"),
		)
		dscContext := metadata.NewContext(context.Background(), md)

		success := true
func TestRouterCommunication(t *testing.T) {
	a := New(t)

	ctx := GetLogger(t, "TestRouterCommunication")
	log.Set(apex.Wrap(ctx))

	rtr := newTestRouter()
	rand.Seed(time.Now().UnixNano())
	port := rand.Intn(1000) + 10000
	go rtr.Serve(port)

	conn, _ := api.Dial(fmt.Sprintf("localhost:%d", port))

	{
		rtr.UplinkChanFunc = func(md metadata.MD) (chan *UplinkMessage, error) {
			ch := make(chan *UplinkMessage, 1)
			go func() {
				ctx.Info("[SERVER] Channel opened")
				for message := range ch {
					ctx.WithField("Message", message).Info("[SERVER] Received Uplink")
				}
				ctx.Info("[SERVER] Channel closed")
			}()
			return ch, nil
		}

		rtrClient := NewRouterClient(conn)
		gtwClient := NewRouterClientForGateway(rtrClient, "dev", "token")
		uplink := NewMonitoredUplinkStream(gtwClient)

		err := uplink.Send(&UplinkMessage{
			Payload: []byte{1, 2, 3, 4},
			ProtocolMetadata: &protocol.RxMetadata{Protocol: &protocol.RxMetadata_Lorawan{Lorawan: &lorawan.Metadata{
				Modulation: lorawan.Modulation_LORA,
				DataRate:   "SF7BW125",
				CodingRate: "4/7",
			}}},
			GatewayMetadata: &gateway.RxMetadata{
				GatewayId: "dev",
			},
		})

		a.So(err, ShouldBeNil)

		time.Sleep(10 * time.Millisecond)

		uplink.Close()

		time.Sleep(10 * time.Millisecond)

		gtwClient.Close()

		time.Sleep(10 * time.Millisecond)
	}

	{
		rtr.GatewayStatusChanFunc = func(md metadata.MD) (chan *gateway.Status, error) {
			ch := make(chan *gateway.Status, 1)
			go func() {
				ctx.Info("[SERVER] Channel opened")
				for message := range ch {
					ctx.WithField("Message", message).Info("[SERVER] Received GatewayStatus")
				}
				ctx.Info("[SERVER] Channel closed")
			}()
			return ch, nil
		}

		rtrClient := NewRouterClient(conn)
		gtwClient := NewRouterClientForGateway(rtrClient, "dev", "token")
		status := NewMonitoredGatewayStatusStream(gtwClient)

		err := status.Send(&gateway.Status{Time: time.Now().UnixNano()})

		a.So(err, ShouldBeNil)

		time.Sleep(10 * time.Millisecond)

		status.Close()

		time.Sleep(10 * time.Millisecond)

		gtwClient.Close()

		time.Sleep(10 * time.Millisecond)
	}

	{
		rtr.DownlinkChanFunc = func(md metadata.MD) (<-chan *DownlinkMessage, func(), error) {
			ch := make(chan *DownlinkMessage, 1)
			stop := make(chan struct{})
			cancel := func() {
				ctx.Info("[SERVER] Canceling downlink")
				close(stop)
			}
			go func() {
			loop:
				for {
					select {
					case <-stop:
						break loop
					case <-time.After(5 * time.Millisecond):
						ctx.Info("[SERVER] Sending Downlink")
						ch <- &DownlinkMessage{
							Payload: []byte{1, 2, 3, 4},
						}
					}
				}
				close(ch)
				ctx.Info("[SERVER] Closed Downlink")
			}()
			return ch, cancel, nil
		}

		rtrClient := NewRouterClient(conn)
		gtwClient := NewRouterClientForGateway(rtrClient, "dev", "token")
		downlink := NewMonitoredDownlinkStream(gtwClient)

		ch := downlink.Channel()

		go func() {
			for downlink := range ch {
				ctx.WithField("Downlink", downlink).Info("[CLIENT] Received Downlink")
			}
			ctx.Info("[CLIENT] Closed Downlink")
		}()

		time.Sleep(10 * time.Millisecond)

		downlink.Close()

		time.Sleep(10 * time.Millisecond)

		gtwClient.Close()

		time.Sleep(10 * time.Millisecond)
	}

}