Overte C++ Documentation
EntityScriptClient.h
1 //
2 // EntityScriptClient.h
3 // libraries/script-engine/src
4 //
5 // Created by Ryan Huffman on 2017/01/13
6 // Copyright 2017 High Fidelity, Inc.
7 // Copyright 2023 Overte e.V.
8 //
9 // Distributed under the Apache License, Version 2.0.
10 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
11 // SPDX-License-Identifier: Apache-2.0
12 //
13 
14 #ifndef hifi_EntityScriptClient_h
15 #define hifi_EntityScriptClient_h
16 
17 #include <QtCore/QSharedPointer>
18 
19 #include <ClientServerUtils.h>
20 #include <LimitedNodeList.h>
21 #include <ReceivedMessage.h>
22 #include <AssetUtils.h>
23 #include "EntityScriptUtils.h"
24 
25 #include <DependencyManager.h>
26 #include <unordered_map>
27 
28 using GetScriptStatusCallback = std::function<void(bool responseReceived, bool isRunning, EntityScriptStatus status, QString errorInfo)>;
29 
30 class GetScriptStatusRequest : public QObject {
31  Q_OBJECT
32 public:
33  GetScriptStatusRequest(QUuid);
34  ~GetScriptStatusRequest();
35 
36  Q_INVOKABLE void start();
37 
38  bool getResponseReceived() const { return _responseReceived; }
39  bool getIsRunning() const { return _isRunning; }
40  EntityScriptStatus getStatus() const { return _status; }
41  QString getErrorInfo() const { return _errorInfo; }
42 
43 signals:
44  void finished(GetScriptStatusRequest* request);
45 
46 private:
47  QUuid _entityID;
48  MessageID _messageID;
49 
50  bool _responseReceived;
51  bool _isRunning;
52  EntityScriptStatus _status;
53  QString _errorInfo;
54 };
55 
56 class EntityScriptClient : public QObject, public Dependency {
57  Q_OBJECT
58 public:
59  EntityScriptClient();
60 
61  Q_INVOKABLE GetScriptStatusRequest* createScriptStatusRequest(QUuid entityID);
62 
63  bool reloadServerScript(QUuid entityID);
64  MessageID getEntityServerScriptStatus(QUuid entityID, GetScriptStatusCallback callback);
65  void callEntityServerMethod(QUuid id, const QString& method, const QStringList& params);
66 
67 
68 private slots:
69  void handleNodeKilled(SharedNodePointer node);
70  void handleNodeClientConnectionReset(SharedNodePointer node);
71 
72  void handleGetScriptStatusReply(QSharedPointer<ReceivedMessage> message, SharedNodePointer senderNode);
73 
74 private:
75  static MessageID _currentID;
76  std::unordered_map<SharedNodePointer, std::unordered_map<MessageID, GetScriptStatusCallback>> _pendingEntityScriptStatusRequests;
77 
78  void forceFailureOfPendingRequests(SharedNodePointer node);
79 };
80 
81 
82 class EntityScriptServerServices : public QObject, public Dependency {
83  Q_OBJECT
84 public:
85  void callEntityClientMethod(QUuid clientSessionID, QUuid entityID, const QString& method, const QStringList& params);
86 };
87 
88 #endif