File: bzbz.pl

package info (click to toggle)
bzflag 2.4.30-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 26,488 kB
  • sloc: cpp: 150,376; ansic: 3,463; sh: 2,535; makefile: 2,194; perl: 486; python: 260; objc: 246; php: 206
file content (78 lines) | stat: -rwxr-xr-x 1,886 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/perl

# This is a simple script that uses bzadmin to create a chat gateway between
# two bzflag servers.
#
# Use it like this:
#
#   bzbz.pl <CALLSIGN1>@<HOST1>:<PORT1> <CALLSIGN2>@<HOST2>:<PORT2> [OPTIONS]
#
# The options will be given to both bzadmin processes.
#

use IPC::Open2;


# parse the command line
$bz1 = $ARGV[0];
$bz2 = $ARGV[1];
$options = join(' ', @ARGV[2 .. $#ARGV]);
$callsign1 = '@' . substr($bz1, 0, index($bz1, '@'));
$callsign2 = '@' . substr($bz2, 0, index($bz2, '@'));

print "Creating a gateway between $bz1 and $bz2...\n";

# start the two bzadmin processes
$pid1 = open2(\*RDRFH1, \*WTRFH1, "bzadmin -ui stdboth $bz1 $options");
$pid2 = open2(\*RDRFH2, \*WTRFH2, "bzadmin -ui stdboth $bz2 $options");

while (true) {

  # check for new data from the servers
  $rin = $win = $ein = '';
  vec($rin, fileno(RDRFH1), 1) = 1;
  vec($rin, fileno(RDRFH2), 1) = 1;
  select($rin, $win, $ein, 0.01);

  # new data from server 1, print it to server 2
  if (vec($rin, fileno(RDRFH1), 1) == 1) {
    sysread RDRFH1, $c, 1;
    if ($c ne "\n") {
      $line1 = $line1 . $c;
    }
    elsif (substr($line1, 0,
		  length("    $callsign1: ")) eq "    $callsign1: ") {
      $line1 = "";
    }
    else {
      $line1 = $line1 . $c;
      syswrite WTRFH2, $line1, length($line1);
      $line1 = "";
    }
  }

  # new data from server 2, print it to server 1
  if (vec($rin, fileno(RDRFH2), 1) == 1) {
    sysread RDRFH2, $c, 1;
    if ($c ne "\n") {
      $line2 = $line2 . $c;
    }
    elsif (substr($line2, 0, length("    $callsign2: ")) eq
	   "    $callsign2: ") {
      $line2 = "";
    }
    else {
      $line2 = $line2 . $c;
      syswrite WTRFH1, $line2, length($line2);
      $line2 = "";
    }
  }
}

# Local Variables: ***
# mode: Perl ***
# tab-width: 8 ***
# c-basic-offset: 2 ***
# indent-tabs-mode: t ***
# End: ***
# ex: shiftwidth=2 tabstop=8