1: <?php
2:
3: namespace Budabot\Core;
4:
5: /**
6: * The SocketNotifier class provides a way to be notified when some
7: * activity happens in a socket.
8: *
9: * You can add instance of SocketNotifier to Budabot's event loop with method
10: * SocketManager::addSocketNotifier() and remove it with
11: * SocketManager::removeSocketNotifier().
12: *
13: * When some activity happens in the given socket the event loop will call the
14: * given callback to notify of the activity.
15: */
16: class SocketNotifier {
17: private $socket;
18: private $type;
19: private $callback;
20:
21: const ACTIVITY_READ = 1; // there is
22: const ACTIVITY_WRITE = 2;
23: const ACTIVITY_ERROR = 4;
24:
25: /**
26: * Constructor method.
27: * @param $socket the socket to listen
28: * @param $type type of activity
29: * @param $callback the callback which is called on socket activity
30: */
31: public function __construct($socket, $type, $callback) {
32: $this->socket = $socket;
33: $this->type = $type;
34: $this->callback = $callback;
35: }
36:
37: /**
38: * Returns the socket resource.
39: */
40: public function getSocket() {
41: return $this->socket;
42: }
43:
44: /**
45: * Returns type of the activity.
46: */
47: public function getType() {
48: return $this->type;
49: }
50:
51: /**
52: * Calls the callback and passes given @a $type to the callback.
53: */
54: public function notify($type) {
55: call_user_func($this->callback, $type);
56: }
57: }
58: