summaryrefslogtreecommitdiff
path: root/conmux/conmux-registry
blob: 1f7a41718ca6afdf2e897751e78a12ce2f8d72c8 (plain)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/perl
#
# conmux-registry -- console name registry server
#
# Main registry server.  This server holds host/port assignments for
# conmux daemons registering with it.  This allows users to specify
# human names for their consoles and find the relevant conmux daemon.
#
# (C) Copyright IBM Corp. 2004, 2005, 2006
# Author: Andy Whitcroft <andyw@uk.ibm.com>
#
# The Console Multiplexor is released under the GNU Public License V2
#
use strict;

use FindBin;
use Symbol qw(gensym);

use IO::Socket;
use IO::Multiplex;
use URI::Escape;

# Find our internal libraries.
use lib $FindBin::Bin;
use Conmux;

our $P = 'conmux-registry';
our $debug = 0;

#
# LISTENER SOCKET: creates an intenet listener for new clients and
# connects them to the junction provided.
#
package ListenerSocket;

sub new {
	my ($class, $mux, $port, $registry) = @_;
	my $self = bless { 'mux' => $mux, 'registry' => $registry }, $class;

	print "ListenerSocket::new [$self] mux<$mux> port<$port> " .
		"registry<$registry>\n" if ($main::debug);

	$self->initialise($mux, $port, $registry);

	$self;
}

sub initialise {
	my ($self, $mux, $port, $registry) = @_;
	my ($sock);

	print "ListenerSocket::initialise [$self] mux<$mux> port<$port> " .
		"registry<$registry>\n" if ($main::debug);

	# Create a listening socket and add it to the multiplexor.
	my $sock = new IO::Socket::INET(Proto     => 'tcp',
					LocalPort => $port,
					Listen    => 4,
					ReuseAddr => 1)
		or die "socket: $@";

	print "  adding $self $sock\n" if ($main::debug);
	$mux->listen($sock);
	$mux->set_callback_object($self, $sock);
	$self->{'listener'} = $sock;
}

# Handle new connections by instantiating a new client class.
sub mux_connection {
	my ($self, $mux, $fh) = @_;
	my ($client);

	print "ListenerSocket::mux_connection [$self] mux<$mux> fh<$fh>\n"
		if ($main::debug);

	# Make a new client connection.
	$client = Client->new($mux, $fh, $self->{'registry'});
	print "  new connection $self $client\n" if ($main::debug);
}

sub DESTROY {
	my ($self) = @_;

	print "ListenerSocket::DESTROY [$self]\n" if ($main::debug);

	close($self->{'listener'});
}

#
# CLIENT: general client object, represents a remote client channel
#
package Client;

sub new {
	my ($class, $mux, $fh, $registry) = @_;
	my $self = bless { 'mux' => $mux,
			   'fh'  => $fh,
			   'registry' => $registry }, $class;

	print "Client::new [$self] mux<$mux> fh<$fh> registry<$registry>\n"
		if ($main::debug);

	$self->initialise($mux, $fh, $registry);

	$self;
}

sub initialise {
	my ($self, $mux, $fh, $registry) = @_;

	print "Client::initialise [$self] mux<$mux> fh<$fh> " .
		"registry<$registry>\n" if ($main::debug);

	$mux->set_callback_object($self, $fh);
}

sub mux_input {
	my ($self, $mux, $fh, $input) = @_;

	print "Client::mux_input [$self] mux<$mux> fh<$fh> input<$$input>\n"
		if ($main::debug);

        while ($$input =~ s/^(.*?)\n//) {
		my ($cmd, $args) = split(' ', $1, 2);
		my (%args) = Conmux::decodeArgs($args);

		my $reply = {
			'status' => 'ENOSYS',
		};

		# Fill in the common results.
		$reply->{'title'} = 'registry';

		# Handle this command.
		if ($cmd eq "LOOKUP") {
			my $r = $self->{'registry'}->lookup($args{'service'});

			if (defined $r) {
				$reply->{'result'} = $r;
				$reply->{'status'} = 'OK';

			} else {
				$reply->{'status'} = 'ENOENT entry not found';
			}

		} elsif ($cmd eq "ADD") {
			$self->{'registry'}->add($args{'service'},
				$args{'location'});
			$reply->{'status'} = 'OK';

		} elsif ($cmd eq "LIST") {
			$reply->{'result'} = $self->{'registry'}->list();
			$reply->{'status'} = 'OK';
		}

		$fh->write(Conmux::encodeArgs($reply) .  "\n");
	}
}
sub mux_eof {
	my ($self, $mux, $fh, $input) = @_;

	print "Client::mux_eof [$self] mux<$mux> fh<$fh> input<$input>\n"
		if ($main::debug);

	# Handle any pending input, then remove myself.
	$self->mux_input($mux, $fh, $input);

	# Tell the multiplexor we no longer are using this channel.
	$mux->shutdown($fh, 1);
}
sub mux_close {
	my ($self, $mux, $fn) = @_;

	print "Client::close [$self]\n" if ($main::debug);
}

sub DESTROY {
	my ($self) = @_;

	print "Client::DESTROY [$self]\n" if ($main::debug);
}

#
# REGISTRY: registry elements.
#
package Registry;

sub new {
	my ($class, $store) = @_;
	my $self = bless { 'store'  => $store }, $class;

	my ($key, $val);

	print "Registry::new [$self] store<$store>\n" if ($main::debug);

	# Open the store and populate the keys.
	open(S, '<', $store) || die "Registry::new: $store: open failed - $!\n";
	while (<S>) {
		chomp;

		($key, $val) = split(' ', $_);

		$self->{'key'}->{$key} = $val;
	}
	close(S);

	$self;
}

sub add {
	my ($self, $what, $where) = @_;

	my ($key);

	print "Registry::add [$self] what<$what> where<$where>\n"
		if ($main::debug);

	$self->{'key'}->{$what} = $where;

	print "$what at $where\n";

	if (open(S, '>', $self->{'store'} . '.new')) {
		foreach $key (sort keys %{$self->{'key'}}) {
			print S "$key $self->{'key'}->{$key}\n";
		}
		close(S);
		rename $self->{'store'} . '.new', $self->{'store'};

	} else {
		warn "$P: $self->{'store'}.new: open failed - $!";
	}
}

sub lookup {
	my ($self, $what) = @_;

	print "Registry::lookup [$self] what<$what>\n" if ($main::debug);

	$self->{'key'}->{$what};
}

sub list {
	my ($self) = @_;
	my ($r, $key);

	print "Registry::list [$self]\n" if ($main::debug);

	foreach $key (sort keys %{$self->{'key'}}) {
		$r .= "$key $self->{'key'}->{$key}\n";
	}

	$r;
}

#
# MAIN: makes the IO multiplexor, listener and registry and stitches
# them all together.
#
package main;

# Usage checks.
if ($#ARGV != 1) {
	print STDERR "Usage: $P <local port> <store>\n";
	exit 1
}
my ($lport, $store) = @ARGV;

# Make a new multiplexer.
my $mux  = new IO::Multiplex;

# Make the registry object.
my $registry = Registry->new($store);

# Create the client listener socket.
my $listener = ListenerSocket->new($mux, $lport, $registry);

# Hand over to the multiplexor.
$mux->loop;