ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/widget.ext
(Generate patch)

Comparing deliantra/server/ext/widget.ext (file contents):
Revision 1.12 by root, Sun Jul 22 20:08:38 2007 UTC vs.
Revision 1.14 by root, Mon Jul 23 23:38:17 2007 UTC

1#! perl # mandatory depends=login 1#! perl # mandatory depends=login
2 2
3# sends the following ext message types 3# sends the following ext message types
4# ws_n id # widgetset new 4# ws_n ws # widgetset new
5# ws_d id # widgetset destroy 5# ws_d ws # widgetset destroy
6# ws_c ws id class args # widgetset create 6# ws_c ws id class args # widgetset create
7# w_c id [rid] name args # widget method call 7# w_c id rid name args # widget method call
8# w_s id @attr # widget member set 8# w_s id @attr # widget member set
9# w_g id rid @attr # widget member get 9# w_g id rid @attr # widget member get
10# 10#
11# and expects the following exti message types 11# and expects the following exti message types
12# w_r rid res # widget call return 12# w_r rid res # widget call return
13# w_e id name args # widget_event 13# w_e id name args # widget_event
14
15our $DEBUG = 1;
14 16
15cf::client->attach ( 17cf::client->attach (
16 on_connect => sub { 18 on_connect => sub {
17 my ($ns) = @_; 19 my ($ns) = @_;
18 20
95 #csc_start $ns; 97 #csc_start $ns;
96 }, 98 },
97); 99);
98 100
99cf::register_exticmd w_e => sub { 101cf::register_exticmd w_e => sub {
100 my ($ns, $pkt) = @_; 102 my ($ns, $id, $name, $args) = @_;
101 103
102 if (my $w = $ns->{widget}{$pkt->{id}}) { 104 if (my $w = $ns->{widget}{$id}) {
103 if (my $cb = $w->{ev}{$pkt->{name}}) { 105 if (my $cb = $w->{ev}{$name}) {
104 $_->($w, @{ $pkt->{args} || [] }) 106 $_->($w, @$args)
105 for @$cb; 107 for @$cb;
106 } 108 }
107 } 109 }
108 110
109 () 111 ()
110}; 112};
111 113
112cf::register_exticmd w_r => sub { 114cf::register_exticmd w_r => sub {
113 my ($ns, $pkt) = @_; 115 my ($ns, $rid, $res) = @_;
114 116
115 if (my $cb = delete $ns->{widget_return}{$pkt->{rid}}) { 117 if (my $cb = delete $ns->{widget_return}{$rid}) {
116 $cb->(@{$pkt->{res} || [] }); 118 $cb->(@$res);
117 } 119 }
118 120
119 () 121 ()
120}; 122};
121 123
128 id => $id, 130 id => $id,
129 ns => $self, 131 ns => $self,
130 _w => {}, 132 _w => {},
131 }, "ext::widget::set"; 133 }, "ext::widget::set";
132 134
133 $ws->msg (ws_n => id => $id); 135 $ws->msg (ws_n => $id);
134 136
135 $ws 137 $ws
136} 138}
137 139
138############################################################################# 140#############################################################################
144} 146}
145 147
146sub destroy { 148sub destroy {
147 my ($self) = @_; 149 my ($self) = @_;
148 150
149 $self->msg (ws_d => id => $self->{id}); 151 $self->msg (ws_d => $self->{id});
150 delete $self->{ns}; 152 delete $self->{ns};
151 $_->destroy 153 $_->destroy
152 for values %{ $self->{w} }; 154 for values %{ $self->{w} };
153} 155}
154 156
155sub msg { 157sub msg {
156 my ($self, $type, %msg) = @_; 158 my ($self, @msg) = @_;
157 159
158 if (my $ns = shift->{ns}) { 160 if (my $ns = shift->{ns}) {
159 $msg{msgtype} = $type; 161 warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d#
160 $ns->send_packet ("ext " . $ns->{json_coder}->encode (\%msg)); 162 $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg));
161 } 163 }
162} 164}
163 165
164sub new { 166sub new {
165 my ($self, $class, %args) = @_; 167 my ($self, $class, %args) = @_;
166 168
167 my $id = ++$self->{ns}{id}; 169 my $id = ++$self->{ns}{id};
168 170
169 my $proxy = $self->{_w}{$id} = bless { 171 my $proxy = bless {
170 id => $id, 172 id => $id,
171 }, "ext::widget::proxy"; 173 }, "ext::widget::proxy";
172 174
175 Scalar::Util::weaken ($self->{_w}{$id} = $proxy);
173 Scalar::Util::weaken ($proxy->{ws} = $self); 176 Scalar::Util::weaken ($proxy->{ws} = $self);
174 Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); 177 Scalar::Util::weaken ($proxy->{ns} = $self->{ns});
175 Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy); 178 Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy);
176 179
177 for my $ev (grep /^on_/, keys %args) { 180 for my $ev (grep /^on_/, keys %args) {
178 push @{$proxy->{ev}{$ev}}, $args{$ev}; 181 push @{$proxy->{ev}{$ev}}, $args{$ev};
179 $args{$ev} = 0; 182 $args{$ev} = 0;
180 } 183 }
181 184
182 $self->msg (ws_c => 185 $self->msg (ws_c =>
183 ws => $proxy->{id}, 186 $proxy->{id},
184 id => $id, 187 $id,
185 class => $class, 188 $class,
186 args => \%args, 189 \%args,
187 ); 190 );
188 191
189 $proxy 192 $proxy
190} 193}
191 194
196sub DESTROY { 199sub DESTROY {
197 my ($self) = @_; 200 my ($self) = @_;
198 201
199 delete $self->{ns}{widget}{$self->{id}}; 202 delete $self->{ns}{widget}{$self->{id}};
200 203
201 #warn "DES<$self> $self->{ws}\n";#d#
202 if (my $ws = $self->{ws}) { 204 if (my $ws = $self->{ws}) {
203 $self->msg (w_c => name => "destroy"); 205 $self->msg (w_c => 0, "destroy");
204 delete $ws->{_w}{$self->{id}}; 206 delete $ws->{_w}{$self->{id}};
205 } 207 }
206} 208}
207 209
208sub msg { 210sub msg {
209 my ($self, $type, %msg) = @_; 211 my ($self, $type, @arg) = @_;
210 212
211 if (my $ws = $self->{ws}) { 213 if (my $ws = $self->{ws}) {
212 $ws->msg ($type, 214 $ws->msg ($type, $self->{id}, @arg);
213 %msg,
214 id => $self->{id},
215 );
216 } 215 }
217} 216}
218 217
219sub msg_cb { 218sub msg_cb {
220 my ($self, $cb, $type, %msg) = @_; 219 my ($self, $cb, $type, @arg) = @_;
221 220
222 if (my $ws = $self->{ws}) { 221 if (my $ws = $self->{ws}) {
223
224 my $rid = ++$ws->{ns}{id}; 222 my $rid = ++$ws->{ns}{id};
225 223
226 $self->msg ($type, %msg, rid => $rid); 224 $self->msg ($type, $rid, @arg);
227 225
228 if ($cb) { 226 if ($cb) {
229 $ws->{ns}{widget_return}{$rid} = $cb; 227 $ws->{ns}{widget_return}{$rid} = $cb;
230 } else { 228 } else {
231 # synchronous case 229 # synchronous case
246} 244}
247 245
248sub set { 246sub set {
249 my ($self, @kv) = @_; 247 my ($self, @kv) = @_;
250 248
251 $self->msg (w_s => attr => \@kv); 249 $self->msg (w_s => \@kv);
252} 250}
253 251
254sub get { 252sub get {
255 my ($self, $member, $cb) = @_; 253 my ($self, $member, $cb) = @_;
256 254
257 $self->msg_cb ($cb, w_g => attr => [$member]); 255 $self->msg_cb ($cb, w_g => [$member]);
258} 256}
259 257
260sub TO_JSON { 258sub TO_JSON {
261 { __widget_ref__ => $_[0]{id} } 259 { __widget_ref__ => $_[0]{id} }
262} 260}
267 $AUTOLOAD =~ s/^.*:// 265 $AUTOLOAD =~ s/^.*://
268 or return; 266 or return;
269 267
270 my $self = shift; 268 my $self = shift;
271 269
272 $self->msg (w_c => name => $AUTOLOAD, args => \@_); 270 $self->msg (w_c => 0, $AUTOLOAD, \@_);
273 271
274 () 272 ()
275} 273}
276 274

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines