Samchon Framework for CPP  1.0.0
IPUserPair.cpp
1 #include <samchon/protocol/service/IPUserPair.hpp>
2 # include <samchon/protocol/service/Server.hpp>
3 # include <samchon/protocol/service/User.hpp>
4 
5 #include <boost/asio.hpp>
6 #include <mutex>
7 #include <thread>
8 #include <sstream>
9 
10 #include <samchon/library/Datetime.hpp>
11 #include <samchon/library/XML.hpp>
12 #include <samchon/protocol/Invoke.hpp>
13 #include <samchon/protocol/InvokeParameter.hpp>
14 
15 using namespace std;
16 
17 using namespace samchon;
18 using namespace samchon::library;
19 using namespace samchon::protocol;
20 using namespace samchon::protocol::service;
21 
22 using namespace boost::asio;
23 using namespace boost::asio::ip;
24 
25 IPUserPair::IPUserPair(Server *server, const string &ip)
26 {
27  this->server = server;
28  this->ip = ip;
29 }
30 auto IPUserPair::getSessionID(Socket *socket, size_t sequence) -> std::string
31 {
32  std::string sessionID;
33 
34  //GET SESSION_ID FROM CLIENT'S COOKIE
35  {
36  boost::system::error_code error;
37 
38  //GET SESSION ID
39  vector<unsigned char> piece(1000, NULL);
40  socket->read_some(boost::asio::buffer(piece), error);
41 
42  if (error)
43  return "";
44 
45  std::string str((char*)piece.data());
46  shared_ptr<XML> xml(new XML(str));
47 
48  shared_ptr<Invoke> invoke(new Invoke());
49  invoke->construct(xml);
50 
51  sessionID = invoke->at(0)->getValue<string>();
52  }
53 
54  UniqueReadLock uk(server->mtx);
55  if (sessionID.empty() == true || server->has(sessionID) == false)
56  {
57  //ISSUE SESSION ID
58  sessionID = issueSessionID(sequence);
59 
60  //IF SESSION_ID IS NOT UNIQUE
61  if (server->has(sessionID) == true)
62  throw runtime_error("Session ID is not unique");
63 
64  uk.unlock();
65 
66  //NOTIFY TO CLIENT
67  shared_ptr<Invoke> invoke( new Invoke("notifySessionID") );
68  invoke->emplace_back( new InvokeParameter("id", sessionID) );
69 
70  std::string &data = invoke->toXML()->toString();
71  boost::system::error_code error;
72 
73  socket->write_some(boost::asio::buffer(data), error);
74  if (error)
75  return "";
76  }
77 
78  return sessionID;
79 }
80 
81 auto IPUserPair::issueSessionID(size_t sequence) const -> std::string
82 {
83  std::string &name = server->NAME();
84  int port = server->PORT();
85  long long linuxTime = Datetime().toLinuxTime();
86 
87  stringstream sstream;
88  sstream << name << "::"
89  << hex << port << "::"
90  << hex << sequence << "::"
91  << hex << linuxTime;
92 
93  return sstream.str();
94 }
auto toLinuxTime() const -> long long
Converts the Date to linux_time.
Definition: Date.cpp:214
Definition: RWMutex.hpp:4
Package of libraries.
Definition: library.hpp:84
Unique lock for reading.
Package of cloud service as a server.
Package of network protocol and libraries.
Definition: protocol.hpp:185
A server for (cloud) service.
Definition: Server.hpp:48
Standard message of network I/O.
Definition: Invoke.hpp:47
XML is a class representing xml object.
Definition: XML.hpp:72
A parameter of an Invoke.
Top level namespace of products built from samchon.
Definition: ByteArray.hpp:7