main.cpp 7.4 KB

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