Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Avoiding circular dependency with signals/slots
Forum Updated to NodeBB v4.3 + New Features

Avoiding circular dependency with signals/slots

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 3 Posters 900 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    ECEC
    wrote on last edited by
    #1

    My application is made up of multiple, dynamically created "Node" widgets. In addition, there is a single "ProtocolManager" object, a pointer to which is stored in each "Node" object. The "ProtocolManager" object handles the creation, management, and processing of a TCP socket which communicates with a target system. "Node" objects call parameterized "ProtocolManager" methods to send commands to the target, the idea being that all communication happens via a single socket.

    This all works fine, although I now need to process responses from the target system and return the received data to the correct "Node" object which is identified by a unique ID. I could make a forward declaration inside "ProtocolManager" to "Node", pass a list of "Node" object pointers, and then transfer the data to the correct object. However, I'd like to avoid circular dependencies if possible.

    Alternatively, I could connect all "Nodes" to a signal which passes the data and the "Node" ID. Each object could then decide whether the data is relevant and ignore if not. With potentially dozens of "Nodes", this could be particularly inefficient.

    Is there perhaps a way to dynamically create a signal and connect it to the slot of one, and only one "Node", so data is only sent to the object for which it was intended?

    Any other ideas to get around this problem would be great! Thanks

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      You could take a look at QNetworkAccessManager, your architecture description makes it look like a usefully inspiration.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      E 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        You could take a look at QNetworkAccessManager, your architecture description makes it look like a usefully inspiration.

        E Offline
        E Offline
        ECEC
        wrote on last edited by
        #3

        @SGaist Thanks for your response. Could you elaborate please? I'm not sure how QNetworkAccessManager might solve my problem.

        SGaistS 1 Reply Last reply
        0
        • JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on last edited by JoeCFD
          #4

          Two ways:
          1, same signals are sent to all nodes. These signals, however, have embedded Node id info in them. Parse them and ignore the signals which are not related. Simple protocol thing.
          2. send different signals to different nodes.
          I will go with the first one.

          E 1 Reply Last reply
          0
          • JoeCFDJ JoeCFD

            Two ways:
            1, same signals are sent to all nodes. These signals, however, have embedded Node id info in them. Parse them and ignore the signals which are not related. Simple protocol thing.
            2. send different signals to different nodes.
            I will go with the first one.

            E Offline
            E Offline
            ECEC
            wrote on last edited by
            #5

            @JoeCFD 1. is what I proposed in the question, but worried that it would be inefficient.
            Not sure how 2. would work as the "Nodes" are generated dynamically, unless there's also a way to dynamically create signals? Thanks

            JoeCFDJ 1 Reply Last reply
            0
            • E ECEC

              @JoeCFD 1. is what I proposed in the question, but worried that it would be inefficient.
              Not sure how 2. would work as the "Nodes" are generated dynamically, unless there's also a way to dynamically create signals? Thanks

              JoeCFDJ Offline
              JoeCFDJ Offline
              JoeCFD
              wrote on last edited by
              #6

              @ECEC Parsing is very fast in GUI design. No worry about the efficiency. It is good enough to do it with unique ids or object names.

              1 Reply Last reply
              0
              • E ECEC

                @SGaist Thanks for your response. Could you elaborate please? I'm not sure how QNetworkAccessManager might solve my problem.

                SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @ECEC said in Avoiding circular dependency with signals/slots:

                @SGaist Thanks for your response. Could you elaborate please? I'm not sure how QNetworkAccessManager might solve my problem.

                I meant from an architecture point of view.

                With QNAM you can either connect your QNAM directly and manage things with it concerning the replies or you use the QNetworkReply returned by the operation you want to execute and use its signals and slots.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                E 1 Reply Last reply
                1
                • SGaistS SGaist

                  @ECEC said in Avoiding circular dependency with signals/slots:

                  @SGaist Thanks for your response. Could you elaborate please? I'm not sure how QNetworkAccessManager might solve my problem.

                  I meant from an architecture point of view.

                  With QNAM you can either connect your QNAM directly and manage things with it concerning the replies or you use the QNetworkReply returned by the operation you want to execute and use its signals and slots.

                  E Offline
                  E Offline
                  ECEC
                  wrote on last edited by
                  #8

                  @SGaist Could you please illustrate with an example in the context of my application? Not sure I'm understanding how implementing something similar would work in practice.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Your ProtocolManager could return something similar to a reply that could then be connected by the Node to a lambda or a slot to do further processing of the answer.

                    Therefor, your ProtocolManager would not have to care about the Node objects.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    E 1 Reply Last reply
                    1
                    • SGaistS SGaist

                      Your ProtocolManager could return something similar to a reply that could then be connected by the Node to a lambda or a slot to do further processing of the answer.

                      Therefor, your ProtocolManager would not have to care about the Node objects.

                      E Offline
                      E Offline
                      ECEC
                      wrote on last edited by
                      #10

                      @SGaist Ahhh, I think I see what you mean. So when the Nodes make a command request to the ProtocolManager, it returns a pointer to some sort of Reply object which the ProtocolManager populates when data for that Node is available. The Reply object then emits a signal which connects to a slot in Node. As requests are made frequently (10/sec) and the responses are short, I wonder whether it might make sense to have just one Reply object per Node instead of generating a new one per command request. What do you think, and is the above what you had in mind? Cheers

                      SGaistS 1 Reply Last reply
                      0
                      • E ECEC

                        @SGaist Ahhh, I think I see what you mean. So when the Nodes make a command request to the ProtocolManager, it returns a pointer to some sort of Reply object which the ProtocolManager populates when data for that Node is available. The Reply object then emits a signal which connects to a slot in Node. As requests are made frequently (10/sec) and the responses are short, I wonder whether it might make sense to have just one Reply object per Node instead of generating a new one per command request. What do you think, and is the above what you had in mind? Cheers

                        SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        Yes that's that.

                        I would first get it to work and then benchmark the simple implementation. If you see performance issue then start optimizing.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        E 1 Reply Last reply
                        0
                        • SGaistS SGaist

                          Yes that's that.

                          I would first get it to work and then benchmark the simple implementation. If you see performance issue then start optimizing.

                          E Offline
                          E Offline
                          ECEC
                          wrote on last edited by
                          #12

                          @SGaist As the responses are so short, is there a risk that data is received and the Reply signal is emitted before the Reply object has been returned to the Node and connected? Is there a way to prevent this from happening?

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            If you queue the request, it should not.

                            As already suggested, you should take a look at the implementation of QNAM. You make a request, get a reply object and there's no "speed" issue with it.

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved