main.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <vector>
  5. #include <thread>
  6. #include <grpc++/grpc++.h>
  7. #include <addressbook.pb.h>
  8. #include <addressbook.grpc.pb.h>
  9. using namespace ::qtprotobuf::examples;
  10. class ContactsHandler;
  11. class CallHandler;
  12. class AddressBookService final : public AddressBook::WithAsyncMethod_callStatus<AddressBook::WithAsyncMethod_contacts<AddressBook::Service>> {
  13. public:
  14. PhoneNumber m_lastPhone;
  15. CallStatus m_lastCallStatus;
  16. Contacts m_contacts;
  17. std::vector<::grpc::ServerAsyncWriter<Contacts> *> m_clients;
  18. std::vector<::grpc::ServerAsyncWriter<CallStatus> *> m_callClients;
  19. AddressBookService(): m_clients({}) {
  20. Contact *contact = m_contacts.add_list();
  21. contact->set_firstname("John");
  22. contact->set_lastname("Doe");
  23. contact = m_contacts.add_list();
  24. contact->set_firstname("Jane");
  25. contact->set_lastname("Doe");
  26. contact = m_contacts.add_list();
  27. contact->set_firstname("John");
  28. contact->set_lastname("McClane");
  29. contact = m_contacts.add_list();
  30. contact->set_firstname("Alexey");
  31. contact->set_lastname("Edelev");
  32. contact = m_contacts.add_list();
  33. contact->set_firstname("Ebenezer");
  34. contact->set_lastname("Scrooge");
  35. Job *job = new Job;
  36. job->set_title("Job title");
  37. contact->set_allocated_job(job);
  38. PhoneNumber *home = (*contact->mutable_phones()).Add();
  39. home->set_countrycode(49);
  40. home->set_number(12324534679);
  41. m_lastCallStatus.set_allocated_phonenumber(new PhoneNumber);
  42. m_lastCallStatus.set_status(CallStatus::Inactive);
  43. }
  44. ~AddressBookService() = default;
  45. void updateContacts() {
  46. for (unsigned int i = 0; i < (m_clients.size() - 1); i++) {
  47. m_clients[i]->Write(m_contacts, nullptr);
  48. }
  49. }
  50. void registerWriter(ContactsHandler *handler);
  51. void registerCallStatusHandler(CallHandler *handler);
  52. ::grpc::Status addContact(::grpc::ServerContext *context, const Contact *request, Contacts *response) override
  53. {
  54. bool isUserOk = false;
  55. bool isPasswordOk = false;
  56. for (auto it = context->client_metadata().begin(); it != context->client_metadata().end(); ++it) {
  57. if ((*it).first == std::string("user-name") && (*it).second == "authorizedUser") {
  58. isUserOk = true;
  59. }
  60. if ((*it).first == std::string("user-password") && (*it).second == "098f6bcd4621d373cade4e832627b4f6") {
  61. isPasswordOk = true;
  62. }
  63. std::cout << (*it).first << (*it).second << std::endl;
  64. }
  65. if (!isUserOk || !isPasswordOk) {
  66. return ::grpc::Status(::grpc::StatusCode::UNAUTHENTICATED, grpc::string("Invalid user or password"));
  67. }
  68. std::cout << "addContact called" << std::endl;
  69. Contact *newContact = m_contacts.add_list();
  70. *newContact = *request;
  71. *response = m_contacts;
  72. updateContacts();
  73. return ::grpc::Status();
  74. }
  75. ::grpc::Status makeCall(grpc::ServerContext *, const PhoneNumber *request, CallStatus *response) override
  76. {
  77. m_lastPhone = *request;
  78. for (unsigned int i = 0; i < (m_callClients.size() - 1); i++) {
  79. response->set_status(CallStatus::Active);
  80. PhoneNumber *phoneNumber = new PhoneNumber(*request);
  81. response->set_allocated_phonenumber(phoneNumber);
  82. m_lastCallStatus = *response;
  83. m_callClients[i]->Write(m_lastCallStatus, nullptr);
  84. }
  85. return ::grpc::Status();
  86. }
  87. ::grpc::Status endCall(grpc::ServerContext *, const None *, None *) override
  88. {
  89. if (m_lastCallStatus.status() != CallStatus::Active) {
  90. return ::grpc::Status();
  91. }
  92. std::cout << "Call ended" << std::endl;
  93. m_lastCallStatus.set_status(CallStatus::Ended);
  94. for (unsigned int i = 0; i < (m_callClients.size() - 1); i++) {
  95. m_callClients[i]->Write(m_lastCallStatus, nullptr);
  96. }
  97. std::this_thread::sleep_for(std::chrono::seconds(1));
  98. std::cout << "Call ended" << std::endl;
  99. m_lastCallStatus.set_status(CallStatus::Inactive);
  100. for (unsigned int i = 0; i < (m_callClients.size() - 1); i++) {
  101. m_callClients[i]->Write(m_lastCallStatus, nullptr);
  102. }
  103. return ::grpc::Status();
  104. }
  105. ::grpc::Status removeContact(::grpc::ServerContext *, const Contact *, Contacts *) override
  106. {
  107. std::cout << "removeContact called" << std::endl;
  108. updateContacts();
  109. return ::grpc::Status(::grpc::UNIMPLEMENTED, "Unimplemented");
  110. }
  111. ::grpc::Status navigateTo(::grpc::ServerContext *, const Address *, None *) override
  112. {
  113. std::cout << "navigateTo called" << std::endl;
  114. return ::grpc::Status(::grpc::UNIMPLEMENTED, "Unimplemented");
  115. }
  116. };
  117. class ContactsHandler {
  118. public:
  119. ContactsHandler(AddressBookService *service, ::grpc::ServerCompletionQueue *cq) : tag_(0xdeadbeef)
  120. , writer_(&ctx_)
  121. , cq_(cq)
  122. {
  123. service->Requestcontacts(&ctx_, &request_, &writer_, cq_, cq_, &tag_);
  124. service->registerWriter(this);
  125. }
  126. unsigned int tag_;
  127. grpc::ServerContext ctx_;
  128. ListFrame request_;
  129. ::grpc::ServerAsyncWriter< ::qtprotobuf::examples::Contacts> writer_;
  130. ::grpc::ServerCompletionQueue *cq_;
  131. };
  132. class CallHandler {
  133. public:
  134. CallHandler(AddressBookService *service, ::grpc::ServerCompletionQueue *cq) : tag_(0xdeadbeee)
  135. , writer_(&ctx_)
  136. , cq_(cq)
  137. {
  138. service->RequestcallStatus(&ctx_, &request_, &writer_, cq_, cq_, &tag_);
  139. service->registerCallStatusHandler(this);
  140. }
  141. int tag_;
  142. grpc::ServerContext ctx_;
  143. None request_;
  144. ::grpc::ServerAsyncWriter< ::qtprotobuf::examples::CallStatus> writer_;
  145. ::grpc::ServerCompletionQueue *cq_;
  146. };
  147. void AddressBookService::registerWriter(ContactsHandler *handler) {
  148. m_clients.push_back(&(handler->writer_));
  149. }
  150. void AddressBookService::registerCallStatusHandler(CallHandler *handler) {
  151. m_callClients.push_back(&(handler->writer_));
  152. }
  153. int main(int, char *[])
  154. {
  155. std::string server_address("localhost:65001");
  156. AddressBookService service;
  157. std::ifstream tfile("cert.pem");
  158. std::stringstream cert;
  159. cert << tfile.rdbuf();
  160. tfile.close();
  161. tfile.open("key.pem");
  162. std::stringstream key;
  163. key << tfile.rdbuf();
  164. tfile.close();
  165. grpc::ServerBuilder builder;
  166. grpc::SslServerCredentialsOptions opts(GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE);
  167. opts.pem_key_cert_pairs.push_back({key.str(), cert.str()});
  168. builder.AddListeningPort(server_address, grpc::SslServerCredentials(opts));
  169. builder.RegisterService(&service);
  170. std::unique_ptr<grpc::ServerCompletionQueue> cq = builder.AddCompletionQueue();
  171. std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
  172. std::cout << "Server listening on " << server_address << std::endl;
  173. ContactsHandler *last = new ContactsHandler(&service, cq.get());
  174. CallHandler *lastCall = new CallHandler(&service, cq.get());
  175. while (true) {
  176. unsigned int *tag;
  177. bool ok;
  178. cq->Next(reinterpret_cast<void**>(&tag), &ok);
  179. if (tag == nullptr) {
  180. std::cout << "Some request";
  181. continue;
  182. }
  183. if ((*tag) == 0xdeadbeef) {
  184. last->writer_.Write(service.m_contacts, nullptr);
  185. last = new ContactsHandler(&service, cq.get());
  186. }
  187. if ((*tag) == 0xdeadbeee) {
  188. lastCall->writer_.Write(service.m_lastCallStatus, nullptr);
  189. lastCall = new CallHandler(&service, cq.get());
  190. }
  191. }
  192. }