mardi 17 décembre 2019

Configuring a socket connection inside a library

I'm currently developing a library for one of my programs. Internally the library itself connects via a socket to a server. Currently, socket settings like the target host and port are configured via an IConnection, which is one of two classes that I want to offer users of the lib:

public interface IConnection {
   public void connect(String hostname, int port); //lib connects to a server internally
   public void disconnect();
}

Once a connection to a server via IConnection is established, requests can be fired via the IRequestor, the second interface of the library:

public interface IRequestor {
    public String processRequest(String request);
}

I don't see big drawbacks with this design, however, two things bug me:

  1. The library assumes that the user has knowledge about a happens-before relationship, which means that the user first has to establish a connection via IConnection in order to use the main functionality of the library, which is firing requests via IRequestor. Is that acceptable as long as I provide a through documentation or are there better design solutions, which automatically enforce a happens-before relationship?

  2. The socket is used all over the library as a field. When the user is disconnecting and reconnecting to a different server the state of a lot of classes is changed (they now request against a different server). So, suddently the classes request against a different server and it's hard to reason about who, where and when the "socket connection" was changed (since the socket sits as field and is "exchanged" via memory, rather than method passing). Of course, I could always specify the server via the IRequestor, but this doesn't seem right either, e.g.

    processRequest(String request, String hostname, int port)
    

Aucun commentaire:

Enregistrer un commentaire