qabstractgrpcclient.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 <QTimer>
  27. namespace QtProtobuf {
  28. class QAbstractGrpcClientPrivate final {
  29. public:
  30. QAbstractGrpcClientPrivate(const QString &service) : service(service) {}
  31. std::shared_ptr<QAbstractGrpcChannel> channel;
  32. const QString service;
  33. };
  34. }
  35. using namespace QtProtobuf;
  36. QAbstractGrpcClient::QAbstractGrpcClient(const QString &service, QObject *parent) : QObject(parent)
  37. , d(std::make_unique<QAbstractGrpcClientPrivate>(service))
  38. {
  39. }
  40. QAbstractGrpcClient::~QAbstractGrpcClient()
  41. {}
  42. void QAbstractGrpcClient::attachChannel(const std::shared_ptr<QAbstractGrpcChannel> &channel)
  43. {
  44. d->channel = channel;
  45. }
  46. QGrpcStatus QAbstractGrpcClient::call(const QString &method, const QByteArray &arg, QByteArray &ret)
  47. {
  48. QGrpcStatus callStatus{QGrpcStatus::Unknown};
  49. if (d->channel) {
  50. callStatus = d->channel->call(method, d->service, arg, ret);
  51. } else {
  52. callStatus = QGrpcStatus{QGrpcStatus::Unknown, QLatin1String("No channel(s) attached.")};
  53. }
  54. if (callStatus != QGrpcStatus::Ok) {
  55. error(callStatus);
  56. }
  57. return callStatus;
  58. }
  59. QGrpcAsyncReply *QAbstractGrpcClient::call(const QString &method, const QByteArray &arg)
  60. {
  61. QGrpcAsyncReply *reply = nullptr;
  62. if (d->channel) {
  63. reply = new QGrpcAsyncReply(d->channel, this);
  64. connect(reply, &QGrpcAsyncReply::error, this, [this, reply](const QGrpcStatus &status) {
  65. error(status);
  66. reply->deleteLater();
  67. });
  68. connect(reply, &QGrpcAsyncReply::finished, this, [reply]() {
  69. reply->deleteLater();
  70. });
  71. d->channel->call(method, d->service, arg, reply);
  72. } else {
  73. error({QGrpcStatus::Unknown, QLatin1String("No channel(s) attached.")});
  74. }
  75. return reply;
  76. }
  77. void QAbstractGrpcClient::subscribe(const QString &method, const QByteArray &arg, const std::function<void(const QByteArray&)> &handler)
  78. {
  79. if (d->channel) {
  80. d->channel->subscribe(method, d->service, arg, this, handler);
  81. } else {
  82. error({QGrpcStatus::Unknown, QLatin1String("No channel(s) attached.")});
  83. }
  84. }