Client.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * Copyright 2014 Fabian Grutschus. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  19. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *
  26. * The views and conclusions contained in the software and documentation are those
  27. * of the authors and should not be interpreted as representing official policies,
  28. * either expressed or implied, of the copyright holders.
  29. *
  30. * @author Fabian Grutschus <f.grutschus@lubyte.de>
  31. * @copyright 2014 Fabian Grutschus. All rights reserved.
  32. * @license BSD
  33. * @link http://github.com/fabiang/xmpp
  34. */
  35. namespace Fabiang\Xmpp;
  36. use Fabiang\Xmpp\Options;
  37. use Fabiang\Xmpp\Connection\ConnectionInterface;
  38. use Fabiang\Xmpp\Connection\Socket;
  39. use Fabiang\Xmpp\Protocol\ProtocolImplementationInterface;
  40. use Fabiang\Xmpp\Event\EventManagerAwareInterface;
  41. use Fabiang\Xmpp\Event\EventManagerInterface;
  42. use Fabiang\Xmpp\Event\EventManager;
  43. use Fabiang\Xmpp\EventListener\Logger;
  44. /**
  45. * Xmpp connection client.
  46. *
  47. * @package Xmpp
  48. */
  49. class Client implements EventManagerAwareInterface
  50. {
  51. /**
  52. * Eventmanager.
  53. *
  54. * @var EventManagerInterface
  55. */
  56. protected $eventManager;
  57. /**
  58. * Options.
  59. *
  60. * @var Options
  61. */
  62. protected $options;
  63. /**
  64. * @var ConnectionInterface
  65. */
  66. protected $connection;
  67. /**
  68. * Constructor.
  69. *
  70. * @param Options $options Client options
  71. * @param EventManagerInterface $eventManager Event manager
  72. */
  73. public function __construct(Options $options, EventManagerInterface $eventManager = null)
  74. {
  75. // create default connection
  76. if (null !== $options->getConnection()) {
  77. $connection = $options->getConnection();
  78. } else {
  79. $connection = Socket::factory($options);
  80. $options->setConnection($connection);
  81. }
  82. $this->options = $options;
  83. $this->connection = $connection;
  84. if (null === $eventManager) {
  85. $eventManager = new EventManager();
  86. }
  87. $this->eventManager = $eventManager;
  88. $this->setupImplementation();
  89. }
  90. /**
  91. * Setup implementation.
  92. *
  93. * @return void
  94. */
  95. protected function setupImplementation()
  96. {
  97. $this->connection->setEventManager($this->eventManager);
  98. $this->connection->setOptions($this->options);
  99. $implementation = $this->options->getImplementation();
  100. $implementation->setEventManager($this->eventManager);
  101. $implementation->setOptions($this->options);
  102. $implementation->register();
  103. $implementation->registerListener(new Logger());
  104. }
  105. /**
  106. * Connect to server.
  107. *
  108. * @return void
  109. */
  110. public function connect()
  111. {
  112. $this->connection->connect();
  113. }
  114. /**
  115. * Disconnect from server.
  116. *
  117. * @return void
  118. */
  119. public function disconnect()
  120. {
  121. $this->connection->disconnect();
  122. }
  123. /**
  124. * Send data to server.
  125. *
  126. * @param ProtocolImplementationInterface $interface Interface
  127. * @return void
  128. */
  129. public function send(ProtocolImplementationInterface $interface)
  130. {
  131. $data = $interface->toString();
  132. $this->connection->send($data);
  133. }
  134. /**
  135. * {@inheritDoc}
  136. */
  137. public function getEventManager()
  138. {
  139. return $this->eventManager;
  140. }
  141. /**
  142. * {@inheritDoc}
  143. */
  144. public function setEventManager(EventManagerInterface $eventManager)
  145. {
  146. $this->eventManager = $eventManager;
  147. return $this;
  148. }
  149. /**
  150. * Get options.
  151. *
  152. * @return Options
  153. */
  154. public function getOptions()
  155. {
  156. return $this->options;
  157. }
  158. /**
  159. * @return ConnectionInterface
  160. */
  161. public function getConnection()
  162. {
  163. return $this->connection;
  164. }
  165. }