diff --git a/.gitignore b/.gitignore index a5a9324..06448b5 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ Release *~ *.mk libServerCore.a +docs/latex/ +docs/html diff --git a/Command.cpp b/Command.cpp index 6720121..e5fe0bf 100644 --- a/Command.cpp +++ b/Command.cpp @@ -1,23 +1,12 @@ #include "Command.h" #include "Log.h" +#include "CommandList.h" namespace core { - - Command::Command() {} - - Command::Command(CommandList &commandList) : commandList(commandList) {} int Command::processCommand(std::string request, TCPSession *session, std::stringstream &data) { return 0; } - - void grabInput() { - commandList.grapInput(this); - } - - void clearGrab() { - commandList.clearGrab(this); - } void Command::output(Session *session) {} diff --git a/Command.h b/Command.h index 31c66c0..e3c999a 100644 --- a/Command.h +++ b/Command.h @@ -7,6 +7,8 @@ #include "PString.h" namespace core { + + class CommandList; class Session; @@ -20,9 +22,6 @@ namespace core { class Command : public Object { public: - - Command(); - Command(CommandList &commandList); /// /// Implement check method to provide a special check rule upon the request to see @@ -73,14 +72,9 @@ namespace core { void setName(std::string name); std::string getName(); - - void grabInput(); - void clearGrab(); private: - std::string name; - CommandList &commandList; - + std::string name; }; diff --git a/CommandList.cpp b/CommandList.cpp index 9ba6265..1474518 100644 --- a/CommandList.cpp +++ b/CommandList.cpp @@ -14,15 +14,15 @@ namespace core { bool CommandList::processRequest(std::string request, TCPSession *session, std::stringstream &data) { if(session->grab != NULL) - session->grab->processCommand(request, session, data); + return session->grab->processCommand(request, session, data); else { int pos = request.find(" "); std::string function = pos == request.npos ? request: request.substr(0, pos); for(auto *command : commands) if(command->check(function)) - command->processCommand(request, session, data); + return command->processCommand(request, session, data); } - return true; + return false; } bool CommandList::grabInput(TCPSession *session, Command &command) { diff --git a/TCPSession.cpp b/TCPSession.cpp index 775957a..0967014 100644 --- a/TCPSession.cpp +++ b/TCPSession.cpp @@ -16,10 +16,9 @@ namespace core { } void TCPSession::protocol(std::string data = "") { - if(data.length() > 0) { - if(!server.commands.processRequest(data, this, out)) - server.sessionErrorHandler("Invalid data received.", out); - } + if(!server.commands.processRequest(data, this, out)) + if(data != "") + server.sessionErrorHandler("Invalid data received.", out); } void TCPSession::onRegistered() { @@ -38,24 +37,43 @@ namespace core { memcpy(lineBuffer + lineBufferSize, data, len); lineBufferSize += len; while(lineBufferSize > 0) { - int lineLength = strcspn(lineBuffer, "\r\n"); - if(lineLength == lineBufferSize) - break; - onLineReceived(std::string(lineBuffer, lineLength)); - if(lineBuffer[lineLength] == '\r') - ++lineLength; - if(lineBuffer[lineLength] == '\n') - ++lineLength; - lineBufferSize -= lineLength; - if(lineBufferSize > 0) - memmove(lineBuffer, lineBuffer + lineLength, lineBufferSize); - lineBuffer = (char *)realloc(lineBuffer, lineBufferSize); - } + switch(mode) { + case LINE: + int lineLength = strcspn(lineBuffer, "\r\n"); + if(lineLength == lineBufferSize) + break; + onLineReceived(std::string(lineBuffer, lineLength)); + if(lineBuffer[lineLength] == '\r') + ++lineLength; + if(lineBuffer[lineLength] == '\n') + ++lineLength; + lineBufferSize -= lineLength; + if(lineBufferSize > 0) + memmove(lineBuffer, lineBuffer + lineLength, lineBufferSize); + lineBuffer = (char *)realloc(lineBuffer, lineBufferSize); + break; + case BLOCK: + if(lineBufferSize >= blockLength) { + onBlockReceived(std::string(lineBuffer, blockLength)); + lineBufferSize -= blockLength; + if(lineBufferSize > 0) + memmove(lineBuffer, lineBuffer + blockLength, lineBufferSize); + lineBuffer = (char *)realloc(lineBuffer, lineBufferSize); + } + break; + } + } } } + + void setMode(Mode mode, int blockSize = 0) { + this->mode = mode; + this->blockSize = blockSize; + } void TCPSession::onLineReceived(std::string line) { coreutils::Log(coreutils::LOG_DEBUG_3) << "[" << line << "]"; + protocol(line); send(); if(term) diff --git a/TCPSession.h b/TCPSession.h index ddb92fe..c48ca33 100644 --- a/TCPSession.h +++ b/TCPSession.h @@ -14,16 +14,28 @@ namespace core { /// TCPSession /// /// TCPSession defines the nature of the interaction with the client - /// and stores persistent data for a defined session. BMASession objects + /// and stores persistent data for a defined session. TCPSession objects /// are not sockets but instead provide a communications control /// mechanism. Protocol conversations are provided through extensions /// from this object. /// + /// + /// class TCPSession : public TCPSocket { public: + + /// + /// + /// + TCPSession(EPoll &ePoll, TCPServer &server, std::string text = ""); + + /// + /// + /// + ~TCPSession(); Command *grab = NULL; @@ -59,13 +71,13 @@ namespace core { void sendToAll(SessionFilter filter); /// - /// + /// Use this method to terminate this TCPSession. /// void terminate(); /// - /// + /// /// TCPServer &server; @@ -95,6 +107,15 @@ namespace core { virtual void onLineReceived(std::string line); + /// + /// Override the onBlockReceived method to receive a string of characters that + /// represents a single block of data of length determined by the block length value. If + /// onDataReceived was overriden this method will not be called unless the onDataReceived + /// calls this method explicitly using the class and member name. + /// + + virtual void onBlockReceived(std::string block); + /// /// This method is called from within the protocol method when protocol is called /// on the initial connection where the data is an empty string. Use this method @@ -107,16 +128,31 @@ namespace core { /// Override the protocol method to manage and control the session communications /// in your inherited session. If you do not override this method then the Session /// default will process the 'commands' added to the server object using the - /// processRequest method on the session input. + /// processRequest method on the session input. + /// + /// When data is received within the session two modes are available to pass the + /// data through the protocol method: LINE or BLOCK. /// virtual void protocol(std::string data); + /// + /// Use the setMode method to set the receiving mode for the data on this socket. + /// Data can be received in LINE mode, which will receive data from the socket one + /// line at a time, or BLOCK mode where a certain specified data block is received + /// before calling the onBlockReceived method. + /// + + void setMode(Mode mode, int size = 0); + private: char *lineBuffer = NULL; int lineBufferSize = 0; std::mutex mtx; bool term = false; + enum Mode {LINE, BLOCK}; + enum Mode mode = LINE; + int blockSize; }; diff --git a/docs/guide/BMASockets Programmer's Guide.tex b/docs/guide/BMASockets Programmer's Guide.tex index b693f69..bf5867d 100644 --- a/docs/guide/BMASockets Programmer's Guide.tex +++ b/docs/guide/BMASockets Programmer's Guide.tex @@ -101,14 +101,22 @@ the BMATCPServerSocket or BMAUDPServerSocket, depending on the type desired, and the BMASession object. When extending the BMATCPServerSocket all that is needed is to -override the getSocketAccept() method to return an extended BMASession +override the getSocketAccept() method to return an extended TCPSession object. This basically tells the server to spawn a new session of a particular type for every new connection to the bound TCP port. -The extended BMASession object can override the onDataReceived() -method to handle the incoming requests for the socket. An entire -application structure could be built upon this mechanism to handle -complex protocols with the client. +Data received in the session can be delivered to the application +using two different modes. The LINE mode will call the onLineReceived() +handler for each line received in the buffer. The line termination can +be LF or CRLF combination and is filtered from the string handed to +the onLineReceived() method call. The BLOCK mode will call the +onBlockReceived() handler for each blockSize bytes received in the +data buffer. + +The extended TCPSession object can override the onLineReceived() +and/or the onBlockReceived() methods to handle the incoming requests +for the socket. An entire application structure could be built upon +this mechanism to handle complex protocols with the client. \chapter{Sample Server Example} diff --git a/docs/html/_command_8cpp.html b/docs/html/_command_8cpp.html new file mode 100644 index 0000000..2e7f369 --- /dev/null +++ b/docs/html/_command_8cpp.html @@ -0,0 +1,135 @@ + + + + + + + +ServerCore: Command.cpp File Reference + + + + + + + + + +
+
+ + + + + + +
+
ServerCore +
+
+
+ + + + + + + + +
+
+ + +
+ +
+ +
+
+
+Namespaces
+
+
Command.cpp File Reference
+
+
+
#include "Command.h"
+#include "Log.h"
+#include "CommandList.h"
+
+Include dependency graph for Command.cpp:
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ + + +

+Namespaces

 core
 
+
+ + + + diff --git a/docs/html/_command_8cpp__incl.map b/docs/html/_command_8cpp__incl.map new file mode 100644 index 0000000..0a56ad9 --- /dev/null +++ b/docs/html/_command_8cpp__incl.map @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/html/_command_8cpp__incl.md5 b/docs/html/_command_8cpp__incl.md5 new file mode 100644 index 0000000..ce2e774 --- /dev/null +++ b/docs/html/_command_8cpp__incl.md5 @@ -0,0 +1 @@ +c3493544a0dd589b872c2be0e89dc3a7 \ No newline at end of file diff --git a/docs/html/_command_8cpp__incl.png b/docs/html/_command_8cpp__incl.png new file mode 100644 index 0000000..ed569d6 Binary files /dev/null and b/docs/html/_command_8cpp__incl.png differ diff --git a/docs/html/_command_8h.html b/docs/html/_command_8h.html new file mode 100644 index 0000000..0cccc54 --- /dev/null +++ b/docs/html/_command_8h.html @@ -0,0 +1,145 @@ + + + + + + + +ServerCore: Command.h File Reference + + + + + + + + + +
+
+ + + + + + +
+
ServerCore +
+
+
+ + + + + + + + +
+
+ + +
+ +
+ +
+
+
+Classes | +Namespaces
+
+
Command.h File Reference
+
+
+
#include "includes"
+#include "Object.h"
+#include "TCPSession.h"
+#include "PString.h"
+
+Include dependency graph for Command.h:
+
+
+ + + + + + + + + + + +
+
+This graph shows which files directly or indirectly include this file:
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+

Go to the source code of this file.

+ + + + +

+Classes

class  core::Command
 
+ + + +

+Namespaces

 core
 
+
+ + + + diff --git a/docs/html/_command_8h__dep__incl.map b/docs/html/_command_8h__dep__incl.map new file mode 100644 index 0000000..acbffa0 --- /dev/null +++ b/docs/html/_command_8h__dep__incl.map @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/html/_command_8h__dep__incl.md5 b/docs/html/_command_8h__dep__incl.md5 new file mode 100644 index 0000000..71699c2 --- /dev/null +++ b/docs/html/_command_8h__dep__incl.md5 @@ -0,0 +1 @@ +d0ae1bcca810bb89f04e7b8d7a94d8d7 \ No newline at end of file diff --git a/docs/html/_command_8h__dep__incl.png b/docs/html/_command_8h__dep__incl.png new file mode 100644 index 0000000..3dde69f Binary files /dev/null and b/docs/html/_command_8h__dep__incl.png differ diff --git a/docs/html/_command_8h__incl.map b/docs/html/_command_8h__incl.map new file mode 100644 index 0000000..909bce0 --- /dev/null +++ b/docs/html/_command_8h__incl.map @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/docs/html/_command_8h__incl.md5 b/docs/html/_command_8h__incl.md5 new file mode 100644 index 0000000..4125270 --- /dev/null +++ b/docs/html/_command_8h__incl.md5 @@ -0,0 +1 @@ +b554c51bb6894aff8f756d5daeff3714 \ No newline at end of file diff --git a/docs/html/_command_8h__incl.png b/docs/html/_command_8h__incl.png new file mode 100644 index 0000000..a895eb1 Binary files /dev/null and b/docs/html/_command_8h__incl.png differ diff --git a/docs/html/_command_8h_source.html b/docs/html/_command_8h_source.html index c35f82d..2bdc509 100644 --- a/docs/html/_command_8h_source.html +++ b/docs/html/_command_8h_source.html @@ -1,11 +1,11 @@ - + - + -BMA Server Framework: /home/bradarant/barant/ServerCore/Command.h Source File +ServerCore: Command.h Source File @@ -21,7 +21,7 @@ -
BMA Server Framework +
ServerCore
@@ -29,18 +29,21 @@
- + +/* @license-end */ @@ -59,23 +62,67 @@ $(function() {
-
/home/bradarant/barant/ServerCore/Command.h
+
Command.h
-
1 #ifndef __Command_h__
2 #define __Command_h__
3 
4 #include "includes"
5 #include "Object.h"
6 
7 namespace core {
8 
9  class Session;
10 
17 
18  class Command : public Object {
19 
20  public:
21 
35 
36  virtual bool check(std::string request);
37 
48 
49  virtual int processCommand(std::string request, Session *session, std::stringstream &data);
50 
56 
57  virtual void output(Session *session);
58 
67 
68  void setName(std::string name);
69 
70  std::string getName();
71 
72  private:
73  std::string name;
74 
75  };
76 
77 }
78 
79 #endif
virtual int processCommand(std::string request, Session *session, std::stringstream &data)
Definition: Command.cpp:6
-
Definition: Command.cpp:4
-
Definition: Session.h:22
-
void setName(std::string name)
Definition: Command.cpp:21
-
virtual bool check(std::string request)
Definition: Command.cpp:10
-
Definition: Object.h:8
-
Definition: Command.h:18
-
virtual void output(Session *session)
Definition: Command.cpp:8
+Go to the documentation of this file.
1 #ifndef __Command_h__
+
2 #define __Command_h__
+
3 
+
4 #include "includes"
+
5 #include "Object.h"
+
6 #include "TCPSession.h"
+
7 #include "PString.h"
+
8 
+
9 namespace core {
+
10 
+
11  class CommandList;
+
12 
+
13  class Session;
+
14 
+
21 
+
22  class Command : public Object {
+
23 
+
24  public:
+
25 
+
39 
+
40  virtual bool check(std::string request);
+
41 
+
52 
+
53  virtual int processCommand(std::string request, TCPSession *session, std::stringstream &data);
+
54 
+
60 
+
61  virtual void output(Session *session);
+
62 
+
71 
+
72  void setName(std::string name);
+
73 
+
74  std::string getName();
+
75 
+
76  private:
+
77  std::string name;
+
78 
+
79  };
+
80 
+
81 }
+
82 
+
83 #endif
+
std::string getName()
Definition: Command.cpp:25
+
void setName(std::string name)
Definition: Command.cpp:21
+
+
Definition: TCPSession.h:25
+
Definition: Command.cpp:5
+
Definition: Object.h:8
+
Definition: Command.h:22
+
virtual void output(Session *session)
Definition: Command.cpp:11
+
virtual bool check(std::string request)
Definition: Command.cpp:13
+
virtual int processCommand(std::string request, TCPSession *session, std::stringstream &data)
Definition: Command.cpp:7
+
diff --git a/html/classcore_1_1IPAddressList-members.html b/docs/html/_command_list_8cpp.html similarity index 55% rename from html/classcore_1_1IPAddressList-members.html rename to docs/html/_command_list_8cpp.html index 8d59b01..07c5dcf 100644 --- a/html/classcore_1_1IPAddressList-members.html +++ b/docs/html/_command_list_8cpp.html @@ -5,7 +5,7 @@ -My Project: Member List +ServerCore: CommandList.cpp File Reference @@ -21,7 +21,7 @@ -
My Project +
ServerCore
@@ -59,25 +59,42 @@ $(function() {
-
+
+Namespaces
-
core::IPAddressList Member List
+
CommandList.cpp File Reference
- -

This is the complete list of members for core::IPAddressList, including all inherited members.

- - - - - - -
add(IPAddress ipAddress) (defined in core::IPAddressList)core::IPAddressList
contains(std::string ipAddress) (defined in core::IPAddressList)core::IPAddressList
getList() (defined in core::IPAddressList)core::IPAddressList
IPAddressList() (defined in core::IPAddressList)core::IPAddressList
remove(IPAddress ipAddress) (defined in core::IPAddressList)core::IPAddressList
+
#include "CommandList.h"
+#include "Log.h"
+
+Include dependency graph for CommandList.cpp:
+
+
+ + + + + + + + + + + + + + +
+
+ + + +

+Namespaces

 core
 
+