Abstract.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * @file
  4. * TeamSpeak 3 PHP Framework
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * @package TeamSpeak3
  20. * @author Sven 'ScP' Paulsen
  21. * @copyright Copyright (c) Planet TeamSpeak. All rights reserved.
  22. */
  23. /**
  24. * @class TeamSpeak3_Adapter_Abstract
  25. * @brief Provides low-level methods for concrete adapters to communicate with a TeamSpeak 3 Server.
  26. */
  27. abstract class TeamSpeak3_Adapter_Abstract
  28. {
  29. /**
  30. * Stores user-provided options.
  31. *
  32. * @var array
  33. */
  34. protected $options = null;
  35. /**
  36. * Stores an TeamSpeak3_Transport_Abstract object.
  37. *
  38. * @var TeamSpeak3_Transport_Abstract
  39. */
  40. protected $transport = null;
  41. /**
  42. * The TeamSpeak3_Adapter_Abstract constructor.
  43. *
  44. * @param array $options
  45. * @return TeamSpeak3_Adapter_Abstract
  46. */
  47. public function __construct(array $options)
  48. {
  49. $this->options = $options;
  50. if($this->transport === null)
  51. {
  52. $this->syn();
  53. }
  54. }
  55. /**
  56. * The TeamSpeak3_Adapter_Abstract destructor.
  57. *
  58. * @return void
  59. */
  60. abstract public function __destruct();
  61. /**
  62. * Connects the TeamSpeak3_Transport_Abstract object and performs initial actions on the remote
  63. * server.
  64. *
  65. * @throws TeamSpeak3_Adapter_Exception
  66. * @return void
  67. */
  68. abstract protected function syn();
  69. /**
  70. * Commit pending data.
  71. *
  72. * @return array
  73. */
  74. public function __sleep()
  75. {
  76. return array("options");
  77. }
  78. /**
  79. * Reconnects to the remote server.
  80. *
  81. * @return void
  82. */
  83. public function __wakeup()
  84. {
  85. $this->syn();
  86. }
  87. /**
  88. * Returns the profiler timer used for this connection adapter.
  89. *
  90. * @return TeamSpeak3_Helper_Profiler_Timer
  91. */
  92. public function getProfiler()
  93. {
  94. return TeamSpeak3_Helper_Profiler::get(spl_object_hash($this));
  95. }
  96. /**
  97. * Returns the transport object used for this connection adapter.
  98. *
  99. * @return TeamSpeak3_Transport_Abstract
  100. */
  101. public function getTransport()
  102. {
  103. return $this->transport;
  104. }
  105. /**
  106. * Loads the transport object object used for the connection adapter and passes a given set
  107. * of options.
  108. *
  109. * @param array $options
  110. * @param string $transport
  111. * @throws TeamSpeak3_Adapter_Exception
  112. * @return void
  113. */
  114. protected function initTransport($options, $transport = "TeamSpeak3_Transport_TCP")
  115. {
  116. if(!is_array($options))
  117. {
  118. throw new TeamSpeak3_Adapter_Exception("transport parameters must provided in an array");
  119. }
  120. $this->transport = new $transport($options);
  121. }
  122. /**
  123. * Returns the hostname or IPv4 address the underlying TeamSpeak3_Transport_Abstract object
  124. * is connected to.
  125. *
  126. * @return string
  127. */
  128. public function getTransportHost()
  129. {
  130. return $this->getTransport()->getConfig("host", "0.0.0.0");
  131. }
  132. /**
  133. * Returns the port number of the server the underlying TeamSpeak3_Transport_Abstract object
  134. * is connected to.
  135. *
  136. * @return string
  137. */
  138. public function getTransportPort()
  139. {
  140. return $this->getTransport()->getConfig("port", "0");
  141. }
  142. }