-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.h
More file actions
188 lines (150 loc) · 4.77 KB
/
Server.h
File metadata and controls
188 lines (150 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* Server.h
* Created on: Jun 10, 2016
* Author: nicole
*/
#ifndef SERVER_H_
#define SERVER_H_
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h> // for stat
#include <fstream> // std::fstream
#include <string>
#include <string.h>
#include <sys/types.h> // for size_t, off_t
#include <vector>
#include <list>
#include <map>
#include <iostream> // std::cout
#include <sstream> // std::stringstream, std::stringbuf
#include <algorithm> // sort,distance, find_if, copy_if, transform
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
#include <sys/socket.h>
#include "Logger.h"
#include "Event.h"
#include "CommandParser.h"
/* server log name - includes all the commands it received */
#define LOGNAME "emServer.log"
/**
* A singleton Server in charge over management of incoming client commands.
* (similar to the my CachManager implementation in ex4).
*/
class Server
{
public:
static void logServer( const std::string response);
/**
* @brief: Every error message in the server log (except system call error)
* should be in the following format:
* HH:MM:SS \tERROR\t<function name>\t<error description>.\n
*/
static void logServerError( std::string func, std::string desc);
/**
* destructor
*/
virtual ~Server();
/**
* @brief: copy constructor - disabled in order to avoid accidental
* copies creation of the singleton instance.
*/
Server( Server const &other) = delete;
/**
* @brief: copy assignment copy constructor - disabled in order to avoid
* accidental copies creation of the singleton instance.
*/
void operator=( Server const &other) = delete;
/**
* @brief: move assignment - disabled
*/
Server& operator=( Server& other) = delete;
/**
* @brief: public getter - gets the singleton instance each call.
* @return: Cache manager instance.
*/
static Server& getInstance();
/**
* @brief: Initialize the cache manager.
*/
void initServer();
/**
* @brief: read client request from given socket into the buffer and
* write server response to this socket.
*/
static void handleClient( int sock);
/**
* @brief: the server receives the user request that begins with the
* command and it's arguments, parses request into tokens, identifies
* the command type and passes next to the relevant command function
* that will return the response string.
* @return: response string.
*/
std::string parseCommand( const std::string client, std::string request);
/* client requests from server */
/**
* @brief: register new client.
* @return: result of registration.
*/
std::string registerClient( const std::string client);
/**
* @brief: unregister existing client.
* @return: result of registration.
*/
std::string unregisterClient( const std::string client);
/**
* @brief: create new event
* @return: new event id.
*/
std::string createEvent( const std::string client, const std::string title,
const std::string date,const std::string description);
/**
* @brief: send RSVP to event
* @return: result of trying to register to event.
*/
std::string sendRSVP( const std::string client, int eventId);
/**
* @brief: EXIT command from client that terminates the server.
* exit command is legal even if no client was registered to the server yet.
*/
static void exitServer();
/**
* @brief: tries to get the guests list associated with the given eventId.
* @return: guests list response or and error response if invalid id.
*/
std::string getRSVP_List( const std::string client, int eventId);
/**
* @brief: gets the top five most recent new added events. in case there
* are less then five it returns those events.
* @return: events list as string.
*/
std::string getTop5Events( const std::string client);
private:
Logger *_logger;
int _nextEventId;
std::vector<Event*> _eventList;
std::set<std::string /*client name*/> _clientsSet;
std::map<int /*eventId*/, Event*> _eventsMap;
/**
* @brief: private Constructor - default ctor.
*/
Server();
/**
* @brief: checks if exists an event with given eventId.
* @return: true if there is event with this id.
*/
bool _isEventExist( int eventId);
/**
* @brief: checks if exists a client with given client name.
* @return: true if there is client with this client name.
*/
bool _isClientExist( const std::string client);
/**
* @brief: in case a client chose to unregister- the server removes
* it's name from the guests list of each event that he might have sent
* a RSVP request. events with empty guests list will remain.
*/
void _removeClientFromEvents( const std::string client);
};
#endif /* SERVER_H_ */