PhysicsClientGRPC.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #ifdef BT_ENABLE_GRPC
  2. #include "PhysicsClientGRPC.h"
  3. #include "SharedMemory/grpc/proto/pybullet.grpc.pb.h"
  4. #include <grpc++/grpc++.h>
  5. using grpc::Channel;
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include "../Utils/b3Clock.h"
  9. #include "PhysicsClient.h"
  10. //#include "LinearMath/btVector3.h"
  11. #include "SharedMemoryCommands.h"
  12. #include <string>
  13. #include "Bullet3Common/b3Logging.h"
  14. #include "Bullet3Common/b3AlignedObjectArray.h"
  15. #include "SharedMemory/grpc/ConvertGRPCBullet.h"
  16. using pybullet_grpc::grpc::PyBulletAPI;
  17. static unsigned int b3DeserializeInt2(const unsigned char* input)
  18. {
  19. unsigned int tmp = (input[3] << 24) + (input[2] << 16) + (input[1] << 8) + input[0];
  20. return tmp;
  21. }
  22. bool gVerboseNetworkMessagesClient3 = false;
  23. struct GRPCNetworkedInternalData
  24. {
  25. std::shared_ptr<grpc::Channel> m_grpcChannel;
  26. std::unique_ptr<PyBulletAPI::Stub> m_stub;
  27. bool m_isConnected;
  28. SharedMemoryCommand m_clientCmd;
  29. bool m_hasCommand;
  30. SharedMemoryStatus m_lastStatus;
  31. b3AlignedObjectArray<char> m_stream;
  32. std::string m_hostName;
  33. int m_port;
  34. b3AlignedObjectArray<unsigned char> m_tempBuffer;
  35. double m_timeOutInSeconds;
  36. GRPCNetworkedInternalData()
  37. : m_isConnected(false),
  38. m_hasCommand(false),
  39. m_timeOutInSeconds(60)
  40. {
  41. }
  42. void disconnect()
  43. {
  44. if (m_isConnected)
  45. {
  46. m_stub = 0;
  47. m_grpcChannel = 0;
  48. m_isConnected = false;
  49. }
  50. }
  51. bool connectGRPC()
  52. {
  53. if (m_isConnected)
  54. return true;
  55. std::string hostport = m_hostName;
  56. if (m_port >= 0)
  57. {
  58. hostport += ':' + std::to_string(m_port);
  59. }
  60. m_grpcChannel = grpc::CreateChannel(
  61. hostport, grpc::InsecureChannelCredentials());
  62. m_stub = PyBulletAPI::NewStub(m_grpcChannel);
  63. // Set timeout for API
  64. std::chrono::system_clock::time_point deadline =
  65. std::chrono::system_clock::now() + std::chrono::seconds((long long)m_timeOutInSeconds);
  66. grpc::ClientContext context;
  67. context.set_deadline(deadline);
  68. ::pybullet_grpc::PyBulletCommand request;
  69. pybullet_grpc::CheckVersionCommand* cmd1 = request.mutable_checkversioncommand();
  70. cmd1->set_clientversion(SHARED_MEMORY_MAGIC_NUMBER);
  71. ::pybullet_grpc::PyBulletStatus response;
  72. // The actual RPC.
  73. grpc::Status status = m_stub->SubmitCommand(&context, request, &response);
  74. if (response.has_checkversionstatus())
  75. {
  76. if (response.checkversionstatus().serverversion() == SHARED_MEMORY_MAGIC_NUMBER)
  77. {
  78. m_isConnected = true;
  79. }
  80. else
  81. {
  82. printf("Error: Client version (%d) is different from server version (%d)", SHARED_MEMORY_MAGIC_NUMBER, response.checkversionstatus().serverversion());
  83. }
  84. }
  85. else
  86. {
  87. printf("Error: cannot connect to GRPC server\n");
  88. }
  89. return m_isConnected;
  90. }
  91. bool checkData()
  92. {
  93. bool hasStatus = false;
  94. return hasStatus;
  95. }
  96. };
  97. GRPCNetworkedPhysicsProcessor::GRPCNetworkedPhysicsProcessor(const char* hostName, int port)
  98. {
  99. m_data = new GRPCNetworkedInternalData;
  100. if (hostName)
  101. {
  102. m_data->m_hostName = hostName;
  103. }
  104. m_data->m_port = port;
  105. }
  106. GRPCNetworkedPhysicsProcessor::~GRPCNetworkedPhysicsProcessor()
  107. {
  108. disconnect();
  109. delete m_data;
  110. }
  111. bool GRPCNetworkedPhysicsProcessor::processCommand(const struct SharedMemoryCommand& clientCmd, struct SharedMemoryStatus& serverStatusOut, char* bufferServerToClient, int bufferSizeInBytes)
  112. {
  113. if (gVerboseNetworkMessagesClient3)
  114. {
  115. printf("GRPCNetworkedPhysicsProcessor::processCommand\n");
  116. }
  117. ::pybullet_grpc::PyBulletCommand grpcCommand;
  118. pybullet_grpc::PyBulletCommand* grpcCmdPtr = convertBulletToGRPCCommand(clientCmd, grpcCommand);
  119. if (grpcCmdPtr)
  120. {
  121. grpc::ClientContext context;
  122. std::chrono::system_clock::time_point deadline =
  123. std::chrono::system_clock::now() + std::chrono::seconds((long long)m_data->m_timeOutInSeconds);
  124. context.set_deadline(deadline);
  125. ::pybullet_grpc::PyBulletStatus status;
  126. // The actual RPC.
  127. grpc::Status grpcStatus = m_data->m_stub->SubmitCommand(&context, grpcCommand, &status);
  128. //convert grpc status to Bullet status
  129. bool convertedOk = convertGRPCToStatus(status, serverStatusOut, bufferServerToClient, bufferSizeInBytes);
  130. if (!convertedOk)
  131. {
  132. disconnect();
  133. }
  134. return convertedOk;
  135. }
  136. return false;
  137. }
  138. bool GRPCNetworkedPhysicsProcessor::receiveStatus(struct SharedMemoryStatus& serverStatusOut, char* bufferServerToClient, int bufferSizeInBytes)
  139. {
  140. bool hasStatus = m_data->checkData();
  141. if (hasStatus)
  142. {
  143. if (gVerboseNetworkMessagesClient3)
  144. {
  145. printf("GRPCNetworkedPhysicsProcessor::receiveStatus\n");
  146. }
  147. serverStatusOut = m_data->m_lastStatus;
  148. int numStreamBytes = m_data->m_stream.size();
  149. if (numStreamBytes < bufferSizeInBytes)
  150. {
  151. for (int i = 0; i < numStreamBytes; i++)
  152. {
  153. bufferServerToClient[i] = m_data->m_stream[i];
  154. }
  155. }
  156. else
  157. {
  158. printf("Error: steam buffer overflow\n");
  159. }
  160. }
  161. return hasStatus;
  162. }
  163. void GRPCNetworkedPhysicsProcessor::renderScene(int renderFlags)
  164. {
  165. }
  166. void GRPCNetworkedPhysicsProcessor::physicsDebugDraw(int debugDrawFlags)
  167. {
  168. }
  169. void GRPCNetworkedPhysicsProcessor::setGuiHelper(struct GUIHelperInterface* guiHelper)
  170. {
  171. }
  172. bool GRPCNetworkedPhysicsProcessor::isConnected() const
  173. {
  174. return m_data->m_isConnected;
  175. }
  176. bool GRPCNetworkedPhysicsProcessor::connect()
  177. {
  178. bool isConnected = m_data->connectGRPC();
  179. return isConnected;
  180. }
  181. void GRPCNetworkedPhysicsProcessor::disconnect()
  182. {
  183. m_data->disconnect();
  184. }
  185. void GRPCNetworkedPhysicsProcessor::setTimeOut(double timeOutInSeconds)
  186. {
  187. m_data->m_timeOutInSeconds = timeOutInSeconds;
  188. }
  189. #endif //BT_ENABLE_GRPC