connection.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # Copyright 2021 EMQ Technologies Co., Ltd.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import logging
  15. import time
  16. from typing import Callable
  17. from pynng import Req0, Push0, Pull0, Timeout
  18. class PairChannel:
  19. def __init__(self, name: str, typ: int):
  20. s = Req0(resend_time=0)
  21. """TODO options"""
  22. if typ == 0:
  23. url = "ipc:///tmp/plugin_{}.ipc".format(name)
  24. else:
  25. url = "ipc:///tmp/func_{}.ipc".format(name)
  26. try:
  27. dial_with_retry(s, url)
  28. except Exception as e:
  29. print(e)
  30. exit(0)
  31. self.sock = s
  32. """ run this in a new thread"""
  33. def run(self, reply_func: Callable[[bytes], bytes]):
  34. self.sock.send(b'handshake')
  35. while True:
  36. try:
  37. msg = self.sock.recv()
  38. reply = reply_func(msg)
  39. self.sock.send(reply)
  40. except Timeout:
  41. print('pair timeout')
  42. pass
  43. def close(self):
  44. self.sock.close()
  45. class SourceChannel:
  46. def __init__(self, meta: dict):
  47. s = Push0(send_timeout=1000)
  48. url = "ipc:///tmp/{}_{}_{}.ipc".format(meta['ruleId'], meta['opId'], meta['instanceId'])
  49. logging.info(url)
  50. dial_with_retry(s, url)
  51. self.sock = s
  52. def send(self, data: bytes):
  53. self.sock.send(data)
  54. def close(self):
  55. self.sock.close()
  56. class SinkChannel:
  57. def __init__(self, meta: dict):
  58. s = Pull0()
  59. url = "ipc:///tmp/{}_{}_{}.ipc".format(meta['ruleId'], meta['opId'], meta['instanceId'])
  60. logging.info(url)
  61. listen_with_retry(s, url)
  62. self.sock = s
  63. def recv(self) -> bytes:
  64. return self.sock.recv()
  65. def close(self):
  66. self.sock.close()
  67. def listen_with_retry(sock, url: str):
  68. retry_count = 10
  69. retry_interval = 0.05
  70. while True:
  71. # noinspection PyBroadException
  72. try:
  73. sock.listen(url)
  74. break
  75. except Exception:
  76. retry_count -= 1
  77. if retry_count < 0:
  78. raise
  79. time.sleep(retry_interval)
  80. def dial_with_retry(sock, url: str):
  81. retry_count = 50
  82. retry_interval = 0.1
  83. while True:
  84. try:
  85. sock.dial(url, block=True)
  86. break
  87. except Exception:
  88. retry_count -= 1
  89. if retry_count < 0:
  90. raise
  91. time.sleep(retry_interval)