1-slot bi-directional communication - how to deal with collisions?

I have one application. which consists of "Manager" and "Worker". Currently, the worker always initiates a connection, says something to the manager, and the manager sends a response.

Since there is a LOT connection between the Manager and the Worker, I am considering the possibility of opening a socket between them and communicating. I also hope to start interaction from both sides - letting the manager say something to the employee when he wants to.

However, I am a little confused as to how to deal with "collisions". Let's say the manager decides to say something to the employee, and at the same time the worker decides to tell the manager something. What will happen? How should this situation be handled?

PS I am planning on using Netty for the actual implementation.

+2


a source to share


5 answers


"I also hope to initiate interaction from both sides, which allows the manager to say something to the employee when he wants to."

Simple answer. Not.



Learn about existing protocols: client and server availability. Everything will work well. The worker can be the server, and the Manager can be the client. The manager can fulfill numerous requests. The employee responds to inquiries as they become available.

Peer-to-peer can be tricky with no real meaning to complexity.

+3


a source


I would go for a persistent bi-directional channel between server and client.

If you have one server and one client then there is no conflict problem ... If the server accepts the connection, it knows its client and vice versa. Both can read and write on the same socket.

Now if you have multiple clients and your server needs to send a request specifically to client X, you need handshaking !

When the client boots up, it connects to the server. Once this connection is established, the client identifies itself as client X (confirmation message). The server now knows that it has a socket open for client X, and every time it needs to send a message to client X, it reuses that socket.



Luckily, I just wrote a tutorial (including a sample project) on this exact problem. Using Netty! :)

Here's the link: http://bruno.linker45.eu/2010/07/15/handshaking-tutorial-with-netty/

Note that in this solution, the server does not try to connect to the client. It is always the client that connects to the server. If you've been thinking about opening a socket every time you wanted to send a message, you should reconsider persistent connections as they avoid the overhead of establishing a connection and hence speed up the data transfer rate N-fold.

+2


a source


I think you need to read on sockets ....

You don't really have problems like this ... Aside from how to react to both receive and send, this is usually done by streaming your messages ... depending on the application, you can take several approaches to this.

+1


a source


The correct link is to the Handshake / Netty manual mentioned in brunodecarvalho's answer, http://bruno.factor45.org/blag/2010/07/15/handshaking-tutorial-with-netty/
I would add this as a comment to his question. but I don't have the minimum required reputation to do this.

+1


a source


If you feel like reinventing the wheel and don't want to use middleware ...

Design your protocol so that another peer-to-peer response to your requests is always easily distinguishable from another peer's requests. Then choose your network I / O strategy carefully. Regardless of which code is responsible for reading from the socket, you first need to determine if the incoming data is a response to the data sent or if it is a new request from a peer (looking at the data header and issuing the request recently). In addition, you need to maintain proper queuing so that when you send replies to peer requests, it is properly segregated from the new requests you issue.

0


a source







All Articles