Rackspace2.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. /// \file Rackspace.h
  11. /// \brief Helper to class to manage Rackspace servers
  12. #include "NativeFeatureIncludes.h"
  13. #if _RAKNET_SUPPORT_TCPInterface==1
  14. #include "Export.h"
  15. #include "RakNetTypes.h"
  16. #include "DS_Queue.h"
  17. #include "RakString.h"
  18. #include "jansson.h"
  19. #ifndef __RACKSPACE_2_H
  20. #define __RACKSPACE_2_H
  21. namespace RakNet
  22. {
  23. class TCPInterface;
  24. class HTTPConnection2;
  25. struct Packet;
  26. enum Rackspace2ResponseCode
  27. {
  28. R2RC_AUTHENTICATED,
  29. R2RC_GOT_DOMAINS,
  30. R2RC_GOT_RECORDS,
  31. R2RC_GOT_SERVERS,
  32. R2RC_GOT_IMAGES,
  33. R2RC_NO_CONTENT,
  34. R2RC_BAD_JSON,
  35. R2RC_UNAUTHORIZED,
  36. R2RC_404_NOT_FOUND,
  37. R2RC_UNKNOWN,
  38. };
  39. /// \brief Callback interface to receive the results of operations
  40. class RAK_DLL_EXPORT Rackspace2EventCallback
  41. {
  42. public:
  43. virtual void OnTCPFailure(void)=0;
  44. virtual void OnTransmissionFailed(HTTPConnection2 *httpConnection2, RakString postStr, RakString authURLDomain)=0;
  45. virtual void OnResponse(Rackspace2ResponseCode r2rc, RakString responseReceived, int contentOffset)=0;
  46. virtual void OnEmptyResponse(RakString stringTransmitted)=0;
  47. virtual void OnMessage(const char *message, RakString responseReceived, RakString stringTransmitted, int contentOffset)=0;
  48. };
  49. /// \brief Version 2 of the code that uses the TCPInterface class to communicate with the Rackspace API servers
  50. /// Works with their "next-gen" API as of 2012
  51. /// API operations are hidden at http://docs.rackspace.com/servers/api/v2/cs-devguide/content/ch_api_operations.html
  52. /// \pre Compile RakNet with OPEN_SSL_CLIENT_SUPPORT set to 1
  53. /// Maintains its own TCPInterface class
  54. class RAK_DLL_EXPORT Rackspace2
  55. {
  56. public:
  57. Rackspace2();
  58. ~Rackspace2();
  59. // Call periodically
  60. void Update(void);
  61. /// Sets the callback to be used when an operation completes
  62. void SetEventCallback(Rackspace2EventCallback *callback);
  63. int GetCloudAccountNumber(void) const;
  64. const char *GetAuthToken(void) const;
  65. /// \brief Authenticate with Rackspace servers, required before executing any commands.
  66. /// \details All requests to authenticate and operate against Cloud Servers are performed using SSL over HTTP (HTTPS) on TCP port 443.
  67. /// TODO - Will reauthenticate automatically as needed
  68. /// \param[in] authenticationURL See http://docs.rackspace.com/cdns/api/v1.0/cdns-devguide/content/Authentication-d1e647.html
  69. /// \param[in] rackspaceCloudUsername Username you registered with Rackspace on their website
  70. /// \param[in] apiAccessKey Obtain your API access key from the Rackspace Cloud Control Panel in the Your Account API Access section.
  71. void Authenticate(const char *authenticationURL, const char *rackspaceCloudUsername, const char *apiAccessKey);
  72. enum OpType
  73. {
  74. OT_POST,
  75. OT_PUT,
  76. OT_GET,
  77. OT_DELETE,
  78. };
  79. /// \param[in] URL Path to command, for example https://identity.api.rackspacecloud.com/v2.0/tokens
  80. /// \param[in] opType Type of operation to perform
  81. /// \param[in] data If the operation requires data, put it here
  82. /// \param[in] setAuthToken true to automatically set the auth token for the operation. I believe this should be true for everything except authentication itself
  83. void AddOperation(RakNet::RakString URL, OpType opType, json_t *data, bool setAuthToken);
  84. struct Operation
  85. {
  86. RakNet::RakString URL;
  87. bool isPost;
  88. json_t *data;
  89. };
  90. protected:
  91. void AuthenticateInt(const char *authenticationURL, const char *rackspaceCloudUsername, const char *apiAccessKey);
  92. void Reauthenticate(void);
  93. char X_Auth_Token[128];
  94. Rackspace2EventCallback *eventCallback;
  95. DataStructures::Queue<Operation> operations;
  96. TCPInterface *tcp;
  97. HTTPConnection2 *httpConnection2;
  98. int cloudAccountNumber;
  99. RakString lastAuthenticationURL;
  100. RakString lastRackspaceCloudUsername;
  101. RakString lastApiAccessKey;
  102. bool reexecuteLastRequestOnAuth;
  103. //SystemAddress serverAddress;
  104. RakString __addOpLast_URL;
  105. OpType __addOpLast_isPost;
  106. RakString __addOpLast_dataAsStr;
  107. };
  108. } // namespace RakNet
  109. #endif // __RACKSPACE_API_H
  110. #endif // _RAKNET_SUPPORT_Rackspace
粤ICP备19079148号