Example #1
0
func startBridge() (map[string]reporter.StatusReporter, error) {
	appReporter := reporter.New()

	commands := make(chan bridge.Command)
	events := make(chan iotf.Event)

	deviceType := "LRSC"
	iotfManager, err := iotf.NewIoTFManager(os.Getenv("VCAP_SERVICES"), commands, events, deviceType)
	if err != nil {
		appReporter.Report("IoTF Manager:", err.Error())
		return nil, err
	}

	if err := setupLrscClient(); err != nil {
		return nil, err
	}

	reporters := make(map[string]reporter.StatusReporter)
	reporters["app"] = appReporter
	reporters["lrsc"] = lrscClient.StatusReporter
	reporters["iotf"] = iotfManager.StatusReporter()

	go runConnectionLoop("LRSC client", &lrscClient)
	go runConnectionLoop("IoTF client", iotfManager)

	go func() {
		for command := range commands {
			lrscClient.sendCommand(command)
			logger.Debug("Received command message: %v", command)
		}
	}()

	go func() {
		for {
			message := <-lrscClient.inbound
			event := iotf.Event{Device: message.DeviceGuid, Payload: message.Payload}
			events <- event
		}
	}()

	return reporters, nil
}
Example #2
0
func setupLrscClient() error {
	lrscClient.StatusReporter = reporter.New()
	dialerConfig := dialerConfig{
		host: os.Getenv("LRSC_HOST"),
		port: os.Getenv("LRSC_PORT"),
		cert: os.Getenv("LRSC_CLIENT_CERT"),
		key:  os.Getenv("LRSC_CLIENT_KEY"),
	}
	dialer, err := createTlsDialer(dialerConfig, &lrscClient.StatusReporter)
	if err != nil {
		logger.Error("failed to create dialer: %v", err)
		lrscClient.Report("CONNECTION", err.Error())
		return err
	}

	lrscClient.dialer = dialer
	lrscClient.err = make(chan error)
	lrscClient.inbound = make(chan lrscMessage, 100)

	return nil
}
					count += 1
					return "", errors.New("EOF")
				} else {
					return "{}\n", nil
				}
			},
			writeFunc: func(message string) error {
				if message == "JSON_000" {
					connectionAttempts += 1
				}
				return nil
			}}

		testDialer := &testDialer{conn: mockConn}
		lrscClient := &lrscConnection{dialer: testDialer}
		lrscClient.StatusReporter = reporter.New()
		lrscClient.inbound = make(chan lrscMessage)
		go runConnectionLoop("LRSC Client", lrscClient)

		messages := lrscClient.inbound
		<-messages
		Expect(connectionAttempts).To(Equal(2))
	})

	It("can receive a message", func() {
		mockConn := &mockConnection{
			readFunc: func() (string, error) {
				return `{"deveui": "id", "pdu": "data"}` + "\n", nil
			},
			writeFunc: func(string) error {
				return nil
Example #4
0
func newIoTFBroker(credentials *Credentials, commands chan<- bridge.Command, errChan chan<- error, deviceType string, clientFactory clientFactory) *iotfBroker {
	reporter := reporter.New()
	return &iotfBroker{commands: commands, StatusReporter: reporter, deviceType: deviceType, clientFactory: clientFactory, clientId: uuid.New()}
}