Advertisement
Guest User

Untitled

a guest
Nov 14th, 2016
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.82 KB | None | 0 0
  1. <?php
  2.  
  3. class Messages
  4. {
  5.     public static $login = "\e[31mPlease enter a username:\r\n\e[39m";
  6.     public static $commands = "\e[32mThe available commands are:\r\n\r\n/send\r\n/receive\r\n/exit\r\n\r\n\e[39m";
  7.     public static $sendDestination = "\e[34mMessage destination:\r\n\e[39m";
  8.     public static $sendMessage = "\e[34mMessage to be sent:\r\n\e[39m";
  9.     public static $receivedBy = "\e[32mSent by: \e[39m";
  10.     public static $receivedMessage = "\e[32mMessage: \e[39m";
  11.     public static $noMessages = "\e[93m\r\n\r\nNo new messages.\r\n\r\n\e[39m";
  12.     public static $ok = "\e[93m\r\n ~~~OK~~~ \r\n\r\n\e[39m";
  13.     public static $no = "\e[31m\r\nThe command was not recognised\r\n\e[39m";
  14. }
  15.  
  16. class Input extends Messages
  17. {
  18.     public function waitInput()
  19.     {
  20.         $input = fgets(STDIN, 1024);
  21.         $input = rtrim($input, "\n\r");
  22.  
  23.         return $input;
  24.     }
  25.  
  26.     public function checkInput($input, $user)
  27.     {
  28.         $actions = new Actions();
  29.         if ($input === '/send') {
  30.             $actions->send($user);
  31.         } elseif ($input === '/receive') {
  32.             $actions->receive($user);
  33.         } elseif ($input === '/exit') {
  34.             exit();
  35.         } else {
  36.             $actions->sandby($user);
  37.         }
  38.     }
  39. }
  40.  
  41. class Actions extends Input
  42. {
  43.     public function standby($user)
  44.     {
  45.         echo Messages::$commands;
  46.         $inputCommand = Input::waitInput();
  47.         Input::checkInput($inputCommand, $user);
  48.     }
  49.  
  50.     public function login()
  51.     {
  52.         echo Messages::$login;
  53.         $username = Input::waitInput();
  54.         self::standby($username);
  55.     }
  56.  
  57.     public function send($user)
  58.     {
  59.         echo Messages::$sendDestination;
  60.         $destination = Input::waitInput();
  61.  
  62.         echo Messages::$sendMessage;
  63.         $message = Input::waitInput();
  64.  
  65.         try {
  66.             $stomp = new Stomp('tcp://localhost:61613');
  67.             while (true) {
  68.                 $stomp->send($destination, $message, array('sent_by' => $user));
  69.                 break;
  70.             }
  71.         } catch (StompException $e) {
  72.             die('Connection error: '.$e);
  73.         }
  74.  
  75.         self::standby($user);
  76.     }
  77.  
  78.     public function receive($user)
  79.     {
  80.         $stomp = new Stomp('tcp://127.0.0.1:61613');
  81.         $stomp->subscribe($user);
  82.         while ($stomp->hasFrame()) {
  83.             $frame = $stomp->readFrame();
  84.             if ($frame != null) {
  85.                 echo Messages::$receivedBy.$frame->headers['sent_by']."\r\n";
  86.                 echo Messages::$receivedMessage.$frame->body."\r\n\r\n\r\n";
  87.                 $stomp->ack($frame);
  88.             }
  89.         }
  90.         if (!$stomp->hasFrame()) {
  91.             unset($stomp);
  92.             echo Messages::$noMessages;
  93.         }
  94.         self::standBy($user);
  95.     }
  96. }
  97.  
  98. $start = new Actions();
  99. $start->login();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement