Boost logo

Boost-Commit :

From: mconsoni_at_[hidden]
Date: 2007-06-05 21:00:40


Author: mconsoni
Date: 2007-06-05 21:00:39 EDT (Tue, 05 Jun 2007)
New Revision: 4463
URL: http://svn.boost.org/trac/boost/changeset/4463

Log:
This is the new example that specifically shows the use of Info class with a base class
and concrete implementations. I've added a new directory under info, because it's an
example of Info class, but with its own subdirectory (im).

The example is about Instant Messengers... We have a protocol interface and then some
plugins that implements the generic protocol with the specific operations for each
concrete network (MSN, Jabber). The Info class is used for network parameters. There
is a base_class that define the available parameters for a generic protocol, and
then there are the different concrete implementations for the plugin. In short,
there is for example an http_method that it's only available for MSN.. then in main
we could check that parameter.

Of course all is just a test.. there are no real implementations..! :)

- Jamfile.v2: added the new library and sources.

- info/im: main subdirectory for the example

        . protocol.hpp: generic protocol interface (send, receive, login).
        . plugins.cpp: concrete implementations of the interface for MSN
                       and Jabber protocol, and factory map.
        . network_parameters.hpp : base class and concrete implementations for
                                         the network parameters, for each protocol.
        . im_main.cpp: main function, it loads the library and make some operations
                       with the protocol.

Added:
   sandbox/libs/extension/examples/info/im/
   sandbox/libs/extension/examples/info/im/im_main.cpp
   sandbox/libs/extension/examples/info/im/network_parameters.hpp
   sandbox/libs/extension/examples/info/im/plugins.cpp
   sandbox/libs/extension/examples/info/im/protocol.hpp
Text files modified:
   sandbox/libs/extension/examples/Jamfile.v2 | 4 ++++
   1 files changed, 4 insertions(+), 0 deletions(-)

Modified: sandbox/libs/extension/examples/Jamfile.v2
==============================================================================
--- sandbox/libs/extension/examples/Jamfile.v2 (original)
+++ sandbox/libs/extension/examples/Jamfile.v2 2007-06-05 21:00:39 EDT (Tue, 05 Jun 2007)
@@ -19,6 +19,9 @@
 exe MultilanguageWord : info/multilanguage_main.cpp ;
 lib MultilanguageHelloWorld : info/multilanguage_hello_world.cpp : <link>shared ;
 
+exe IM : info/im/im_main.cpp ;
+lib IMPlugins : info/im/plugins.cpp : <link>shared ;
+
 lib Vehicle : multiple_inheritance/vehicle.cpp : <link>shared ;
 lib Car : multiple_inheritance/car.cpp Vehicle : <link>shared ;
 lib Plane : multiple_inheritance/plane.cpp Vehicle : <link>shared ;
@@ -40,6 +43,7 @@
 install ../bin :
   HelloWorld HelloWorldLib
   MultilanguageWord MultilanguageHelloWorld
+ IM IMPlugins
   Vehicle Boat FlyingCar Car Plane
   CarOfTheFuture MultipleInheritance RegistryLibrary Computer
   RegistryExample

Added: sandbox/libs/extension/examples/info/im/im_main.cpp
==============================================================================
--- (empty file)
+++ sandbox/libs/extension/examples/info/im/im_main.cpp 2007-06-05 21:00:39 EDT (Tue, 05 Jun 2007)
@@ -0,0 +1,83 @@
+/* (C) Copyright Mariano G. Consoni 2007
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ */
+
+#include <boost/extension/factory_map.hpp>
+#include <boost/extension/shared_library.hpp>
+#include <boost/extension/convenience.hpp>
+#include <boost/shared_ptr.hpp>
+
+#include <iostream>
+
+#include "protocol.hpp"
+#include "network_parameters.hpp"
+
+
+int main(void)
+{
+ using namespace boost::extensions;
+
+ // create the factory_map object - it will hold all of the available
+ // constructors. Multiple factory_maps can be constructed.
+ factory_map fm;
+
+ // load the shared library with
+ load_single_library(fm, "libIMPlugins.extension", "extension_export_plugins");
+
+ // get a reference to the list of constructors for protocols
+ std::list< factory<protocol, boost::shared_ptr<network_parameters> > > & factory_list =
+ fm.get<protocol, boost::shared_ptr<network_parameters> >();
+
+ if (factory_list.size() < 2) {
+ std::cout << "Error - the classes were not found.";
+ return 1;
+ }
+
+ std::list<factory< protocol, boost::shared_ptr<network_parameters> > >::iterator current_plugin = factory_list.begin();
+
+ // MSN plugin
+ std::auto_ptr<protocol> MSN_ptr(current_plugin->create());
+ boost::shared_ptr<network_parameters> msn_parameters = current_plugin->get_info();
+ current_plugin++;
+
+ // Jabber plugin
+ std::auto_ptr<protocol> Jabber_ptr(current_plugin->create());
+ boost::shared_ptr<network_parameters> jabber_parameters = current_plugin->get_info();
+
+ // server
+ std::cout << "MSN hostname: " << msn_parameters->hostname() << std::endl;
+ std::cout << "Jabber hostname: " << jabber_parameters->hostname() << std::endl;
+ std::cout << std::endl;
+
+ // http_mode: note that one of the implementations doesn't support it, having a base class
+ // and different specific concrete network parameters allow us to handle this
+ std::cout << "MSN: ";
+ msn_parameters->set_http_mode();
+ std::cout << "Jabber: ";
+ jabber_parameters->set_http_mode();
+ std::cout << std::endl;
+
+ // login
+ MSN_ptr->login("testuser", "testpass");
+ Jabber_ptr->login("testuser", "testpass");
+ std::cout << std::endl;
+
+ // send message
+ MSN_ptr->send("hi");
+ Jabber_ptr->send("hi");
+ std::cout << std::endl;
+
+ // change status
+ MSN_ptr->change_status("away");
+ Jabber_ptr->change_status("away");
+ std::cout << std::endl;
+
+ // wait for message
+ std::cout << MSN_ptr->receive() << std::endl;
+ std::cout << Jabber_ptr->receive() << std::endl;
+ std::cout << std::endl;
+
+ return 0;
+}

Added: sandbox/libs/extension/examples/info/im/network_parameters.hpp
==============================================================================
--- (empty file)
+++ sandbox/libs/extension/examples/info/im/network_parameters.hpp 2007-06-05 21:00:39 EDT (Tue, 05 Jun 2007)
@@ -0,0 +1,46 @@
+/* (C) Copyright Mariano G. Consoni 2007
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ */
+
+#include <string>
+#include <iostream>
+
+// interface for the parameters of each plugin
+class network_parameters
+{
+ public:
+ virtual std::string hostname(void) = 0;
+ virtual std::string port(void) = 0;
+ virtual void set_http_mode(void)= 0;
+
+ virtual ~network_parameters(void) {};
+};
+
+
+// MSN implementation
+class MSN_network_parameters : public network_parameters
+{
+ public:
+ virtual std::string hostname(void) { return "msn.messenger.com"; }
+ virtual std::string port(void) { return "1863"; }
+
+ virtual void set_http_mode(void) { std::cout << "http mode set" << std::endl; }
+
+ virtual ~MSN_network_parameters() {}
+};
+
+
+
+// Jabber implementation
+class Jabber_network_parameters : public network_parameters
+{
+ public:
+ virtual std::string hostname(void) { return "jabber.org"; }
+ virtual std::string port(void) { return "7063"; }
+
+ virtual void set_http_mode(void) { std::cout << "http mode not supported" << std::endl; }
+
+ virtual ~Jabber_network_parameters() {}
+};

Added: sandbox/libs/extension/examples/info/im/plugins.cpp
==============================================================================
--- (empty file)
+++ sandbox/libs/extension/examples/info/im/plugins.cpp 2007-06-05 21:00:39 EDT (Tue, 05 Jun 2007)
@@ -0,0 +1,48 @@
+/* (C) Copyright Mariano G. Consoni 2007
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ */
+
+
+#include <boost/shared_ptr.hpp>
+#include <boost/extension/factory_map.hpp>
+
+#include "protocol.hpp"
+#include "network_parameters.hpp"
+
+#include <iostream>
+
+// MSN protocol implementation
+class MSN : public protocol
+{
+public:
+ virtual void login(const std::string &user, const std::string &pass) { std::cout << "MSN: Logged In" << std::endl; }
+ virtual void send(const std::string &msg) { std::cout << "MSN: message [" << msg << "] sent" << std::endl; }
+ virtual std::string receive(void) { return std::string("MSN: hello! msg received"); }
+ virtual void change_status(const std::string &new_status) { std::cout << "MSN: Status changed to [" << new_status << "]" << std::endl; }
+
+ virtual ~MSN(void) {}
+};
+
+// Jabber protocol implementation
+class Jabber : public protocol
+{
+public:
+ virtual void login(const std::string &user, const std::string &pass) { std::cout << "Jabber: Logged In" << std::endl; }
+ virtual void send(const std::string &msg) { std::cout << "Jabber: message [" << msg << "] sent" << std::endl; }
+ virtual std::string receive(void) { return std::string("Jabber: hello! msg received"); }
+ virtual void change_status(const std::string &new_status) { std::cout << "Jabber: Status changed to [" << new_status << "]" << std::endl; }
+
+ virtual ~Jabber(void) {}
+};
+
+
+
+extern "C" void BOOST_EXTENSION_EXPORT_DECL extension_export_plugins(boost::extensions::factory_map & fm)
+{
+ fm.add< MSN, protocol, boost::shared_ptr<network_parameters> >
+ (boost::shared_ptr<network_parameters>(new MSN_network_parameters));
+ fm.add< Jabber, protocol, boost::shared_ptr<network_parameters> >
+ (boost::shared_ptr<network_parameters>(new Jabber_network_parameters));
+}

Added: sandbox/libs/extension/examples/info/im/protocol.hpp
==============================================================================
--- (empty file)
+++ sandbox/libs/extension/examples/info/im/protocol.hpp 2007-06-05 21:00:39 EDT (Tue, 05 Jun 2007)
@@ -0,0 +1,20 @@
+/* (C) Copyright Mariano G. Consoni 2007
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ */
+
+#include <string>
+
+// protocol for instant message communication
+class protocol
+{
+ public:
+
+ virtual void login(const std::string &user, const std::string &pass) {}
+ virtual void send(const std::string &msg) {}
+ virtual std::string receive(void) { return std::string(""); }
+ virtual void change_status(const std::string &new_status) {}
+
+ virtual ~protocol(void) {}
+};


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk