ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf.pm
(Generate patch)

Comparing deliantra/server/lib/cf.pm (file contents):
Revision 1.183 by root, Fri Jan 19 19:42:37 2007 UTC vs.
Revision 1.188 by root, Sat Jan 20 23:30:16 2007 UTC

349 my $runtime = cf::localdir . "/runtime"; 349 my $runtime = cf::localdir . "/runtime";
350 350
351 my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 351 my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644
352 or return; 352 or return;
353 353
354 my $value = $cf::RUNTIME + 90 + 10;
354 my $value = $cf::RUNTIME + 1 + 10; # 10 is the runtime save interval, for a monotonic clock 355 # 10 is the runtime save interval, for a monotonic clock
356 # 60 allows for the watchdog to kill the server.
357
355 (aio_write $fh, 0, (length $value), $value, 0) <= 0 358 (aio_write $fh, 0, (length $value), $value, 0) <= 0
356 and return; 359 and return;
357 360
358 aio_fsync $fh 361 aio_fsync $fh
359 and return; 362 and return;
1137 $rmp->{map_layout_style}, $rmp->{treasureoptions}, $rmp->{symmetry_used}, 1140 $rmp->{map_layout_style}, $rmp->{treasureoptions}, $rmp->{symmetry_used},
1138 (cf::region::find $rmp->{region}), $rmp->{custom} 1141 (cf::region::find $rmp->{region}), $rmp->{custom}
1139 ) 1142 )
1140} 1143}
1141 1144
1145=item cf::map->register ($regex, $prio)
1146
1147Register a handler for the map path matching the given regex at the
1148givne priority (higher is better, built-in handlers have priority 0, the
1149default).
1150
1151=cut
1152
1142sub register { 1153sub register {
1143 my (undef, $regex) = @_; 1154 my (undef, $regex, $prio) = @_;
1144 my $pkg = caller; 1155 my $pkg = caller;
1145 1156
1146 no strict; 1157 no strict;
1147 push @{"$pkg\::ISA"}, __PACKAGE__; 1158 push @{"$pkg\::ISA"}, __PACKAGE__;
1148 1159
1149 $EXT_MAP{$pkg} = qr<$regex>; 1160 $EXT_MAP{$pkg} = [$prio, qr<$regex>];
1150} 1161}
1151 1162
1152# also paths starting with '/' 1163# also paths starting with '/'
1153$EXT_MAP{"cf::map"} = qr{^(?=/)}; 1164$EXT_MAP{"cf::map"} = [0, qr{^(?=/)}];
1154 1165
1155sub thawer_merge { 1166sub thawer_merge {
1156 my ($self, $merge) = @_; 1167 my ($self, $merge) = @_;
1157 1168
1158 # we have to keep some variables in memory intact 1169 # we have to keep some variables in memory intact
1201 1212
1202 return $path if UNIVERSAL::isa $path, "cf::map"; # already a map object 1213 return $path if UNIVERSAL::isa $path, "cf::map"; # already a map object
1203 1214
1204 $path = normalise $path, $base; 1215 $path = normalise $path, $base;
1205 1216
1206 for my $pkg (keys %EXT_MAP) { 1217 for my $pkg (sort { $EXT_MAP{$b}[0] <=> $EXT_MAP{$a}[0] } keys %EXT_MAP) {
1207 if ($path =~ $EXT_MAP{$pkg}) { 1218 if ($path =~ $EXT_MAP{$pkg}[1]) {
1208 my $self = bless cf::map::new, $pkg; 1219 my $self = bless cf::map::new, $pkg;
1209 $self->{path} = $path; $self->path ($path); 1220 $self->{path} = $path; $self->path ($path);
1210 $self->init; # pass $1 etc. 1221 $self->init; # pass $1 etc.
1211 return $self; 1222 return $self;
1212 } 1223 }
1286 $self->{load_path} = $path; 1297 $self->{load_path} = $path;
1287 1298
1288 1 1299 1
1289} 1300}
1290 1301
1291sub load_orig { 1302sub load_header_orig {
1292 my ($self) = @_; 1303 my ($self) = @_;
1293 1304
1294 $self->load_header_from ($self->load_path) 1305 $self->load_header_from ($self->load_path)
1295} 1306}
1296 1307
1297sub load_temp { 1308sub load_header_temp {
1298 my ($self) = @_; 1309 my ($self) = @_;
1299 1310
1300 $self->load_header_from ($self->save_path) 1311 $self->load_header_from ($self->save_path)
1312}
1313
1314sub prepare_temp {
1315 my ($self) = @_;
1316
1317 $self->last_access ((delete $self->{last_access})
1318 || $cf::RUNTIME); #d#
1319 # safety
1320 $self->{instantiate_time} = $cf::RUNTIME
1321 if $self->{instantiate_time} > $cf::RUNTIME;
1322}
1323
1324sub prepare_orig {
1325 my ($self) = @_;
1326
1327 $self->{load_original} = 1;
1328 $self->{instantiate_time} = $cf::RUNTIME;
1329 $self->last_access ($cf::RUNTIME);
1330 $self->instantiate;
1301} 1331}
1302 1332
1303sub load_header { 1333sub load_header {
1304 my ($self) = @_; 1334 my ($self) = @_;
1305 1335
1306 if ($self->load_temp) { 1336 if ($self->load_header_temp) {
1307 $self->last_access ((delete $self->{last_access}) 1337 $self->prepare_temp;
1308 || $cf::RUNTIME); #d#
1309 # safety
1310 $self->{instantiate_time} = $cf::RUNTIME
1311 if $self->{instantiate_time} > $cf::RUNTIME;
1312 } else { 1338 } else {
1313 $self->load_orig 1339 $self->load_header_orig
1314 or return; 1340 or return;
1315 1341 $self->prepare_orig;
1316 $self->{load_original} = 1;
1317 $self->{instantiate_time} = $cf::RUNTIME;
1318 $self->last_access ($cf::RUNTIME);
1319 $self->instantiate;
1320 } 1342 }
1321 1343
1322 1 1344 1
1323} 1345}
1324 1346
1339 1361
1340 $map->load_header 1362 $map->load_header
1341 or return; 1363 or return;
1342 1364
1343 if ($map->should_reset) { 1365 if ($map->should_reset) {
1366 # doing this can freeze the server in a sync job, obviously
1344 $cf::WAIT_FOR_TICK->wait; 1367 #$cf::WAIT_FOR_TICK->wait;
1345 $map->reset; 1368 $map->reset;
1346 undef $guard; 1369 undef $guard;
1347 $map = find $path 1370 $map = find $path
1348 or return; 1371 or return;
1349 } 1372 }
1350 1373
1351 $cf::MAP{$path} = $map 1374 $cf::MAP{$path} = $map
1352 } 1375 }
1353} 1376}
1354 1377
1378sub pre_load { }
1379sub post_load { }
1380
1355sub load { 1381sub load {
1356 my ($self) = @_; 1382 my ($self) = @_;
1357 1383
1358 my $path = $self->{path}; 1384 my $path = $self->{path};
1359 my $guard = cf::lock_acquire "map_load:$path"; 1385 my $guard = cf::lock_acquire "map_load:$path";
1361 return if $self->in_memory != cf::MAP_SWAPPED; 1387 return if $self->in_memory != cf::MAP_SWAPPED;
1362 1388
1363 $self->in_memory (cf::MAP_LOADING); 1389 $self->in_memory (cf::MAP_LOADING);
1364 1390
1365 $self->alloc; 1391 $self->alloc;
1392
1393 $self->pre_load;
1394
1366 $self->_load_objects ($self->{load_path}, 1) 1395 $self->_load_objects ($self->{load_path}, 1)
1367 or return; 1396 or return;
1368 1397
1369 $self->set_object_flag (cf::FLAG_OBJ_ORIGINAL, 1) 1398 $self->set_object_flag (cf::FLAG_OBJ_ORIGINAL, 1)
1370 if delete $self->{load_original}; 1399 if delete $self->{load_original};
1392 unless $self->difficulty; 1421 unless $self->difficulty;
1393 Coro::cede; 1422 Coro::cede;
1394 $self->activate; 1423 $self->activate;
1395 } 1424 }
1396 1425
1426 $self->post_load;
1427
1397 $self->in_memory (cf::MAP_IN_MEMORY); 1428 $self->in_memory (cf::MAP_IN_MEMORY);
1398} 1429}
1399 1430
1400sub customise_for { 1431sub customise_for {
1401 my ($self, $ob) = @_; 1432 my ($self, $ob) = @_;
1456 1487
1457 undef $MAP_PREFETCH{$path}; 1488 undef $MAP_PREFETCH{$path};
1458 $MAP_PREFETCHER ||= cf::async { 1489 $MAP_PREFETCHER ||= cf::async {
1459 while (%MAP_PREFETCH) { 1490 while (%MAP_PREFETCH) {
1460 for my $path (keys %MAP_PREFETCH) { 1491 for my $path (keys %MAP_PREFETCH) {
1461 warn "prefetching $path...\n";#d#
1462 my $map = find $path 1492 my $map = find $path
1463 or next; 1493 or next;
1464 $map->load; 1494 $map->load;
1465 1495
1466 delete $MAP_PREFETCH{$path}; 1496 delete $MAP_PREFETCH{$path};
2477 2507
2478$WRITE_RUNTIME_WATCHER = Event->timer ( 2508$WRITE_RUNTIME_WATCHER = Event->timer (
2479 data => WF_AUTOCANCEL, 2509 data => WF_AUTOCANCEL,
2480 after => 1, 2510 after => 1,
2481 interval => 10, 2511 interval => 10,
2512 prio => 6, # keep it lowest so it acts like a watchdog
2482 cb => Coro::unblock_sub { 2513 cb => Coro::unblock_sub {
2483 write_runtime 2514 write_runtime
2484 or warn "ERROR: unable to write runtime file: $!"; 2515 or warn "ERROR: unable to write runtime file: $!";
2485 }, 2516 },
2486); 2517);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines