Exemplo n.º 1
0
func (a *AllJoynBridge) processSignals() {

	// work on currently reveived signals only
	// TODO:https://golang.org/doc/effective_go.html#leaky_buffer
	signals := a.signals
	a.signals = make(chan *dbus.Signal, 100)
	close(signals)

	// log.Printf("Processing signals: %d", len(signals))

	for signal := range signals {
		log.Printf("**** Incoming Signal: %+v", signal)

		// get signal signature
		msgId, _ := a.findSignal(signal)

		if msgId == 0 {
			log.Printf("Could not find any matching service for signal: %+v", signal)
			continue
		}

		for _, sessionId := range a.sessions {

			var status C.AJ_Status = C.AJ_OK
			msg := C.Get_AJ_Message()

			status = C.AJ_MarshalSignal_cgo((*C.AJ_Message)(msg), C.uint32_t(msgId), C.uint32_t(sessionId), C.uint8_t(0), C.uint32_t(0))
			log.Printf("**** AJ_MarshalSignal: %d", status)

			// for _, arg := range signal.Body {
			// 	log.Printf("**** ARG: %v", arg)
			// 	signature := dbus.SignatureOf(arg).String()
			// 	sig := C.CString(signature)
			// 	defer C.free(unsafe.Pointer(sig))
			// 	status = C.MarshalArg((*C.AJ_Message)(msg), sig, unsafe.Pointer(&arg))
			// 	log.Printf("**** MarshalArg: (%s, %v) => %d", signature, arg, status)
			// }

			if len(signal.Body) > 0 {

				buf := new(bytes.Buffer)
				buf.Write(make([]byte, (int)(msg.bodyBytes)))
				enc := devicehivealljoyn.NewEncoderAtOffset(buf, (int)(msg.bodyBytes), binary.LittleEndian)
				pad, err := enc.Encode(signal.Body...)

				if err != nil {
					log.Printf("Error encoding result: %s", err)
					continue
				}

				newBuf := buf.Bytes()[(int)(msg.bodyBytes)+pad:]

				status = C.AJ_DeliverMsgPartial((*C.AJ_Message)(msg), C.uint32_t(len(newBuf)))
				log.Printf("**** AJ_DeliverMsgPartial: %d", status)

				if len(newBuf) > 0 {
					status = C.AJ_MarshalRaw((*C.AJ_Message)(msg), unsafe.Pointer(&newBuf[0]), C.size_t(len(newBuf)))
					log.Printf("**** AJ_MarshalRaw: %d", status)
				} else {
					status = C.AJ_MarshalRaw((*C.AJ_Message)(msg), unsafe.Pointer(&newBuf), C.size_t(0))
					log.Printf("**** AJ_MarshalRaw: %d", status)
				}

			}

			status = C.AJ_DeliverMsg((*C.AJ_Message)(msg))
			log.Printf("**** AJ_DeliverMsg: %d", status)

			status = C.AJ_CloseMsg((*C.AJ_Message)(msg))
			log.Printf("**** AJ_CloseMsg: %d", status)
		}

	}
}
Exemplo n.º 2
0
func (a *AllJoynBridge) processSignals() {

	// work on currently reveived signals only
	// TODO:https://golang.org/doc/effective_go.html#leaky_buffer
	signals := a.signals
	a.signals = make(chan *dbus.Signal, 100)
	close(signals)

	// log.Printf("Processing signals: %d", len(signals))

	for signal := range signals {
		log.Printf("**** Incoming Signal: %+v", signal)

		if a.processNotificationSignal(signal) {
			continue
		}

		// get signal signature
		sigIntrospect, msgId := a.findSignal(signal)

		if msgId == 0 {
			log.Printf("Could not find any matching service for signal: %+v", signal)
			continue
		}
		log.Printf("%v", sigIntrospect)
		if isSessionless(sigIntrospect) {
			log.Printf("SESSIONLESS SIGNAL!")

			var status C.AJ_Status = C.AJ_OK
			msg := C.Get_AJ_Message()

			status = C.AJ_MarshalSignal_cgo(msg, C.uint32_t(msgId), C.uint32_t(0), C.AJ_FLAG_SESSIONLESS, C.uint32_t(0))
			log.Printf("**** AJ_MarshalSignal: %s", status)

			if len(signal.Body) > 0 {

				buf := new(bytes.Buffer)
				buf.Write(make([]byte, (int)(msg.bodyBytes)))
				enc := ajmarshal.NewEncoderAtOffset(buf, (int)(msg.bodyBytes), binary.LittleEndian)
				pad, err := enc.Encode(signal.Body...)

				if err != nil {
					log.Printf("Error encoding result: %s", err)
					continue
				}

				newBuf := buf.Bytes()[(int)(msg.bodyBytes)+pad:]

				if len(newBuf) > 0 {
					status = C.AJ_DeliverMsgPartial((*C.AJ_Message)(msg), C.uint32_t(len(newBuf)))
					log.Printf("**** AJ_DeliverMsgPartial: %s", status)
					status = C.AJ_MarshalRaw((*C.AJ_Message)(msg), unsafe.Pointer(&newBuf[0]), C.size_t(len(newBuf)))
				} else {
					status = C.AJ_MarshalRaw((*C.AJ_Message)(msg), unsafe.Pointer(&newBuf), C.size_t(0))
				}
				log.Printf("**** AJ_MarshalRaw: %s", status)

			}

			status = C.AJ_DeliverMsg((*C.AJ_Message)(msg))
			log.Printf("**** AJ_DeliverMsg: %s", status)

			status = C.AJ_CloseMsg((*C.AJ_Message)(msg))
			log.Printf("**** AJ_CloseMsg: %s", status)

		} else {

			for _, sessionId := range a.sessions {

				var status C.AJ_Status = C.AJ_OK
				msg := C.Get_AJ_Message()

				status = C.AJ_MarshalSignal_cgo(msg, C.uint32_t(msgId), C.uint32_t(sessionId), C.uint8_t(0), C.uint32_t(0))
				log.Printf("**** AJ_MarshalSignal: %s", status)

				if len(signal.Body) > 0 {

					buf := new(bytes.Buffer)
					buf.Write(make([]byte, (int)(msg.bodyBytes)))
					enc := ajmarshal.NewEncoderAtOffset(buf, (int)(msg.bodyBytes), binary.LittleEndian)
					pad, err := enc.Encode(signal.Body...)

					if err != nil {
						log.Printf("Error encoding result: %s", err)
						continue
					}

					newBuf := buf.Bytes()[(int)(msg.bodyBytes)+pad:]

					if len(newBuf) > 0 {
						status = C.AJ_DeliverMsgPartial((*C.AJ_Message)(msg), C.uint32_t(len(newBuf)))
						log.Printf("**** AJ_DeliverMsgPartial: %s", status)
						status = C.AJ_MarshalRaw((*C.AJ_Message)(msg), unsafe.Pointer(&newBuf[0]), C.size_t(len(newBuf)))
					} else {
						status = C.AJ_MarshalRaw((*C.AJ_Message)(msg), unsafe.Pointer(&newBuf), C.size_t(0))
					}
					log.Printf("**** AJ_MarshalRaw: %s", status)

				}

				status = C.AJ_DeliverMsg((*C.AJ_Message)(msg))
				log.Printf("**** AJ_DeliverMsg: %s", status)

				status = C.AJ_CloseMsg((*C.AJ_Message)(msg))
				log.Printf("**** AJ_CloseMsg: %s", status)
			}
		}
	}
}