Thread: application design

  1. #1
    sockets mad
    Join Date
    Mar 2002
    Posts
    126

    application design

    This is a bit of a long post but I'll be grateful to anyone who cam help out

    I've been coding a server application for a few weeks now. As it gets more and more complicated, some design issues are coming up which I'm having trouble deciding on, and thought I'd ask you what you think.

    My application uses a select() statement to control the I/O and a callback function system similar to that of squid and ircd-hybrid, although I've decided to try and implement some of this using C++ OOP rather than pure C as in squid and ircd-hybrid.

    The way the system works is like this:

    Each socket (whether it be a listening socket, client connection or whatever) has an associated callback function to call when the socket has data waiting, and when it is possible to write to the socket. Using the output from select, the appropriate callback functions are called, and the I/O is handled. For example, with a listening socket, it's readability callback would be a function which accepts the new connection, and with a client socket, the function would parse the incoming data and do whatever is required.

    I'm finding it hard to decide exactly how to turn this system into C++ classes in an elegant way. I'm very new to OOP so I lack practice.

    I've been trying to think about it in an "is a" fashion, as most OOP books tell you to do.

    I thought of having a Socket class, which has the following member variables0:

    SOCKET sock
    callback* read_handler // Pointer to callback function
    callback* write_handler // Pointer to callback function

    Then I could derive from this class to have more specific objects like a Listener class and a Client class. Then using a pointer to the base class I can still call the read/write handlers of all the Listeners and Clients as they are derived from the Socket class. How's that sound?

    Another way I thought of doing it is to actually have the read/write handlers as pure virtual methods of the Socket class and implement them as needed in the derived classes (Listener and Client). Then using a pointer to the base class again I can call the appropriate handler function using polymorphism.

    I suppose these functions would be called something like

    onReadability and onWritability (Like events)

    What does everyone think?

    Sorry if it's a bit long. Thanks for any help, much appreciated.

    Daniel
    C/C++ Support IRC Channel

    Server: irc.dal.net Channel: #csupport

    Come along and help make it a great resource for the community.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    Instead of callbacks, I will use an action classes with some variation such as
    Code:
    class ReadListener
    {
    public:
              virtual void Action() = 0;
    };
    
    class WriteListener
    {
    public:
              virtual void Action() = 0;
    };
    Specific instances will inherit from these two classes.
    And then within the Socket class you could do something like
    Code:
    class Socket
    {
              typedef std::vector<ReadListener*> ReadListeners;
              typedef std::vector<WriteListener*> WriteListeners;
        
    
              ReadListeners  m_readListeners;
              WriteListeners  m_writeListeners;
    public:
              void Write(...)
              {
                       for (WriteListeners::size_type i = 0; i < m_writeListeners.size(); ++i)
                       {
                                 m_writeListeners[i]->Action();
                       }
              }
    
              void AddWriteListener(WriteListener* writeListener);
              void RemoveWriteListener(WriteListener* writeListener);
    };
    This worked good for my project, and is similar to how java swing does it. You have to be careful to remove the listener inside of the derived listener's destructor though.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    Should probably also use virtual destructors so the action classes will call the right destructor.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Application Design Help Needed?
    By neumee in forum C Programming
    Replies: 3
    Last Post: 07-11-2008, 04:46 PM
  2. Implementing Inheritence into your design
    By bobthebullet990 in forum C++ Programming
    Replies: 6
    Last Post: 08-05-2006, 04:40 PM
  3. MFC run application by clicking on file...
    By dug in forum Windows Programming
    Replies: 4
    Last Post: 12-02-2004, 04:33 AM