php address 127 port 8100 set_time_limit error_reporting E_ALL sock so

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
$address = '127.0.0.1';
$port = 8100;
set_time_limit (0);
error_reporting(E_ALL);
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
socket_bind($sock, $address, $port);
socket_listen($sock);
$client = null;
while (!$client)
{
if ($newsock = @socket_accept($sock))
{
if (is_resource($newsock))
{
echo "New client connected \n";
$client = $newsock;
}
}
sleep(1);
}
$cache = new LimitedCache();
while(true){
if(!@socket_write($client, ">"))
break;
$str = socket_read($client, 1024);
$data = explode(' ', $str);
if(count($data) == 1)
@socket_write($client, $cache->get(trim($data[0])));
else
@socket_write($client, $cache->set(trim($data[0]), $data[1]));
@socket_write($client, "\n");
}
class Node
{
public $data;
public $prev;
public $next;
function __construct($data)
{
$this->data = $data;
$this->prev = null;
$this->next = null;
}
}
class LimitedCache
{
public $head = null;
public $tail = null;
public $map = array();
public $count = 0;
public $limit = 3;
public function set($key, $val){
$is_exist = array_key_exists($key, $this->map);
$deleted_key = null;
if(!$is_exist){
list($deleted_key, $link) = $this->_insert($key);
if($deleted_key)
unset($this->map[$deleted_key]);
// Insert to the map
$this->map[$key] = array('data' => $val, 'link' => $link);
}
else{
$this->_update($this->map[$key]['link']);
// Update map value
$this->map[$key]['data'] = $val;
}
return ($is_exist ? 'Updated': 'Saved').
($deleted_key ? ', deleting '.$deleted_key : "");
}
function get($key){
return array_key_exists($key, $this->map)
? $this->map[$key]['data'] : 'Not found';
}
public function _insert($data){
$link = new Node($data);
$link->prev = $this->tail;
if(!$this->tail)
$this->head = $this->tail = &$link;
else{
$this->tail->next = &$link;
$this->tail = &$link;
}
$deleted_key = null;
$this->count += 1;
if($this->count > $this->limit){
// remove head as it's the oldest
$deleted_key = $this->head->data;
$old_head = $this->head;
$this->head = $this->head->next;
$this->head->prev = null;
unset($old_head);
$this->count -= 1;
}
echo "Inserting ".$data."\n";
if($deleted_key)
echo "Deleting ".$deleted_key."\n";
return array($deleted_key, &$link);
}
public function _update(&$link){
echo "Updating ".$link->data."\n";
if($link == $this->tail)
return;
// remove link from whatever it was
if($link->prev)
$link->prev->next = $link->next;
if($link->next)
$link->next->prev = $link->prev;
if($link == $this->head)
$this->head = $link->next;
// now move it to the tail
$this->tail->next = &$link;
$link->prev = $this->tail;
$this->tail = &$link;
$this->tail->next = null;
}
}