p≡p for Python
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.7 KiB

4 years ago
4 years ago
3 years ago
4 years ago
  1. // This file is under GNU Affero General Public License 3.0
  2. // see LICENSE.txt
  3. #include "user_interface.hh"
  4. #include <assert.h>
  5. #include "adapter.hh"
  6. namespace pEp {
  7. namespace PythonAdapter {
  8. Adapter::Adapter(bool unregister_this)
  9. : flag_unregister(unregister_this)
  10. {
  11. session(init);
  12. }
  13. Adapter::~Adapter()
  14. {
  15. session(release);
  16. }
  17. PEP_SESSION Adapter::session(session_action action)
  18. {
  19. lock_guard<mutex> lock(mtx());
  20. thread_local static PEP_SESSION _session = nullptr;
  21. thread_local int booked = 0;
  22. PEP_STATUS status = PEP_STATUS_OK;
  23. switch (action) {
  24. case release:
  25. if (booked)
  26. --booked;
  27. if (!booked && _session) {
  28. ::release(_session);
  29. _session = nullptr;
  30. }
  31. break;
  32. case none:
  33. if (_session)
  34. break;
  35. case init:
  36. ++booked;
  37. if (!_session)
  38. status = ::init(&_session, _messageToSend, _inject_sync_event, _ensure_passphrase);
  39. // status = ::init(&_session, _messageToSend, _inject_sync_event);
  40. break;
  41. default:
  42. status = PEP_ILLEGAL_VALUE;
  43. }
  44. if (status)
  45. _throw_status(status);
  46. return _session;
  47. }
  48. ::utility::locked_queue< SYNC_EVENT > * Adapter::q = nullptr;
  49. bool Adapter::flag_sync_enabled = false;
  50. void Adapter::shutdown_sync()
  51. {
  52. if (queue_active())
  53. queue().push_front(nullptr);
  54. flag_sync_enabled = false;
  55. }
  56. PyObject *Adapter::ui_object(PyObject *value)
  57. {
  58. lock_guard<mutex> lock(mtx());
  59. static PyObject *obj = nullptr;
  60. if (value)
  61. obj = value;
  62. return obj;
  63. }
  64. // int Adapter::_inject_sync_event(SYNC_EVENT ev, void *management)
  65. // {
  66. // if (!flag_sync_enabled)
  67. // return 1;
  68. //
  69. // if (is_sync_thread(adapter.session())) {
  70. // PEP_STATUS status = do_sync_protocol_step(adapter.session(), adapter.ui_object(), ev);
  71. // return status == PEP_STATUS_OK ? 0 : 1;
  72. // }
  73. //
  74. // try {
  75. // queue().push_back(ev);
  76. // }
  77. // catch (exception&) {
  78. // return 1;
  79. // }
  80. // return 0;
  81. // }
  82. }
  83. }