Skip to content

Commit 72eef38

Browse files
author
Federico Fissore
committed
Ported new php bridge client "getall" function to python.
Added file bridgeclient_example.py (python). Formatted code from both bridgeclients for better readability
1 parent 293c109 commit 72eef38

File tree

4 files changed

+86
-45
lines changed

4 files changed

+86
-45
lines changed

bridge/bridgeclient.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@
2828
from tcp import TCPJSONClient
2929
from time import sleep
3030

31+
3132
class BridgeClient:
3233
def wait_response(self, json, timeout):
33-
while timeout>=0:
34+
while timeout >= 0:
3435
r = json.recv()
3536
if not r is None:
3637
return r
@@ -51,15 +52,23 @@ def wait_key(self, key, json, timeout):
5152

5253
def get(self, key):
5354
json = TCPJSONClient('127.0.0.1', 5700)
54-
json.send({'command':'get', 'key':key})
55+
json.send({'command': 'get', 'key': key})
5556
r = self.wait_key(key, json, 10)
5657
json.close()
5758
return r
5859

60+
def getall(self):
61+
json = TCPJSONClient('127.0.0.1', 5700)
62+
json.send({'command': 'get'})
63+
r = self.wait_response(json, 10)
64+
if not r is None:
65+
r = r['value']
66+
json.close()
67+
return r
68+
5969
def put(self, key, value):
6070
json = TCPJSONClient('127.0.0.1', 5700)
61-
json.send({'command':'put', 'key':key, 'value':value})
71+
json.send({'command': 'put', 'key': key, 'value': value})
6272
r = self.wait_key(key, json, 10)
6373
json.close()
6474
return r
65-

bridge/bridgeclient_example.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/python
2+
3+
import sys
4+
5+
sys.path.insert(0, '/usr/lib/python2.7/bridge/')
6+
7+
from bridgeclient import BridgeClient as bridgeclient
8+
9+
client = bridgeclient()
10+
11+
print 'Assigning value to key D13'
12+
client.put('D13', 'Test D13')
13+
14+
test = client.get('D13')
15+
print 'Value assigned to D13 is ' + test
16+
17+
print
18+
19+
print 'Assigning value to key D11'
20+
client.put('D11', 'Test D11')
21+
22+
all = client.getall()
23+
print 'Listing all stored values'
24+
print all
25+
print 'Value assigned to D11 is ' + all['D11']

bridge/php/bridgeclient.class.php

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -26,62 +26,63 @@ class bridgeclient {
2626
private $service_port = 5700;
2727
private $address = "127.0.0.1";
2828
private $socket;
29-
29+
3030
private function connect() {
31-
($this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP))
32-
|| die("socket_create() failed: " . socket_strerror(socket_last_error()) . "\n");
33-
socket_set_option($this->socket,SOL_SOCKET, SO_RCVTIMEO, array("sec"=>3, "usec"=>0));
34-
socket_connect($this->socket, $this->address, $this->service_port)
35-
|| die("socket_connect() failed: " . socket_strerror(socket_last_error($this->socket)) . "\n");
31+
($this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) || die("socket_create() failed: " . socket_strerror(socket_last_error()) . "\n");
32+
socket_set_option($this->socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 3, "usec" => 0));
33+
socket_connect($this->socket, $this->address, $this->service_port) || die("socket_connect() failed: " . socket_strerror(socket_last_error($this->socket)) . "\n");
3634
}
37-
35+
3836
private function disconnect() {
3937
socket_close($this->socket);
4038
}
4139

42-
private function sendcommand($command,$key="",$value="") {
40+
private function sendcommand($command, $key = "", $value = "") {
4341
$jsonreceive = "";
44-
$obraces=0;
45-
$cbraces=0;
42+
$obraces = 0;
43+
$cbraces = 0;
4644

4745
$this->connect();
48-
49-
if($key<>""){$jsonsend = '{"command":"'.$command.'","key":"'.$key.'","value":"'.$value.'"}';}
50-
else{$jsonsend = '{"command":"'.$command.'"}';}
46+
47+
if ($key <> "") {
48+
$jsonsend = "{\"command\":\"" . $command . "\",\"key\":\"" . $key . "\",\"value\":\"" . $value . "\"}";
49+
} else {
50+
$jsonsend = "{\"command\":\"" . $command . "\"}";
51+
}
5152
socket_write($this->socket, $jsonsend, strlen($jsonsend));
52-
53+
5354
do {
54-
socket_recv($this->socket, $buffer, 1,0);
55-
$jsonreceive.=$buffer;
56-
if($buffer == "{") $obraces++;
57-
if($buffer == "}") $cbraces++;
55+
socket_recv($this->socket, $buffer, 1, 0);
56+
$jsonreceive .= $buffer;
57+
if ($buffer == "{") {
58+
$obraces++;
59+
}
60+
if ($buffer == "}") {
61+
$cbraces++;
62+
}
5863
} while ($obraces != $cbraces);
59-
64+
6065
$this->disconnect();
61-
62-
if($key<>""){
63-
$jsonarray=json_decode($jsonreceive);
64-
if ($jsonarray->{'value'} == NULL) $jsonarray->{'value'}="None";
65-
66-
return $jsonarray->{'value'};
67-
}
68-
else{
69-
return $jsonreceive;
66+
67+
$jsonarray = json_decode($jsonreceive);
68+
if ($jsonarray->{"value"} == NULL) {
69+
$jsonarray->{"value"} = "None";
7070
}
71+
72+
return $jsonarray->{"value"};
7173
}
72-
74+
7375
public function get($key) {
74-
return $this->sendcommand("get",$key);
76+
return $this->sendcommand("get", $key);
7577
}
76-
77-
public function put($key,$value) {
78-
return $this->sendcommand("put",$key,$value);
78+
79+
public function put($key, $value) {
80+
return $this->sendcommand("put", $key, $value);
7981
}
80-
82+
8183
public function getall() {
8284
return $this->sendcommand("get");
8385
}
84-
8586
}
86-
?>
8787

88+
?>

bridge/php/example.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
<?php
2-
require ("bridgeclient.class.php");
2+
require("bridgeclient.class.php");
33

44
$client = new bridgeclient();
55

6-
$client->put("D13","Test");
6+
print("Assigning value to key D13\n");
7+
$client->put("D13", "Test D13");
78

89
$test = $client->get("D13");
9-
echo $test;
10+
print("Value assigned to D13 is " . $test . "\n");
11+
12+
print("\n");
13+
14+
print("Assigning value to key D11\n");
15+
$client->put("D11", "Test D11");
1016

1117
$all = $client->getall();
12-
$all = json_decode($all, true);
18+
print("Listing all stored values\n");
1319
print_r($all);
20+
print("Value assigned to D11 is " . $all->{"D11"} . "\n");
1421
?>
15-

0 commit comments

Comments
 (0)