I have an app which acts like a TCP server and accepts multiple connections. Each connection is made by a distinct device, with a unique ID (the ID is reported by the device in two different messages according to its protocol, but that's less important).
Right now, whenever a connection is established, my app creates a new list of parsers for a bunch of messages the device can send, but at that point it doesn't know yet which device has been connected. The problem is that several messages can be parsed before device's identity is established. Since devices can disconnect and reconnect during app's lifetime, there are long lived objects (device statistics and various other data) that should not be discarded whenever a device disconnects, and therefore need to be reattached as the consumers of the parser pipeline once the device ID is established. Alternatively, if a device was never connected before, its fresh instances will become long lived.
Does anyone have an overall idea of how this application should be rearranged? For example, one idea was to wrap the long lived objects into proxies, and then have the possibility to change the underlying implementation with a different instance, once a new device is connected. To make it clearer, these objects are not public singletons (multitons), but there is obviously a dictionary (by ID) somewhere in the business layer to keep track of per-device data.
This all feels unnecessarily complex, but the code (like it always does) obviously evolved over time and now I feel I can't see the forest through the trees.