1 |
<?php |
2 |
/* |
3 |
The Malete project - the Z39.2/Z39.50 database framework of OpenIsis. |
4 |
Version 0.9.x (patchlevel see file Version) |
5 |
Copyright (C) 2001-2004 by Erik Grziwotz, erik@openisis.org |
6 |
|
7 |
This library is free software; you can redistribute it and/or |
8 |
modify it under the terms of the GNU Lesser General Public |
9 |
License as published by the Free Software Foundation; either |
10 |
version 2.1 of the License, or (at your option) any later version. |
11 |
|
12 |
This library is distributed in the hope that it will be useful, |
13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 |
Lesser General Public License for more details. |
16 |
|
17 |
You should have received a copy of the GNU Lesser General Public |
18 |
License along with this library; if not, write to the Free Software |
19 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
|
21 |
see README for more information |
22 |
EOH */ |
23 |
|
24 |
// $Id: Server.php,v 1.4 2004/11/12 09:34:55 kripke Exp $ |
25 |
|
26 |
|
27 |
/** |
28 |
This class represents the connection to an Isis server. |
29 |
|
30 |
In general, a server is any object having a request function, |
31 |
accepting a single Isis_Rec parameter and returning an Isis_Rec. |
32 |
|
33 |
This implementation is based on a TCP or UNIX socket. |
34 |
|
35 |
@version $Revision: 1.4 $ |
36 |
@license LGPL |
37 |
@package Isis |
38 |
*/ |
39 |
class Isis_Server { |
40 |
var $host; |
41 |
var $port; |
42 |
var $sock; |
43 |
var $pers; |
44 |
var $dbg = false; |
45 |
|
46 |
function Isis_Server ($host = null, $port = 2042, $pers = 0) |
47 |
{ |
48 |
if (!$host && !($host = $_ENV['ISIS_SERVER'])) $host = 'localhost'; |
49 |
$this->host = $host; |
50 |
$this->port = $port; |
51 |
$this->pers = $pers; |
52 |
$this->open(); |
53 |
} |
54 |
|
55 |
function open () |
56 |
{ |
57 |
// pfsockopen will reuse an existing socket on same host:port |
58 |
$this->sock = $this->pers |
59 |
? pfsockopen( $this->host, $this->port /*, $errno, $errstr*/ ) |
60 |
: fsockopen( $this->host, $this->port /*, $errno, $errstr*/ ); |
61 |
return $this->sock; |
62 |
} |
63 |
|
64 |
|
65 |
function request ( $req ) |
66 |
{ |
67 |
if ( !$this->sock && !$this->open() ) |
68 |
return null; |
69 |
if ($this->dbg) error_log("SEND\n".$req->toString(ISIS_REC_TEXT)); |
70 |
fwrite($this->sock, $req->toString(ISIS_REC_TEXT) . "\n"); |
71 |
fflush($this->sock); |
72 |
$txt = ''; |
73 |
while ( ($line = fgets($this->sock)) && "\n" != $line ) { |
74 |
if ($this->dbg) error_log("RETR ".$line ); |
75 |
$txt .= $line; |
76 |
} |
77 |
$res = new Isis_Rec; |
78 |
$res->parse($txt, ISIS_REC_TEXT); |
79 |
if ($this->dbg) error_log("GOT ".$res->toString() ); |
80 |
return $res; |
81 |
} |
82 |
|
83 |
} // class Isis_Server |
84 |
?> |