trailing spaces
[plewww.git] / planetlab / includes / plc_peers.php
1 <?php
2
3   // $Id$
4
5 require_once 'plc_functions.php';
6
7 drupal_set_html_head('<link href="/planetlab/css/plc_peers.css" rel="stylesheet" type="text/css"/>');
8
9 // all known peers hashed on peer_id
10 class Peers {
11   var $hash;
12
13   function __construct ($api) {
14     $hash=array();
15     // fake entry fot the local myplc
16     $local_fake_peer = array ('peername' => PLC_NAME,
17                               'shortname' => PLC_SHORTNAME,
18                               'peer_id'=>'local');
19     $hash['local']=$local_fake_peer;
20     // remote
21     $peer_columns=array('peer_id','shortname','peername');
22     $peer_filter=NULL;
23     $peers = $api->GetPeers($peer_filter,$peer_columns);
24     if ($peers) foreach ($peers as $peer) {
25         $hash[$peer['peer_id']]=$peer;
26       }
27     $this->hash=$hash;
28   }
29
30   public static function is_local ($peer) {
31     return $peer['peer_id'] == 'local';
32   }
33
34   function peer ($peer_id) {
35     // use the fake local entry
36     if (!$peer_id)
37       $peer_id='local';
38     return $this->hash[$peer_id];
39   }
40
41   public function peername ($peer_id) {
42     $peer = $this->peer ($peer_id);
43     return $peer['peername'];
44   }
45
46   public function shortname ($peer_id) {
47     $peer = $this->peer ($peer_id);
48     return $peer['shortname'];
49   }
50
51   public function label ($peer_id) {
52     $peer = $this->peer ($peer_id);
53     $result = $peer['peername'] . " (" . $peer['shortname'] . ")";
54     if (Peers::is_local ($peer))
55       $result = "[local] " . $result;
56     return $result;
57   }
58
59   public function link ($peer_id,$text) {
60     if (! $peer_id)
61       return href("/",$text);
62     $peer = $this->peer ($peer_id);
63     return l_peer_t($peer['peer_id'],$text);
64   }
65
66   public function peer_link ($peer_id) {
67     if (! $peer_id)
68       return href("/",$this->label($peer_id));
69     $peer = $this->peer ($peer_id);
70     return l_peer_t($peer['peer_id'],$this->label($peer_id));
71   }
72
73   function classname ($peer_id) {
74     $shortname=strtolower($this->shortname($peer_id));
75     return "peer-$shortname";
76   }
77
78   function block_start ($peer_id) {
79     // start a <div> element with 2 classes:
80     // (1) generic:  is either peer-local or peer-foreign
81     // (2) specific: is peer-<shortname> based on the plc's shortname
82     // e.g. at PLE we'd get <div class='peer-local peer-ple'>
83     // or                   <div class='peer-local peer-plc'>
84     // see plc_styles.css for how to have the more specific ones override the generic one
85     if ( ! $peer_id )
86       $generic='peer-local';
87     else
88       $generic='peer-foreign';
89     $specific=$this->classname($peer_id);
90     // add nifty-big for the rounded corner
91     printf ("<div class='$generic $specific nifty-big'>");
92   }
93
94   function block_end ($peer_id) {
95     print "</div>\n";
96   }
97
98   // writes a cell in the table with the peer's shortname, link to the peer page,
99   // and classname set for proper color
100   function cell ($table, $peer_id) {
101     $shortname=$this->shortname($peer_id);
102     $table->cell ($this->link($peer_id,$shortname),
103                   array('class'=>$this->classname($peer_id)));
104   }
105
106 }
107
108 ////////////////////////////////////////////////////////////
109 class PeerScope {
110   var $filter;
111   var $label;
112
113   function __construct ($api, $peerscope) {
114     switch ($peerscope) {
115     case '':
116       $this->filter=array();
117       $this->label="all peers";
118       break;
119     case 'local':
120       $this->filter=array("peer_id"=>NULL);
121       $this->label=PLC_SHORTNAME;
122       break;
123     case 'foreign':
124       $this->filter=array("~peer_id"=>NULL);
125       $this->label="foreign peers";
126       break;
127     default:
128       if (my_is_int ($peerscope)) {
129         $peer_id=intval($peerscope);
130         $peers=$api->GetPeers(array("peer_id"=>$peer_id));
131       } else {
132         $peers=$api->GetPeers(array("shortname"=>$peerscope));
133       }
134       if ($peers) {
135         $peer=$peers[0];
136         $peer_id=$peer['peer_id'];
137         $this->filter=array("peer_id"=>$peer_id);
138         $this->label='peer "' . $peer['shortname'] . '"';
139       } else {
140         $this->filter=array();
141         $this->label="[no such peerscope " . $peerscope . "]";
142       }
143       break;
144     }
145   }
146
147   public function filter() {
148     return $this->filter;
149   }
150   public function label() {
151     return $this->label;
152   }
153 }
154
155 ?>