Skip to content

Commit 60e8d68

Browse files
committed
Implemented WsjcppJsonRpc20WebSocketServer
1 parent c2cf4b4 commit 60e8d68

File tree

2 files changed

+101
-27
lines changed

2 files changed

+101
-27
lines changed

src/wsjcpp_jsonrpc20.cpp

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@ nlohmann::json WsjcppJsonRpc20Error::toJson() {
3333
}
3434

3535
// ---------------------------------------------------------------------
36-
37-
/*!
38-
* WsjcppJsonRpc20UserSession - all data by current user session
39-
* */
36+
// WsjcppJsonRpc20UserSession - all data by current user session
4037

4138
WsjcppJsonRpc20UserSession::WsjcppJsonRpc20UserSession() {
4239
TAG = "WsjcppJsonRpc20UserSession";
@@ -388,6 +385,77 @@ void WsjcppJsonRpc20WebSocketClient::unsetUserSession() {
388385
m_pUserSession = nullptr;
389386
}
390387

388+
// ---------------------------------------------------------------------
389+
// WsjcppJsonRpc20WebSocketServer
390+
391+
WsjcppJsonRpc20WebSocketServer::WsjcppJsonRpc20WebSocketServer() {
392+
TAG = "WsjcppJsonRpc20WebSocketServer";
393+
}
394+
395+
// ---------------------------------------------------------------------
396+
397+
void WsjcppJsonRpc20WebSocketServer::onWebSocketConnected(void *pClient, WsjcppJsonRpc20WebSocketClient *pWebSocketClient) {
398+
m_mapClients[pClient] = pWebSocketClient;
399+
WsjcppLog::info(TAG, "NewConnection url=" + pWebSocketClient->getRequestUrl() + " "
400+
+ pWebSocketClient->getPeerIpAddress()
401+
+ ":" + std::to_string(pWebSocketClient->getPeerPort()));
402+
403+
nlohmann::json jsonResult;
404+
jsonResult["method"] = "notify";
405+
jsonResult["id"] = "n0";
406+
nlohmann::json jsonServer;
407+
jsonServer["app_name"] = WSJCPP_APP_NAME;
408+
jsonServer["app_version"] = WSJCPP_APP_VERSION;
409+
jsonResult["result"] = jsonServer;
410+
pWebSocketClient->sendTextMessage(jsonResult.dump());
411+
}
412+
413+
// ---------------------------------------------------------------------
414+
415+
void WsjcppJsonRpc20WebSocketServer::onWebSocketDisconnected(void *pClient) {
416+
// TODO fix memoryleak clean usersession if need - smart pointer
417+
418+
std::map<void *, WsjcppJsonRpc20WebSocketClient *>::iterator it;
419+
it = m_mapClients.find(pClient);
420+
m_mapClients.erase(it);
421+
delete m_mapClients[pClient];
422+
}
423+
424+
// ---------------------------------------------------------------------
425+
426+
int WsjcppJsonRpc20WebSocketServer::getConnectedClients() {
427+
return m_mapClients.size();
428+
}
429+
430+
// ---------------------------------------------------------------------
431+
432+
WsjcppJsonRpc20WebSocketClient *WsjcppJsonRpc20WebSocketServer::findWebSocketClient(void *pClient) {
433+
WsjcppJsonRpc20WebSocketClient *pRet = nullptr;
434+
std::map<void *, WsjcppJsonRpc20WebSocketClient *>::iterator it;
435+
it = m_mapClients.find(pClient);
436+
if (it != m_mapClients.end()) {
437+
pRet = it->second;
438+
}
439+
return pRet;
440+
}
441+
442+
// ---------------------------------------------------------------------
443+
444+
void WsjcppJsonRpc20WebSocketServer::sendMessageToAll(const nlohmann::json& jsonMessage) {
445+
std::string sTextMessage = jsonMessage.dump();
446+
std::map<void *, WsjcppJsonRpc20WebSocketClient *>::iterator it;
447+
for (it = m_mapClients.begin(); it != m_mapClients.end(); ++it) {
448+
it->second->sendTextMessage(sTextMessage);
449+
}
450+
}
451+
452+
// ---------------------------------------------------------------------
453+
454+
void WsjcppJsonRpc20WebSocketServer::sendMessageToOne(WsjcppJsonRpc20WebSocketClient *pClient, const nlohmann::json &jsonMessage) {
455+
std::string sTextMessage = jsonMessage.dump();
456+
pClient->sendTextMessage(sTextMessage);
457+
}
458+
391459
// ---------------------------------------------------------------------
392460
// WsjcppJsonRpc20ParamDef
393461

@@ -623,4 +691,4 @@ const std::vector<WsjcppValidatorJsonBase *> &WsjcppJsonRpc20ParamDef::listOfJso
623691
return m_vValidatorsJson;
624692
}
625693

626-
// ---------------------------------------------------------------------
694+
// ---------------------------------------------------------------------

src/wsjcpp_jsonrpc20.h

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class WsjcppJsonRpc20Error;
1111
class WsjcppJsonRpc20UserSession;
1212
class WsjcppJsonRpc20ParamDef;
13-
class WsjcppJsonRpc20WebSocket;
13+
class WsjcppJsonRpc20WebSocketClient;
1414
class WsjcppJsonRpc20WebSocketServer;
1515
class WsjcppJsonRpc20ParamDef;
1616
class WsjcppJsonRpc20Request;
@@ -94,15 +94,21 @@ class WsjcppJsonRpc20UserSession {
9494
};
9595

9696
// ---------------------------------------------------------------------
97-
// WsjcppJsonRpc20WebSocket
97+
// WsjcppJsonRpc20WebSocketClient
9898

9999
class WsjcppJsonRpc20WebSocketClient {
100100
public:
101101
WsjcppJsonRpc20WebSocketClient();
102102
void setUserSession(WsjcppJsonRpc20UserSession *pUserSession);
103103
WsjcppJsonRpc20UserSession *getUserSession();
104104
void unsetUserSession();
105-
105+
virtual void onDisconnected() = 0;
106+
virtual std::string getPeerIpAddress() = 0;
107+
virtual int getPeerPort() = 0;
108+
virtual std::string getRequestUrl() = 0;
109+
110+
virtual void sendTextMessage(const std::string &sTextMessage) = 0;
111+
106112
protected:
107113
std::string TAG;
108114

@@ -112,26 +118,27 @@ class WsjcppJsonRpc20WebSocketClient {
112118

113119
// ---------------------------------------------------------------------
114120
// WsjcppJsonRpc20WebSocketServer
115-
/*
121+
116122
class WsjcppJsonRpc20WebSocketServer {
117123
public:
118-
virtual void sendMessage(
119-
WsjcppJsonRpc20WebSocket *pClient,
120-
const nlohmann::json& jsonResponse
121-
) = 0;
122-
virtual void sendMessageError(
123-
WsjcppJsonRpc20WebSocket *pClient,
124-
const std::string &sCmd,
125-
const std::string & sM,
126-
WsjcppJsonRpc20Error error
127-
) = 0;
128-
virtual void sendToAll(const nlohmann::json& jsonMessage) = 0;
129-
virtual void sendToOne(WsjcppJsonRpc20WebSocket *pClient, const nlohmann::json &jsonMessage) = 0;
130-
virtual int getConnectedUsers() = 0;
131-
virtual void setWsjcppJsonRpc20UserSession(WsjcppJsonRpc20WebSocket *pClient, WsjcppJsonRpc20UserSession *pUserSession) = 0;
132-
virtual WsjcppJsonRpc20UserSession *getWsjcppJsonRpc20UserSession(WsjcppJsonRpc20WebSocket *pClient) = 0;
124+
WsjcppJsonRpc20WebSocketServer();
125+
126+
127+
void onWebSocketConnected(void *pClient, WsjcppJsonRpc20WebSocketClient *pWebSocketClient);
128+
void onWebSocketDisconnected(void *pClient);
129+
int getConnectedClients();
130+
// TODO int getConnectedUsers();
131+
132+
WsjcppJsonRpc20WebSocketClient *findWebSocketClient(void *pClient);
133+
void sendMessageToAll(const nlohmann::json& jsonMessage);
134+
void sendMessageToOne(WsjcppJsonRpc20WebSocketClient *pClient, const nlohmann::json &jsonMessage);
135+
136+
protected:
137+
std::string TAG;
138+
139+
private:
140+
std::map<void *, WsjcppJsonRpc20WebSocketClient *> m_mapClients;
133141
};
134-
*/
135142

136143
/*!
137144
* WsjcppJsonRpc20ParamDef - helper api for define input params and descrip it for docs.
@@ -221,7 +228,6 @@ class WsjcppJsonRpc20Request {
221228
std::string command();
222229
bool hasCommand();
223230
void sendMessageError(const std::string &cmd, WsjcppJsonRpc20Error error);
224-
void sendMessageSuccess(const std::string &cmd, nlohmann::json& jsonResponse);
225231
void sendResponse(nlohmann::json& jsonResult);
226232
227233
// bool validateInputParameters(Error &error, CmdHandlerBase *pCmdHandler);
@@ -322,4 +328,4 @@ class WsjcppJsonRpc20ServerApi : public WsjcppJsonRpc20Base {
322328
virtual void handle(WsjcppJsonRpc20Request *pRequest);
323329
};
324330
*/
325-
#endif // WSJCPP_JSONRPC20
331+
#endif // WSJCPP_JSONRPC20

0 commit comments

Comments
 (0)