Two observable watching each other? Or something different from that?

Asked

Viewed 49 times

1

Consider a class ServidorTcp (I don’t know if this is the right abstraction; it would have two operations, iniciar() and parar()).

Inside there’s a field socketServer whose type is SocketServer (a server inside a server, but I don’t necessarily consider it wrong because in object orientation it sometimes happens, an abstraction encapsulating something more concrete).

Inside her would have spoken socketServer, a field int porta, and a type object to be defined that holds the set of objects Socket created and opened by SocketServer.

The idea is that ServidorTcp receive the order of parar(), it shoots to all open sockets one socket.close() (otherwise they don’t close themselves until after a long time).

We have here two objects that generate events and can therefore be observed. The server, which can be stopped and the sockets are interested in watching to react to this halt, and the sockets themselves, which can launch a "close" event, in which case they need to leave the list of open sockets that the server can order to close if it is stopped.

So we have a situation where the server looks at the sockets and the sockets look at the server.

Complicated, no?

How would that be "done right"? For some right between easy-to-understand, easy-to-maintain, well-modeled code, or more of an alternative? Or is it hard to tell with just this information?

In terms of design should be two observable if observing? But can it Arnaldo?

P.S.: I just realized that the situation has nothing observer and observable, I misuse the pattern. The intention of the pattern is for the observer to pull (pull) the observable state, and here apparently there is nothing observable state involved.

  • 1

    No one needs to observe anyone, in the stop method you iterate the sockets and close one by one.

  • You know you’re right! It was already overengineering. :) I just wanted to also remove the already closed sockets from the collection as the connections end to avoid memory Leak until the time to iterate the sockets.

  • "Simply impose a read timeout and close every socket you timeout. This is a normal part of any server. You do not need to collect the sockets or take any special measure for them to disconnect." https://stackoverflow.com/a/40575031/2241463

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.