#include <string.hpp> // std::string
#include <string_utils.hpp> // std::string frmstring(const char* fmt, ... );
#include <event.hpp> // std::event
#include <iostream.hpp> // std::Console
// system include
#include <conio.h> // _getch();

class Server;
class Client;

typedef std::event<Server, std::string> ServerEvent;
typedef std::delegate<Client, Server, std::string> ClientDelegate;

class Server
{
public:
    ServerEvent MessageSent;

    void SendMessage(std::delegateparam<std::string>* p_msg)
    {
        std::Console::writeline("Server send message: ");
        MessageSent(this, p_msg);
    }
};

class Client
{
    void MessageSentHandler(const Server* p_sender, std::delegateparam<std::string>* p)
    {
        static int iindex; iindex++;
        
        std::Console::writeline(std::frmstring("Client%d reach messages: ", iindex));
	for(auto i = p->m_data.begin(); i != p->m_data.end(); i++) {
            std::Console::writeline(std::frmstring(" %s", (*i).c_str()));
	}
        std::Console::writeline("-------------------------------");
    }
public:
    Client(Server* p_server)
    {
        p_server->MessageSent += new ClientDelegate(this, &Client::MessageSentHandler);
    }
}; 


int main(int argc, char** argv) {
    Server server;
    Client client1(&server), client2(&server), client3(&server);

    std::delegateparam<std::string> param;
    param += ( std::string("Hallo Welt"));
    param += ( std::string("Wie geht's?"));
    param += ( std::string("Ach ganz gut"));

    server.SendMessage(&param);

    std::Console::writeline(std::string("Bitte druecken Sie eine Taste ..."));
      _getch();
    return 0;
}

Output:
Skizze (1).png

Last edited Nov 28, 2016 at 10:26 PM by pbl1h0sc, version 3