"Fossies" - the Fresh Open Source Software Archive

Member "muscle/html/muscle-by-example/examples/reflector/example_3_annotated_dumb_server.cpp" (8 Jun 2019, 7190 Bytes) of package /linux/privat/muscle7.30.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #include "reflector/DumbReflectSession.h"
    2 #include "reflector/ReflectServer.h"
    3 #include "system/SetupSystem.h"  // for CompleteSetupSystem
    4 #include "syslog/SysLog.h"       // for SetConsoleLogLevel()
    5 #include "util/String.h"
    6 
    7 using namespace muscle;
    8 
    9 static void PrintExampleDescription()
   10 {
   11    printf("\n");
   12    printf("This program is the same as example_1_dumb_server except in this version\n");
   13    printf("we create our own DumbReflectSessionFactory and DumbReflectSession subclasses\n");
   14    printf("instead of using the ones built in to the MUSCLE codebase.  That way we can\n");
   15    printf("override all of their methods to print debug output and that way we can see\n");
   16    printf("when their various methods are called.\n");
   17    printf("\n");
   18 }
   19 
   20 static const uint16 DUMB_SERVER_TCP_PORT = 8765;  // arbitrary port number for the "dumb" server
   21 
   22 /** This session subclass is the same as DumbReflectSession except we do a lot of additional
   23   * logging to stdout, so that you can watch which methods are being called in response to
   24   * which network events.
   25   */
   26 class MyDumbReflectSession : public DumbReflectSession
   27 {
   28 public:
   29    MyDumbReflectSession()
   30    {
   31       LogTime(MUSCLE_LOG_INFO, "MyDumbReflectSession Constructor called (this=%p)\n", this);
   32    }
   33 
   34    virtual ~MyDumbReflectSession() 
   35    {
   36       LogTime(MUSCLE_LOG_INFO, "MyDumbReflectSession Destructor called (this=%p)\n",  this);
   37    }
   38 
   39    virtual status_t AttachedToServer()
   40    {
   41       status_t ret = DumbReflectSession::AttachedToServer();  // this call is what attaches us to the ReflectServer
   42       LogTime(MUSCLE_LOG_INFO, "MyDumbReflectSession(%p)::AttachedToServer() called -- returning %s (my session ID is " UINT32_FORMAT_SPEC ")\n", this, (ret==B_NO_ERROR)?"B_NO_ERROR":"B_ERROR", GetSessionID());
   43       return ret;
   44    }
   45 
   46    virtual ConstSocketRef CreateDefaultSocket()
   47    {
   48       ConstSocketRef ret = DumbReflectSession::CreateDefaultSocket();
   49       LogTime(MUSCLE_LOG_INFO, "MyDumbReflectSession(%p)::CreateDefaultSocket() called -- returning %p (socket_fd=%i)\n", this, ret(), ret.GetFileDescriptor());
   50       return ret;
   51    }
   52 
   53    virtual DataIORef CreateDataIO(const ConstSocketRef & sock)
   54    {
   55       DataIORef ret = DumbReflectSession::CreateDataIO(sock);
   56       LogTime(MUSCLE_LOG_INFO, "MyDumbReflectSession(%p)::CreateDataIO(%p) called -- returning TCPSocketDataIO %p\n", this, sock(), ret());
   57       return ret;
   58    }
   59 
   60    virtual AbstractMessageIOGatewayRef CreateGateway()
   61    {
   62       AbstractMessageIOGatewayRef ret = DumbReflectSession::CreateGateway();
   63       LogTime(MUSCLE_LOG_INFO, "MyDumbReflectSession(%p)::CreateGateway() called -- returning MessageIOGateway %p\n", this, ret());
   64       return ret;
   65    }
   66 
   67    virtual void MessageReceivedFromGateway(const MessageRef & msg, void * userData)
   68    {
   69       printf("\n");
   70       LogTime(MUSCLE_LOG_INFO, "MyDumbReflectSession(%p)::MessageReceivedFromGateway(%p,%p) called\n", this, msg(), userData);
   71       LogTime(MUSCLE_LOG_INFO, "The Message from session #" UINT32_FORMAT_SPEC "'s client is:\n", GetSessionID());
   72       msg()->PrintToStream();
   73       DumbReflectSession::MessageReceivedFromGateway(msg, userData);  // will call MessageReceivedFromSession(*this, msg, userData) on all other attached session objects 
   74    }
   75 
   76    virtual void MessageReceivedFromSession(AbstractReflectSession & from, const MessageRef & msg, void * userData)
   77    {
   78       printf("\n");
   79       LogTime(MUSCLE_LOG_INFO, "MyDumbReflectSession(%p)::MessageReceivedSession(%p,%p,%p) called\n", this, &from, msg(), userData);
   80       LogTime(MUSCLE_LOG_INFO, "The Message from session #" UINT32_FORMAT_SPEC " is:\n", from.GetSessionID());
   81       msg()->PrintToStream();
   82       LogTime(MUSCLE_LOG_INFO, "Forwarding the Message on to our own client (of session #" UINT32_FORMAT_SPEC ")\n", GetSessionID());
   83       DumbReflectSession::MessageReceivedFromSession(from, msg, userData);  // will call AddOutgoingMesssage(msg) on this session
   84    }
   85 
   86    virtual bool ClientConnectionClosed()
   87    {
   88       bool ret = DumbReflectSession::ClientConnectionClosed();
   89       LogTime(MUSCLE_LOG_INFO, "MyDumbReflectSession(%p)::ClientConnectionClosed() called -- returning %i (aka \"%s\")\n", this, ret, ret?"destroy the session":"keep the session anyway");
   90       return ret;
   91    }
   92 
   93    virtual void AboutToDetachFromServer()
   94    {
   95       LogTime(MUSCLE_LOG_INFO, "MyDumbReflectSession(%p)::AboutToDetachFromServer() called -- session # " UINT32_FORMAT_SPEC " is about to go away!\n", this, GetSessionID());
   96       DumbReflectSession::AboutToDetachFromServer();  // this call is what detaches us from the ReflectServer
   97    }
   98 };
   99 
  100 /** This factory will create a MyDumbReflectSession object whenever an incoming TCP
  101   * connection is received.  The MyDumbReflectSession it returns will be attached to
  102   * the ReflectServer.
  103   */
  104 class MyDumbReflectSessionFactory : public DumbReflectSessionFactory
  105 {
  106 public:
  107    MyDumbReflectSessionFactory()          {LogTime(MUSCLE_LOG_INFO, "MyDumbReflectSessionFactory Constructor called (this=%p)\n", this);}
  108    virtual ~MyDumbReflectSessionFactory() {LogTime(MUSCLE_LOG_INFO, "MyDumbReflectSessionFactory Destructor called (this=%p)\n",  this);}
  109 
  110    virtual AbstractReflectSessionRef CreateSession(const String & clientAddress, const IPAddressAndPort & factoryInfo)
  111    {
  112       printf("MyDumbReflectSessionFactory::CreateSession() called!  (clientAddress=[%s] factoryInfo=[%s])\n", clientAddress(), factoryInfo.ToString()());
  113       return AbstractReflectSessionRef(new MyDumbReflectSession);
  114    }
  115 };
  116 
  117 int main(int argc, char ** argv)
  118 {
  119    CompleteSetupSystem css;
  120 
  121    PrintExampleDescription();
  122 
  123    // This object contains our server's event loop.
  124    ReflectServer reflectServer;
  125 
  126    // This factory will create a DumbReflectSession object whenever
  127    // a TCP connection is received on DUMB_SERVER_TCP_PORT, and
  128    // attach the DumbReflectSession to the ReflectServer for use.   
  129    MyDumbReflectSessionFactory dumbSessionFactory;
  130    if (reflectServer.PutAcceptFactory(DUMB_SERVER_TCP_PORT, ReflectSessionFactoryRef(&dumbSessionFactory, false)) != B_NO_ERROR)
  131    {
  132       LogTime(MUSCLE_LOG_CRITICALERROR, "Couldn't bind to TCP port %u!  (Perhaps a copy of this program is already running?\n", DUMB_SERVER_TCP_PORT);
  133       return 5;
  134    }
  135 
  136    // Here's where our server will spend all of its time
  137    LogTime(MUSCLE_LOG_INFO, "example_3_annotated_dumb_server is listening for incoming TCP connections on port %u\n", DUMB_SERVER_TCP_PORT);
  138    LogTime(MUSCLE_LOG_INFO, "Try running one or more instances of example_2_dumb_client to connect and chat!\n");
  139    printf("\n");
  140 
  141    // Our server's event loop will run here -- ServerProcessLoop() return until it's time for the server to exit
  142    if (reflectServer.ServerProcessLoop() == B_NO_ERROR)
  143    {
  144        LogTime(MUSCLE_LOG_INFO, "example_3_annotated_dumb_server is exiting normally.\n");
  145    }
  146    else LogTime(MUSCLE_LOG_ERROR, "example_3_annotated_dumb_server is exiting due to an error.\n");
  147 
  148    // Make sure our server lets go of all of its sessions and factories
  149    // before they are destroyed (necessary only because we may have 
  150    // allocated some of them on the stack rather than on the heap)
  151    reflectServer.Cleanup();
  152 
  153    return 0;
  154 }