diff --git a/ConsoleServer.cpp b/ConsoleServer.cpp index 57e15a3..f898c5b 100644 --- a/ConsoleServer.cpp +++ b/ConsoleServer.cpp @@ -5,8 +5,8 @@ namespace core { - ConsoleServer::ConsoleServer(EPoll &ePoll, std::string url, short int port) - : TCPServerSocket(ePoll, url, port) { + ConsoleServer::ConsoleServer(EPoll &ePoll, IPAddress address) + : TCPServerSocket(ePoll, address) { Log(this); } diff --git a/ConsoleServer.h b/ConsoleServer.h index da5d7e6..ef775a7 100644 --- a/ConsoleServer.h +++ b/ConsoleServer.h @@ -24,7 +24,7 @@ namespace core { // // - ConsoleServer(EPoll &ePoll, std::string url, short int port); + ConsoleServer(EPoll &ePoll, IPAddress address); // // diff --git a/IPAddress.cpp b/IPAddress.cpp index 67b5cc1..c36ffed 100644 --- a/IPAddress.cpp +++ b/IPAddress.cpp @@ -4,8 +4,27 @@ namespace core { IPAddress::IPAddress() { addressLength = sizeof(struct sockaddr_in); + pointer = (sockaddr *)&address; } - + + IPAddress::IPAddress(std::string address) { + std::string url = address.substr(0, address.find(":")); + std::string s_port = address.substr(address.find(":") + 1); + std::stringstream convert(s_port); + short int port = 0; + convert >> port; + IPAddress(url, port); + } + + IPAddress::IPAddress(std::string address, short int port) { + struct sockaddr_in addr; + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons(port); + struct hostent *hp = gethostbyname(address.c_str()); + memcpy((void *)&addr.sin_addr, hp->h_addr_list[0], hp->h_length); + } + IPAddress::~IPAddress() { } diff --git a/IPAddress.h b/IPAddress.h index acbf9b8..f39ba15 100644 --- a/IPAddress.h +++ b/IPAddress.h @@ -10,10 +10,13 @@ namespace core { public: IPAddress(); + IPAddress(std::string address); + IPAddress(std::string address, short int port); ~IPAddress(); struct sockaddr_in address; socklen_t addressLength; + struct sockaddr *pointer; std::string getClientAddress(); /// + + diff --git a/ServerCore.txt b/ServerCore.txt index 62e67bf..47147f7 100644 --- a/ServerCore.txt +++ b/ServerCore.txt @@ -1 +1 @@ -./Debug/Command.cpp.o ./Debug/ConsoleServer.cpp.o ./Debug/ConsoleSession.cpp.o ./Debug/EPoll.cpp.o ./Debug/Exception.cpp.o ./Debug/File.cpp.o ./Debug/Header.cpp.o ./Debug/IPAddress.cpp.o ./Debug/Log.cpp.o ./Debug/Response.cpp.o ./Debug/Session.cpp.o ./Debug/Socket.cpp.o ./Debug/TCPServerSocket.cpp.o ./Debug/TCPSocket.cpp.o ./Debug/Thread.cpp.o ./Debug/Timer.cpp.o ./Debug/TLSServerSocket.cpp.o ./Debug/TLSSession.cpp.o ./Debug/UDPServerSocket.cpp.o ./Debug/UDPSocket.cpp.o ./Debug/CommandList.cpp.o ./Debug/TerminalSession.cpp.o +./Debug/Command.cpp.o ./Debug/ConsoleServer.cpp.o ./Debug/ConsoleSession.cpp.o ./Debug/EPoll.cpp.o ./Debug/Exception.cpp.o ./Debug/File.cpp.o ./Debug/Header.cpp.o ./Debug/IPAddress.cpp.o ./Debug/Log.cpp.o ./Debug/Response.cpp.o ./Debug/Session.cpp.o ./Debug/Socket.cpp.o ./Debug/TCPServerSocket.cpp.o ./Debug/TCPSocket.cpp.o ./Debug/Thread.cpp.o ./Debug/Timer.cpp.o ./Debug/TLSServerSocket.cpp.o ./Debug/TLSSession.cpp.o ./Debug/UDPServerSocket.cpp.o ./Debug/UDPSocket.cpp.o ./Debug/CommandList.cpp.o ./Debug/TerminalSession.cpp.o ./Debug/Service.cpp.o diff --git a/Session.cpp b/Session.cpp index b951e7c..21663d1 100644 --- a/Session.cpp +++ b/Session.cpp @@ -38,9 +38,9 @@ namespace core { out.str(""); } - void Session::sendToAll(SessionFilter *filter) { + void Session::sendToAll(SessionFilter filter) { for(auto session : service.sessions) { - if(filter->test(*session)) { + if(filter.test(*session)) { if(session != this) session->write(out.str()); } diff --git a/Session.h b/Session.h index e270010..a2b9a90 100644 --- a/Session.h +++ b/Session.h @@ -49,7 +49,7 @@ namespace core { /// and the entries identified by the passed in filter object. /// - void sendToAll(SessionFilter *filter); + void sendToAll(SessionFilter filter); std::stringstream out; diff --git a/TCPServerSocket.cpp b/TCPServerSocket.cpp index 3ef2278..21516a2 100644 --- a/TCPServerSocket.cpp +++ b/TCPServerSocket.cpp @@ -5,19 +5,12 @@ namespace core { - TCPServerSocket::TCPServerSocket(EPoll &ePoll, std::string url, short int port) : TCPSocket(ePoll) { - - struct sockaddr_in addr; - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_port = htons(port); - struct hostent *hp = gethostbyname(url.c_str()); - memcpy((void *)&addr.sin_addr, hp->h_addr_list[0], hp->h_length); - + TCPServerSocket::TCPServerSocket(EPoll &ePoll, IPAddress address) : TCPSocket(ePoll) { + setDescriptor(socket(AF_INET, SOCK_STREAM, 0)); int yes = 1; setsockopt(getDescriptor(), SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)); - if(bind(getDescriptor(), (struct sockaddr *)&addr, sizeof(addr)) < 0) + if(bind(getDescriptor(), address.pointer, sizeof(address)) < 0) throw Exception("Error on bind to socket"); if(listen(getDescriptor(), 10) < 0) throw Exception("Error on listen to socket"); diff --git a/TCPServerSocket.h b/TCPServerSocket.h index 8890526..febee0d 100644 --- a/TCPServerSocket.h +++ b/TCPServerSocket.h @@ -32,13 +32,15 @@ namespace core { /// @param commandName the name of the command used to invoke the status display for this object. /// @return the instance of the BMATCPServerSocket. - TCPServerSocket(EPoll &ePoll, std::string url, short int port); + TCPServerSocket(EPoll &ePoll, IPAddress address); /// /// The destructor for this object. /// ~TCPServerSocket(); + + Service *service; protected: @@ -82,8 +84,6 @@ namespace core { /// int processCommand(std::string command, Session *session) override; - - Service *service; private: diff --git a/TLSServerSocket.cpp b/TLSServerSocket.cpp index 0f3711e..e00df61 100644 --- a/TLSServerSocket.cpp +++ b/TLSServerSocket.cpp @@ -16,7 +16,7 @@ namespace core { // pthread_mutex_unlock(&(lockarray[type])); //} - TLSServerSocket::TLSServerSocket(EPoll &ePoll, std::string url, short int port) : TCPServerSocket(ePoll, url, port) { + TLSServerSocket::TLSServerSocket(EPoll &ePoll, IPAddress address) : TCPServerSocket(ePoll, address) { tlsServerInit(); // TODO: Convert to use core::Exception object. if(!(((TLSService *)service)->ctx = SSL_CTX_new(SSLv23_server_method()))) diff --git a/TLSServerSocket.h b/TLSServerSocket.h index 91a5860..4c86e8c 100644 --- a/TLSServerSocket.h +++ b/TLSServerSocket.h @@ -5,6 +5,7 @@ #include "TCPServerSocket.h" #include "Command.h" #include "Session.h" +#include "IPAddress.h" namespace core { @@ -28,7 +29,7 @@ namespace core { /// @param commandName the name of the command used to invoke the status display for this object. /// @return the instance of the BMATLSServerSocket. - TLSServerSocket(EPoll &ePoll, std::string url, short int port); + TLSServerSocket(EPoll &ePoll, IPAddress address); /// /// The destructor for this object. diff --git a/docs/html/_command_8h_source.html b/docs/html/_command_8h_source.html index 7b52c96..e82b82a 100644 --- a/docs/html/_command_8h_source.html +++ b/docs/html/_command_8h_source.html @@ -62,7 +62,7 @@ $(function() {
/home/barant/Development/BMA/server_core/ServerCore/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) = 0;
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
Definition: Command.cpp:4
+
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) = 0;
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
Definition: Command.cpp:4
Definition: Session.h:22
void setName(std::string name)
Definition: Command.cpp:19
virtual int processCommand(std::string request, Session *session)=0
diff --git a/docs/html/_command_list_8h_source.html b/docs/html/_command_list_8h_source.html index 903f4a4..d77e315 100644 --- a/docs/html/_command_list_8h_source.html +++ b/docs/html/_command_list_8h_source.html @@ -62,11 +62,14 @@ $(function() {
/home/barant/Development/BMA/server_core/ServerCore/CommandList.h
-
1 #ifndef __CommandList_h__
2 #define __CommandList_h__
3 
4 #include "Session.h"
5 #include "Command.h"
6 
7 namespace core {
8 
9  class CommandList : public Command {
10 
11  public:
12  CommandList();
13  ~CommandList();
14 
15  void add(Command &command, std::string name = "");
16 
17  void remove(Command &command);
18 
19  bool processRequest(std::string request, Session *session);
20 
21  int processCommand(std::string request, Session *session) override;
22 
23  private:
24  std::vector<Command *> commands;
25 
26  };
27 
28 }
29 
30 #endif
Definition: Command.cpp:4
+
1 #ifndef __CommandList_h__
2 #define __CommandList_h__
3 
4 #include "Session.h"
5 #include "Command.h"
6 
7 namespace core {
8 
16 
17  class CommandList : public Command {
18 
19  public:
20 
24 
25  CommandList();
26 
30 
31  ~CommandList();
32 
36 
37  void add(Command &command, std::string name = "");
38 
39  void remove(Command &command);
40 
41  bool processRequest(std::string request, Session *session);
42 
43  int processCommand(std::string request, Session *session) override;
44 
45  private:
46  std::vector<Command *> commands;
47 
48  };
49 
50 }
51 
52 #endif
CommandList()
Definition: CommandList.cpp:5
+
Definition: Command.cpp:4
Definition: Session.h:22
-
int processCommand(std::string request, Session *session) override
Definition: CommandList.cpp:33
+
~CommandList()
Definition: CommandList.cpp:7
+
int processCommand(std::string request, Session *session) override
Definition: CommandList.cpp:28
Definition: Command.h:18
-
Definition: CommandList.h:9
+
Definition: CommandList.h:17
+
void add(Command &command, std::string name="")
Definition: CommandList.cpp:9
-
1 #ifndef __ConsoleServer_h__
2 #define __ConsoleServer_h__
3 
4 #include "includes"
5 #include "TCPServerSocket.h"
6 #include "Command.h"
7 #include "Session.h"
8 #include "EPoll.h"
9 
10 namespace core {
11 
12  class TCPSocket;
13 
17 
18  class ConsoleServer : public TCPServerSocket {
19 
20  public:
21 
22  //
23  //
24  //
25 
26  ConsoleServer(EPoll &ePoll, std::string url, short int port);
27 
28  //
29  //
30  //
31 
32  ~ConsoleServer();
33 
34  void sendToConnectedConsoles(std::string out);
35 
36  void output(Session *session) override;
37 
38  protected:
39 
40  Session * getSocketAccept() override;
41 
42  };
43 
44 }
45 
46 
47 #endif
Definition: EPoll.h:31
+
1 #ifndef __ConsoleServer_h__
2 #define __ConsoleServer_h__
3 
4 #include "includes"
5 #include "TCPServerSocket.h"
6 #include "Service.h"
7 #include "Command.h"
8 #include "Session.h"
9 #include "EPoll.h"
10 
11 namespace core {
12 
13  class TCPSocket;
14 
18 
19  class ConsoleServer : public TCPServerSocket {
20 
21  public:
22 
23  //
24  //
25  //
26 
27  ConsoleServer(EPoll &ePoll, std::string url, short int port);
28 
29  //
30  //
31  //
32 
33  ~ConsoleServer();
34 
35  void sendToConnectedConsoles(std::string out);
36 
37  void output(Session *session) override;
38 
39  protected:
40 
41  Session * getSocketAccept() override;
42 
43  };
44 
45 }
46 
47 
48 #endif
Definition: EPoll.h:31
Definition: Command.cpp:4
Definition: Session.h:22
Session * getSocketAccept() override
Definition: ConsoleServer.cpp:20
void output(Session *session) override
Output the consoles array to the console.
Definition: ConsoleServer.cpp:24
-
Definition: ConsoleServer.h:18
-
Definition: TCPServerSocket.h:23
+
Definition: ConsoleServer.h:19
+
Definition: TCPServerSocket.h:22
-
1 #ifndef __ConsoleSession_h__
2 #define __ConsoleSession_h__
3 
4 #include "TerminalSession.h"
5 #include "Session.h"
6 #include "ConsoleServer.h"
7 #include "CommandList.h"
8 
9 namespace core {
10 
18 
20 
21  public:
22  ConsoleSession(EPoll &ePoll, ConsoleServer &server);
23  ~ConsoleSession();
24 
25  virtual void output(std::stringstream &out);
26  void writeLog(std::string data);
27 
28  protected:
29  void protocol(std::string data) override;
30 
31  private:
32  enum Status {WELCOME, LOGIN, WAIT_USER_PROFILE, PASSWORD, WAIT_PASSWORD, PROMPT, INPUT, PROCESS, DONE};
33  Status status = WELCOME;
34  void doCommand(std::string request);
35  std::string command;
36 
37  };
38 
39 }
40 
41 #endif
Definition: EPoll.h:31
+
1 #ifndef __ConsoleSession_h__
2 #define __ConsoleSession_h__
3 
4 #include "TerminalSession.h"
5 #include "Session.h"
6 #include "Service.h"
7 #include "CommandList.h"
8 
9 namespace core {
10 
18 
20 
21  public:
22  ConsoleSession(EPoll &ePoll, Service &service);
23  ~ConsoleSession();
24 
25  virtual void output(std::stringstream &out);
26  void writeLog(std::string data);
27 
28  protected:
29  void protocol(std::string data) override;
30 
31  private:
32  enum Status {WELCOME, LOGIN, WAIT_USER_PROFILE, PASSWORD, WAIT_PASSWORD, PROMPT, INPUT, PROCESS, DONE};
33  Status status = WELCOME;
34  void doCommand(std::string request);
35  std::string command;
36 
37  };
38 
39 }
40 
41 #endif
Definition: EPoll.h:31
Definition: Command.cpp:4
Definition: ConsoleSession.h:19
-
virtual void output(std::stringstream &out)
Definition: ConsoleSession.cpp:11
-
Definition: ConsoleServer.h:18
-
Definition: TerminalSession.h:29
+
virtual void output(std::stringstream &out)
Definition: ConsoleSession.cpp:10
+
Definition: Service.h:20
+
void protocol(std::string data) override
Definition: ConsoleSession.cpp:14
+
Definition: TerminalSession.h:30