livesocket.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. """
  2. SleekXMPP: The Sleek XMPP Library
  3. Copyright (C) 2010 Nathanael C. Fritz, Lance J.T. Stout
  4. This file is part of SleekXMPP.
  5. See the file LICENSE for copying permission.
  6. """
  7. import socket
  8. import threading
  9. from sleekxmpp.util import Queue
  10. class TestLiveSocket(object):
  11. """
  12. A live test socket that reads and writes to queues in
  13. addition to an actual networking socket.
  14. Methods:
  15. next_sent -- Return the next sent stanza.
  16. next_recv -- Return the next received stanza.
  17. recv_data -- Dummy method to have same interface as TestSocket.
  18. recv -- Read the next stanza from the socket.
  19. send -- Write a stanza to the socket.
  20. makefile -- Dummy call, returns self.
  21. read -- Read the next stanza from the socket.
  22. """
  23. def __init__(self, *args, **kwargs):
  24. """
  25. Create a new, live test socket.
  26. Arguments:
  27. Same as arguments for socket.socket
  28. """
  29. self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  30. self.recv_buffer = []
  31. self.recv_queue = Queue()
  32. self.send_queue = Queue()
  33. self.send_queue_lock = threading.Lock()
  34. self.recv_queue_lock = threading.Lock()
  35. self.is_live = True
  36. def __getattr__(self, name):
  37. """
  38. Return attribute values of internal, live socket.
  39. Arguments:
  40. name -- Name of the attribute requested.
  41. """
  42. return getattr(self.socket, name)
  43. # ------------------------------------------------------------------
  44. # Testing Interface
  45. def disconnect_errror(self):
  46. """
  47. Used to simulate a socket disconnection error.
  48. Not used by live sockets.
  49. """
  50. try:
  51. self.socket.shutdown()
  52. self.socket.close()
  53. except:
  54. pass
  55. def next_sent(self, timeout=None):
  56. """
  57. Get the next stanza that has been sent.
  58. Arguments:
  59. timeout -- Optional timeout for waiting for a new value.
  60. """
  61. args = {'block': False}
  62. if timeout is not None:
  63. args = {'block': True, 'timeout': timeout}
  64. try:
  65. return self.send_queue.get(**args)
  66. except:
  67. return None
  68. def next_recv(self, timeout=None):
  69. """
  70. Get the next stanza that has been received.
  71. Arguments:
  72. timeout -- Optional timeout for waiting for a new value.
  73. """
  74. args = {'block': False}
  75. if timeout is not None:
  76. args = {'block': True, 'timeout': timeout}
  77. try:
  78. if self.recv_buffer:
  79. return self.recv_buffer.pop(0)
  80. else:
  81. return self.recv_queue.get(**args)
  82. except:
  83. return None
  84. def recv_data(self, data):
  85. """
  86. Add data to a receive buffer for cases when more than a single stanza
  87. was received.
  88. """
  89. self.recv_buffer.append(data)
  90. # ------------------------------------------------------------------
  91. # Socket Interface
  92. def recv(self, *args, **kwargs):
  93. """
  94. Read data from the socket.
  95. Store a copy in the receive queue.
  96. Arguments:
  97. Placeholders. Same as for socket.recv.
  98. """
  99. data = self.socket.recv(*args, **kwargs)
  100. with self.recv_queue_lock:
  101. self.recv_queue.put(data)
  102. return data
  103. def send(self, data):
  104. """
  105. Send data on the socket.
  106. Store a copy in the send queue.
  107. Arguments:
  108. data -- String value to write.
  109. """
  110. with self.send_queue_lock:
  111. self.send_queue.put(data)
  112. return self.socket.send(data)
  113. # ------------------------------------------------------------------
  114. # File Socket
  115. def makefile(self, *args, **kwargs):
  116. """
  117. File socket version to use with ElementTree.
  118. Arguments:
  119. Placeholders, same as socket.makefile()
  120. """
  121. return self
  122. def read(self, *args, **kwargs):
  123. """
  124. Implement the file socket read interface.
  125. Arguments:
  126. Placeholders, same as socket.recv()
  127. """
  128. return self.recv(*args, **kwargs)
  129. def clear(self):
  130. """
  131. Empty the send queue, typically done once the session has started to
  132. remove the feature negotiation and log in stanzas.
  133. """
  134. with self.send_queue_lock:
  135. for i in range(0, self.send_queue.qsize()):
  136. self.send_queue.get(block=False)
  137. with self.recv_queue_lock:
  138. for i in range(0, self.recv_queue.qsize()):
  139. self.recv_queue.get(block=False)