Browse Source

adding comments, adding another interface to be more explicit, updating build

logicp 5 years ago
parent
commit
6e804b2a94

+ 3 - 1
CMakeLists.txt

@@ -1,13 +1,15 @@
 cmake_minimum_required(VERSION 2.8)
 project(ws_server)
-
+set(LISTENERLIB "socket_listener")
 set(THREADS_PREFER_PTHREAD_FLAG ON)
 find_package(Threads REQUIRED)
 set(SOURCES "src/main.cpp" "src/socket_listener.cpp" "src/task_queue.cpp")
 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -std=c++17 -pthread")
 add_executable(${PROJECT_NAME} ${SOURCES})
+add_library(${LISTENERLIB} ${SOURCES})
 target_include_directories(${PROJECT_NAME} PRIVATE
   "headers"
+  "headers/interface"
 )
 target_link_libraries(${PROJECT_NAME} Threads::Threads)
 

+ 0 - 3
headers/constants.hpp

@@ -1,7 +1,5 @@
 #ifndef __CONSTANTS_H__
 #define __CONSTANTS_H__
-#ifndef TRX_SOCKET_CONSTANTS
-#define TRX_SOCKET_CONSTANTS 1
 /**
  * Values used when attempting to open a socket
  */
@@ -13,5 +11,4 @@ const int SOCKET_OK = 0;
 const int WAIT_SOCKET_FAILURE = -1;
 const int WAIT_SOCKET_SUCCESS = 0;
 
-#endif
 #endif  // __CONSTANTS_H__

+ 22 - 0
headers/interface/listen_interface.hpp

@@ -0,0 +1,22 @@
+#ifndef __LISTEN_INTERFACE_H__
+#define __LISTEN_INTERFACE_H__
+
+#include <memory>
+#include <string>
+
+/**
+ * ListenInterface
+ *
+ * A public interface whose implementation handles the receival of a character
+ * buffer assumed have been sent from a client socket connection, indicated by a
+ * file descriptor, communicating with the implementor.
+ *
+ * @interface
+ */
+class ListenInterface {
+ public:
+  virtual void onMessageReceived(int client_socket_fd,
+                                 std::weak_ptr<char[]> w_buffer_ptr) = 0;
+};
+
+#endif  // __LISTEN_INTERFACE_H__

+ 9 - 0
headers/send_interface.hpp → headers/interface/send_interface.hpp

@@ -4,6 +4,15 @@
 #include <memory>
 #include <string>
 
+/**
+ * SendInterface
+ *
+ * A public interface whose implementation sends a buffer of characters to the
+ * socket connection indicated by the client_socket_fd (client socket file
+ * descriptor)
+ *
+ * @interface
+ */
 class SendInterface {
  public:
   virtual void sendMessage(int client_socket_fd,

+ 19 - 6
headers/socket_listener.hpp

@@ -2,7 +2,8 @@
 #define __SOCKET_LISTENER_H__
 
 // Project libraries
-#include "send_interface.hpp"
+#include "interface/listen_interface.hpp"
+#include "interface/send_interface.hpp"
 #include "task_queue.hpp"
 #include "types.hpp"
 
@@ -16,8 +17,22 @@
 #include <string>
 #include <vector>
 
-class SocketListener : public SendInterface {
+/**
+ * SocketListener
+ *
+ * SocketListener is extensible to aid in architecting a socket server
+ */
+class SocketListener : public SendInterface, public ListenInterface {
  public:
+  /* public classes whose instances are used by SocketListener */
+
+  /**
+   * MessageHandler
+   *
+   * Instances of this object type wrap a generic, self-contained function and
+   * behave as callable functions (functors)
+   * @class
+   */
   class MessageHandler {
    public:
     MessageHandler(std::function<void()> cb) : m_cb(cb) {}
@@ -40,7 +55,7 @@ class SocketListener : public SendInterface {
    */
   virtual void sendMessage(int client_socket_fd,
                            std::weak_ptr<char[]> w_buffer_ptr) override;
-
+  /** overload variants */
   void sendMessage(int client_socket_fd, char* message, bool short_message);
 
   void sendMessage(int client_socket_fd, char* message, size_t size);
@@ -63,14 +78,12 @@ class SocketListener : public SendInterface {
    */
   void cleanup();
 
-  // virtual void setMessageHandler(MessageHandler message_handler) override;
-
  private:
   // private methods
   int createSocket();
 
   virtual void onMessageReceived(int client_socket_fd,
-                                 std::weak_ptr<char[]> w_buffer_ptr);
+                                 std::weak_ptr<char[]> w_buffer_ptr) override;
 
   int waitForConnection(int listening);