Communication between manager class and object class - c++

I'm facing a designissue. I'm using DDS (Data Distribution Service) protocol to communicate between application.
In my DDS Library I have 3 classes:
- DDSWriter to write messages to other applications
- DDSReader to read messages from other applications
- DDSManager to instantiate the two others, manage them and be the entry point for the application
DDSManager have an objet which refer to DDSWriter and another which refer to DDSReader.
DDSReader have a thread to read continually the data that we receive but no objet we refer to DDSManager.
How can I send data from DDSReader to DDSManager in this case ?
Thanks in advance for your help.

After discussion with an other software architect that what I need can't be implement.
But I can implement the design pattern Observer which permits to abstract DDSManager instance for DDSReader object.
Thanks for your help and I hope this can be help other person.

Related

Poco websocket server example

I want to create an application that consists of a web based front-end and a c++ back-end. My choice is to use websocket protocol in order to achieve data transfer between them.Specifically the front end will trigger some measurements that will be done in the back-end and eventually return and store the relevant values in the front-end. I decided for the websocket protocol implementation to use poco library and specifically I came across the following example https://github.com/pocoproject/poco/blob/develop/Net/samples/WebSocketServer/src/WebSocketServer.cpp. However since I haven't totally grasped the factory concept in C++ I haven't figured out the role of class RequestHandlerFactory.Is it possible for someone to explain what is the role of the aforementioned class and regarding my implementation (front-end -> trigger back-end -> back-end do measurements ->back-end returns the value to front-end in order to be depicted in a web-based gui) do I need to make any modifications to make this work for my case ?
As you might have read in the sample, there are two implementations derived from HTTPServer. So depending on the type of connection requested by the client (WebsocketRequest, PageRequest) one can return the appropriate HTTPServer. The work of factory class is to handle the incoming request, decide which class should handle it (depending on the connection requested).
Since you would be requesting to exchange data and not a request to display a HTML document, you should go for WebSocketRequestHandler. Yes it can be done. You might want to remove the PageRequestHandler since you wouldn't be using it.

Design strategy, class relationships

I have an application which has the following structure:
(Removed image, I don't have permission to post images. See here: http://s15.postimg.org/98qc3p7uz/Capture.png)
So 'App' holds Manager instance, Manager holds a vector of 'Connection', each of them holds a Protocol, and each Protocol holds a communication class. All the communication classes reference a single HAL class.
At the moment, the communication class constructor obtains a HAL instance as a singleton, eg. hal = HAL::GetInstance().
To make development and testing easier, I'd like to remove this dependency. Dependency Injection pattern seems ideal for this as it would allow me to use a fake HAL.
However, I don't think the Communication classes should have to do anything with HAL, likewise the protocol classes and the connections.
The main 'App' class DOES have a reference however, but I'm not sure about passing that to Manager->Connections[x]->Protocol->Communication.
Maybe I can abstract it out and let Manager have a reference to the HAL, and then each connection can have a parent pointer to the Manager, but it all seems to be getting messy and overcomplicated just for the sake of providing a HAL class reference to the communication.
Is there a better way to do this?
You could let the Manager create instances of Connection, Protocol, Communication and HAL. Then the Manager connects all of them. Very simple example in C++:
Manager::createEverything()
{
Connection connection;
Protocol protocol;
Communication communication;
HAL hal;
communication.setHal(hal);
protocol.setCommunication(communication);
connection.setProtocol(protocol);
}
You could also use dependency injection for creating different HAL-implementations that way. A class design could look like the following:
YUML diagram
YUML Edit Link
The above is quite simple but should give you an idea. Some more thoughts:
Use a Factory to create the different types of IHAL
Use the Builder Pattern to do the whole creation and aggregation thing

Register to the DeviceManager of Linux

I read some questions here but couldn't really find the specific problem I'am faced with here...
I need to implement a "DeviceCache" in a particular project which caches all device-names found in /proc/net/dev .
The Language is C/++
So I thought about a seperate thread looking every X seconds in the directory mentioned above but was encouraged to find a more direct way.
How can I register a method of my process to the device manager of linux?
Is there a similar way like events/signals?
I looked in other sites but couldn't find any helpful code... Im relatively new to Linux-programming but willing to learn new things :)
Based on your comments, what you really want is to track which network interfaces are operational at any given time.
The only true way to determine if a network interface is up is to test it - after all, the router on the other end may be down. You could send pings out periodically, for example.
However, if you just want to know if the media goes down (ie, the network cable is unplugged), take a look at these SO questions:
Linux carrier detection notification
Get notified about network interface change on Linux
If you just want to be notified of the actual hardware-level registration of interfaces (eg, when a USB NIC is plugged in), you can use udev events if your platform has udev; otherwise, I believe there's another netlink category for hardware addition/removal events.

TCPStream Class for multithreaded TCP server

I'm currently working on transitioning a small console application to a TCP server / client application. The client will connect to the server via any Telnet client, and the server will replicate the standard console interface for each Telnet connection.
I started looking into doing this using the techniques I've learned from Beej's guide to network programming -- accepting the connection and then using fork() to separate it into its own process.
However, I would prefer to maintain my use of streaming IO (the original console application uses cin / cout, using similar functions for the networking logic would make the conversion process much simpler).
I've discovered the TCPStream class, hiding within sockets.h (http://www.gnutelephony.org/doxy/bayonne2/a00215.html)
It appears this class will allow me to use the server with streaming IO. However, I can't find a single example of using this class, nor can I find an explanation as to how to use fork() with it.
Any ideas? Thanks in advance for any help.
I think you are confusing the trees for the forest. One socket class is such a small part of what you need to do overall that it is not worth focusing on that.
If your objective is just to get your project working then just use an existing framework rather than trying to pull individual classes out of a large project. POCO has a TCPServer class that will do 90% of the work for you. QT, ACE and others have similar classes. There is not a huge amount of documentation on POCO but they do cover TCPServer pretty well and you can learn a lot from reading the source code if that is really where your interest lies.

How to synchronize the same object on client and server side in client-server application? Is small messages framework good for this job?

I'm making a game engine in c++ and python. I'm using OGRE for 3D rendering, OpenAL for sound, ODE for physics, OIS for input, HawkNL for networking and boost.python for embedded python interpreter. Every subsystem (library) is wrapped by a class - manager and every manager is singleton. Now, I have a class - Object - this could be every visible object in game world. This is some kind of mix, object has graphics representation (entity) and representation in physics simulator (solid). These two are most important here. Class Object is just pure abstract base class - interface. I've decided to implement Object on client side as ObjectClientSide - this implementation has the entity, and on server side as ObjectServerSide - this implementation has the solid. Physics simulator runs only on server, and rendering is done only on client of course. The object can exist only when both implementations are working together. Every object has unique id and both instances of the same object on client and server side have the same id.
So, after this short background, my first question is: is this design good? How can I make it better? And the main question: how should I synchronize these objects?
Next, both implementations have the same interface, but part of it is implemented on server side and part on client side. So, for example, if player wants to move forward his character he should send a request to object on server. The server then makes changes to simulation and sends updated position to client. For that reason, I've created small messages framework. There is a Message class, Router, Rule and rules inherited from Rule. When message arrives, Router checks it against the rules and sends it to destination. So, when I call myObjectInstanceOnClientSide->setPosition(x,y,z) this object creates Message, its content is function and parameters and destination is object with the same id on server. When object with the same id on server side gets this message it calls this function with given arguments. So, when a function can't be implemented on one side it creates a message and sends it to object on the other side. I think this can be very useful in scripts. Scripts can be very clean, if script needs for example turn on animation on clients, I only need to call this function on server's local object - the rest is in background.
So, is this ok? Am I wrong about this? It this common solution?
It sounds like you would be sending a lot of tiny messages. The UDP and IP headers will add 28 bytes of overhead (20 bytes for the IPv4 header or 40 for IPv6 plus 8 bytes for the UDP header). So, I would suggest combining multiple messages to be dispatched together at a perioidic rate.
You may also want to read these other questions and answers:
Dealing with Latency in Networked Games
Real-time multiplayer game (concept question)
I added a bunch of useful links to the DevMaster.net Wiki years ago that are still relavent:
Networking for Games 101 FAQ
Multiplayer and Network Programming
Introduction to Multiplayer Game
Programming The Quake3 Networking Model
Unreal Networking Architecture
Networked Physics
Beej's Network Guide
I'd suggest starting to read Glenn Fiedler's blog. He's done some incredibly work with networked physics including the recent Mercenaries 2 release. He started a series of articles called Networking for Game Programmers.