qabstractgrpcclient.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of QtProtobuf project https://git.semlanik.org/semlanik/qtprotobuf
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "qabstractgrpcclient.h"
  26. #include "qgrpcasyncreply.h"
  27. #include "qprotobufserializerregistry_p.h"
  28. #include <QTimer>
  29. namespace QtProtobuf {
  30. class QAbstractGrpcClientPrivate final {
  31. public:
  32. QAbstractGrpcClientPrivate(const QString &service) : service(service) {
  33. serializer = QProtobufSerializerRegistry::instance().getSerializer("protobuf");
  34. }
  35. std::shared_ptr<QAbstractGrpcChannel> channel;
  36. const QString service;
  37. std::shared_ptr<QAbstractProtobufSerializer> serializer;
  38. };
  39. }
  40. using namespace QtProtobuf;
  41. QAbstractGrpcClient::QAbstractGrpcClient(const QString &service, QObject *parent) : QObject(parent)
  42. , d_ptr(std::make_unique<QAbstractGrpcClientPrivate>(service))
  43. {
  44. }
  45. QAbstractGrpcClient::~QAbstractGrpcClient()
  46. {}
  47. void QAbstractGrpcClient::attachChannel(const std::shared_ptr<QAbstractGrpcChannel> &channel)
  48. {
  49. d_ptr->channel = channel;
  50. d_ptr->serializer = channel->serializer();
  51. }
  52. QGrpcStatus QAbstractGrpcClient::call(const QString &method, const QByteArray &arg, QByteArray &ret)
  53. {
  54. QGrpcStatus callStatus{QGrpcStatus::Unknown};
  55. if (d_ptr->channel) {
  56. callStatus = d_ptr->channel->call(method, d_ptr->service, arg, ret);
  57. } else {
  58. callStatus = QGrpcStatus{QGrpcStatus::Unknown, QLatin1String("No channel(s) attached.")};
  59. }
  60. if (callStatus != QGrpcStatus::Ok) {
  61. error(callStatus);
  62. }
  63. return callStatus;
  64. }
  65. QGrpcAsyncReply *QAbstractGrpcClient::call(const QString &method, const QByteArray &arg)
  66. {
  67. QGrpcAsyncReply *reply = nullptr;
  68. if (d_ptr->channel) {
  69. reply = new QGrpcAsyncReply(d_ptr->channel, this);
  70. connect(reply, &QGrpcAsyncReply::error, this, [this, reply](const QGrpcStatus &status) {
  71. error(status);
  72. reply->deleteLater();
  73. });
  74. connect(reply, &QGrpcAsyncReply::finished, this, [reply]() {
  75. reply->deleteLater();
  76. });
  77. d_ptr->channel->call(method, d_ptr->service, arg, reply);
  78. } else {
  79. error({QGrpcStatus::Unknown, QLatin1String("No channel(s) attached.")});
  80. }
  81. return reply;
  82. }
  83. void QAbstractGrpcClient::subscribe(const QString &method, const QByteArray &arg, const std::function<void(const QByteArray&)> &handler)
  84. {
  85. if (d_ptr->channel) {
  86. d_ptr->channel->subscribe(method, d_ptr->service, arg, this, handler);
  87. } else {
  88. error({QGrpcStatus::Unknown, QLatin1String("No channel(s) attached.")});
  89. }
  90. }
  91. QAbstractProtobufSerializer *QAbstractGrpcClient::serializer() const
  92. {
  93. return d_ptr->serializer.get();
  94. }