Skip to content

Commit 1232411

Browse files
author
Daniel Kinzler
committed
protocol version, VALUE status, _value suffix, tests for set-meta, list-meta
git-svn-id: https://svn.toolserver.org/svnroot/daniel/duesenstuff/trunk/gpClient@566 9f2c43bc-b3c0-43f4-b155-41619b16f219
1 parent 0920fe3 commit 1232411

File tree

2 files changed

+134
-19
lines changed

2 files changed

+134
-19
lines changed

php/gpClient.php

Lines changed: 68 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,23 @@
5757
define( 'GP_PORT', 6666 );
5858

5959
/**
60-
* Expected GraphServ protocol version. If GraphServ (resp GraphCore)
61-
* reports a different protocol version, the conenction is aborted.
60+
* Implemented GraphServ protocol version. This indicates what features the
61+
* client library supports. It is not validated against the peer's protocol version,
62+
* see GP_MIN_PROTOCOL_VERSION and GP_MAX_PROTOCOL_VERSION for that.
6263
*/
63-
define( 'GP_CLIENT_PROTOCOL_VERSION', 3 );
64+
define( 'GP_CLIENT_PROTOCOL_VERSION', 4 ); #TODO: port min/max logic to python
65+
66+
/**
67+
* Lowest acceptable GraphServ protocol version. If GraphServ (resp GraphCore)
68+
* reports a lower protocol version, the conenction is aborted.
69+
*/
70+
define( 'GP_MIN_PROTOCOL_VERSION', 2.0 ); #TODO: port min/max logic to python
71+
72+
/**
73+
* Highest acceptable GraphServ protocol version. If GraphServ (resp GraphCore)
74+
* reports a higher protocol version, the conenction is aborted.
75+
*/
76+
define( 'GP_MAX_PROTOCOL_VERSION', 4.99 ); #TODO: port min/max logic to python
6477

6578
/**
6679
* Base class for gpClient exceptions.
@@ -981,6 +994,11 @@ public function checkPeer() {
981994
* a key-value pairs. If the _map suffix is used without the capture_ prefix,
982995
* a gpUsageException is raised.
983996
*
997+
* * if the method name is suffixed with _value, the command is expected
998+
* to return a value in the status line, prefixed by either "OK." or "VALUE:".
999+
* The value is returned. If the command does not provide a VALUE or OK status,
1000+
* an exception is raised.
1001+
*
9841002
* Modifiers can also be combined. For instance, try_capture_stats_map would
9851003
* return GraphCore stats as an associative array, or null of the call failed.
9861004
*
@@ -1095,7 +1113,7 @@ public function connect() {
10951113
*
10961114
* * $connection this gpConnection instance
10971115
* * &$cmd a reference to the command name, as a string, with the try_,
1098-
* capture_ and _map modifiers removed.
1116+
* capture_, _map or _value modifiers removed.
10991117
* * &$args a reference to the argument array, unprocessed, as passed to
11001118
* the method.
11011119
* * &$source a reference to a gpDatSource (or null), may be altered to
@@ -1211,18 +1229,28 @@ public function setDebug($debug) {
12111229
* Returns the protocol version reported by the peer.
12121230
*/
12131231
public function getProtocolVersion() {
1214-
$this->protocol_version();
1215-
$version = trim($this->statusMessage);
1216-
return $version;
1232+
if ( empty($this->protocol_version) ) { #TODO: port lazy init to python!
1233+
$this->protocol_version();
1234+
$this->protocol_version = trim($this->statusMessage);
1235+
}
1236+
1237+
return $this->protocol_version;
12171238
}
12181239

12191240
/**
12201241
* Raises a gpProtocolException if the protocol version reported by the peer is
12211242
* not compatible with GP_CLIENT_PROTOCOL_VERSION.
12221243
*/
12231244
public function checkProtocolVersion() {
1224-
$version = $this->getProtocolVersion();
1225-
if ( $version != GP_CLIENT_PROTOCOL_VERSION ) throw new gpProtocolException( "Bad protocol version: expected " . GP_CLIENT_PROTOCOL_VERSION . ", but peer uses " . $version );
1245+
$version = (float)$this->getProtocolVersion();
1246+
1247+
if ( $version < GP_MIN_PROTOCOL_VERSION ) { #TODO: port min/max to python
1248+
throw new gpProtocolException( "Bad protocol version: expected at least " . GP_MIN_PROTOCOL_VERSION . ", but peer uses " . $version );
1249+
}
1250+
1251+
if ( $version > GP_MAX_PROTOCOL_VERSION ) { #TODO: port min/max to python
1252+
throw new gpProtocolException( "Bad protocol version: expected at most " . GP_MAX_PROTOCOL_VERSION . ", but peer uses " . $version );
1253+
}
12261254
}
12271255

12281256
/**
@@ -1273,6 +1301,15 @@ public function __call($name, $arguments) {
12731301
$map = false;
12741302
}
12751303

1304+
if ( preg_match( '/-value$/', $cmd ) ) { #TODO: port to python
1305+
if ($capture) throw new gpUsageException( "using the _value suffix together with the capture_ prefix is meaningless" );
1306+
1307+
$cmd = substr( $cmd, 0, strlen($cmd) -6 );
1308+
$val = true;
1309+
} else {
1310+
$val = false;
1311+
}
1312+
12761313
$result = null;
12771314
foreach ( $this->call_handlers as $handler ) {
12781315
$continue = call_user_func_array( $handler, array( $this, &$cmd, &$arguments, &$source, &$sink, &$capture, &$result ) );
@@ -1328,9 +1365,9 @@ public function __call($name, $arguments) {
13281365
else return false;
13291366
}
13301367

1331-
//note: call modifiers like capture change the return type!
1368+
//note: call modifiers like "_capture" change the return type!
13321369
if ( $capture ) {
1333-
if ( $status == 'OK' ) {
1370+
if ( $status == 'OK' || $status == 'VALUE' ) { #TODO: port VALUE to python
13341371
if ( $has_output ) {
13351372
if ($map) return $sink->getMap();
13361373
else return $sink->getData();
@@ -1341,8 +1378,17 @@ public function __call($name, $arguments) {
13411378
else if ( $status == 'NONE' ) return null;
13421379
else return false;
13431380
} else {
1344-
if ( $result ) return $result; // from handler
1345-
else return $status;
1381+
if ( $result ) $status = $result; // from handler
1382+
1383+
if ( $val ) { #TODO: port to python!
1384+
if ( $status == "VALUE" || $status == "OK" ) {
1385+
return $this->statusMessage; #XXX: not so pretty
1386+
} else {
1387+
throw new gpUsageException( "Can't apply _value modifier: command " . $command . " did not return a VALUE or OK status, but this: " . $status );
1388+
}
1389+
}
1390+
1391+
return $status;
13461392
}
13471393
}
13481394

@@ -1366,7 +1412,7 @@ public function __call($name, $arguments) {
13661412
* @throws gpProtocolException if a communication error ocurred while talking to the peer
13671413
* @throws gpProcessorException if the peer reported an error
13681414
* @throws gpUsageException if $command does not conform to the rules for commands. Note that
1369-
* $this->strictArguments and $this->alloPipes influence which commands are allowed.
1415+
* $this->strictArguments and $this->allowPipes influence which commands are allowed.
13701416
*/
13711417
public function exec( $command, gpDataSource $source = null, gpDataSink $sink = null, &$has_output = null ) {
13721418
$this->trace(__METHOD__, "BEGIN");
@@ -1400,6 +1446,11 @@ public function exec( $command, gpDataSource $source = null, gpDataSink $sink =
14001446
}
14011447

14021448
$strictArgs = $this->strictArguments;
1449+
1450+
if ( $c == "set-meta" || $c == "authorize" ) { #XXX: ugly hack for wellknown commands #TODO: port to python
1451+
$strictArgs = false;
1452+
}
1453+
14031454
foreach ( $command as $c ) {
14041455
if ( is_array( $c ) || is_object( $c ) ) throw new gpUsageException("invalid argument type: " . gettype($c));
14051456

@@ -1475,7 +1526,7 @@ public function exec( $command, gpDataSource $source = null, gpDataSink $sink =
14751526
$this->status = $m[1];
14761527
$this->statusMessage = trim($m[2]);
14771528

1478-
if ( $this->status != 'OK' && $this->status != 'NONE' ) {
1529+
if ( $this->status != 'OK' && $this->status != 'NONE' && $this->status != 'VALUE' ) { #TODO: port VALUE to python
14791530
throw new gpProcessorException( $this->status, $m[2], $command );
14801531
}
14811532

@@ -1544,9 +1595,8 @@ public static function isValidCommandString( $command ) {
15441595
public static function isValidCommandArgument( $arg, $strict = true ) {
15451596
if ( $arg === '' || $arg === false || $arg === null ) return false;
15461597

1547-
if ( $strict ) return preg_match('/^\w[-\w]*(:\w[-\w]*)?$/', $arg); //XXX: the ":" is needed for user:passwd auth. not pretty.
1548-
1549-
return !preg_match('/[\0-\x1F\x80-\xFF:|<>!&#]/', $arg); //low chars, high chars, and operators.
1598+
if ( $strict ) return preg_match('/^\w[-\w]*$/', $arg); #TODO: port stricter pattern to python
1599+
else return !preg_match('/[\s\0-\x1F\x80-\xFF:|<>!&#]/', $arg); //space, low chars, high chars, and operators. #TODO: port exclusion of spaces to python
15501600
}
15511601

15521602
/**

php/test/gpCore.test.php

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,72 @@ public function testTraverseSuccessorsWithout() {
114114

115115
$this->assertEquals( array( array(11), array(112), array(1121), ), $succ );
116116
}
117-
117+
118+
public function testSetMeta() { #TODO: port to python
119+
$this->gp->set_meta("foo", 1234);
120+
$val = $this->gp->get_meta_value("foo");
121+
$this->assertEquals( "1234", $val );
122+
123+
$this->gp->set_meta("foo", "bla/bla");
124+
$val = $this->gp->get_meta_value("foo");
125+
$this->assertEquals( "bla/bla", $val );
126+
127+
# test bad -----------------------------------------
128+
try {
129+
$this->gp->set_meta("...", 1234);
130+
$this->fail( "exception expected" );
131+
} catch ( gpException $ex ) {
132+
// ok
133+
}
134+
135+
try {
136+
$ok = $this->gp->try_set_meta("x y", 1234);
137+
$this->fail( "exception expected" );
138+
} catch ( gpException $ex ) {
139+
// ok
140+
}
141+
142+
try {
143+
$this->gp->_set_meta(" ", 1234);
144+
$this->fail( "exception expected" );
145+
} catch ( gpException $ex ) {
146+
// ok
147+
}
148+
149+
try {
150+
$this->gp->set_meta("foo", "bla bla");
151+
$this->fail( "exception expected" );
152+
} catch ( gpException $ex ) {
153+
// ok
154+
}
155+
156+
try {
157+
$this->gp->set_meta("foo", "2<3");
158+
$this->fail( "exception expected" );
159+
} catch ( gpException $ex ) {
160+
// ok
161+
}
162+
}
163+
164+
public function testGetMeta() { #TODO: port to python
165+
}
166+
167+
public function testRemoveMeta() { #TODO: port to python
168+
}
169+
170+
public function testListMeta() { #TODO: port to python
171+
$meta = $this->gp->capture_list_meta();
172+
$this->assertEmpty( $meta );
173+
174+
$this->gp->set_meta("foo", 1234);
175+
$meta = $this->gp->capture_list_meta_map();
176+
$this->assertEquals( array("foo" => "1234"), $meta );
177+
178+
$this->gp->remove_meta("foo");
179+
$meta = $this->gp->capture_list_meta();
180+
$this->assertEmpty( $meta );
181+
}
182+
118183
//TODO: add all the tests we have in the talkback test suit
119184

120185
}

0 commit comments

Comments
 (0)