Server.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zhangdali
  5. * Date: 2016/12/5
  6. * Time: 下午4:36
  7. */
  8. class Server {
  9. /**
  10. * @var string
  11. */
  12. private $host;
  13. /**
  14. * @var int
  15. */
  16. private $priority;
  17. private $minPriority;
  18. private $maxPriority;
  19. private $decrStep;
  20. private $incrStep;
  21. /**
  22. * Server constructor.
  23. * @param string $host
  24. * @param int $minPriority
  25. * @param int $maxPriority
  26. * @param int $decrStep
  27. * @param int $incrStep
  28. */
  29. public function __construct($host, $minPriority, $maxPriority, $decrStep, $incrStep) {
  30. $this->host = $host;
  31. $this->priority = $maxPriority;
  32. $this->minPriority = $minPriority;
  33. $this->maxPriority = $maxPriority;
  34. $this->decrStep = $decrStep;
  35. $this->incrStep = $incrStep;
  36. }
  37. function __destruct() {
  38. }
  39. /**
  40. * @return string
  41. */
  42. public function getHost() {
  43. return $this->host;
  44. }
  45. /**
  46. * @param $host
  47. */
  48. public function setHost($host) {
  49. $this->host = $host;
  50. return $this;
  51. }
  52. /**
  53. * @return int
  54. */
  55. public function getPriority() {
  56. return $this->priority;
  57. }
  58. public function incrPriority() {
  59. $this->changePriority(true, $this->incrStep);
  60. }
  61. public function decrPriority() {
  62. $this->changePriority(false, $this->incrStep);
  63. }
  64. /**
  65. * @param bool $incr
  66. * @param int $step
  67. */
  68. private function changePriority($incr, $step) {
  69. $newPriority = $incr ? $this->priority + $step : $this->priority - $step;
  70. if ($newPriority < $this->minPriority) {
  71. $newPriority = $this->minPriority;
  72. }
  73. if ($newPriority > $this->maxPriority) {
  74. $newPriority = $this->maxPriority;
  75. }
  76. $this->priority = $newPriority;
  77. }
  78. }