-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpubSubListener.pl
190 lines (131 loc) · 4.51 KB
/
pubSubListener.pl
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
#!usr/bin/perl -w
use strict;
use HTTP::Daemon;
use Frontier::RPC2;
use HTTP::Date;
###
### Pub Sub Listener
### Listens for xml-rpc pubsub requests, writes the necessary to a data file.
###
### Version 1.0 12:28 19:March:2002
###
### Author: Ben Hammersley, ben@benhammersley.com
###
### Big thanks to:
### Ken MacLeod for Frontier::* and much advice. Joe Johnston for snippets of server code,
### Noah Grey for Greymatter, Dave Winer for xml-rpc and the cloud idea,
### and all the other Perl hackers, Linux geeks and madarse blogger types.
###
### This is free software. Give it away. Share the love. Tell me if you make good changes.
### Look for the latest version at http://hacks.benhammersley.com/blogging/pubsub/
###
### Pub Sub Listener comes with two other scripts. PubSubNotifier and PubSubStrimmer,
### It's pretty important you have all three.
###
### Try http://hacks.benhammersley.com/blogging/pubsub/
###
# ------USER CHANGABLE VARIABLES HERE -------
my $listeningport = "8888";
# -------------------------------------------
# -----NON-CHANGABLE VARIABLES ARE SETUP HERE
my $methods = {'test' => \&test,
'echotest' => \&echotest,
'pleaseNotify' => \&pleaseNotify
};
our $host = "";
#--------------------------------------------
# --------------- Start the server up ------------------------
my $listen_socket = HTTP::Daemon->new( LocalPort => $listeningport,
Listen => 20,
Proto => 'tcp',
Reuse => 1
);
die "Can't create a listening socket: $@" unless $listen_socket;
# ------------------------------------------------
# ------------- Handle the connection ----------------------
while (my $connection = $listen_socket->accept) {
$host = $connection->peerhost;
interact($connection);
$connection->close;
}
# ----------------------------------------------------------
# ------------- The Interact subroutine, as called when a peer connects
sub interact {
my $sock = shift;
my $req;
eval {
$req = $sock->get_request;
};
# Check to see if the contact is both xml and to the right path.
if( $req->header('Content-Type') eq 'text/xml'&& $req->url->path eq '/RPC2')
{
my $coder = Frontier::RPC2->new('encoding' => 'ISO-8859-1');
my $hash = $coder->decode($req->content);
my $res_xml = $coder->serve($req->content,$methods);
if( $main::Fork ){
my $pid = fork();
unless( defined $pid ){
# check this response
my $res = HTTP::Response->new(500,'Internal Server Error');
$sock->send_status_line();
$sock->send_response($res);
}
if( $pid == 0 ){
$sock->close;
$main::Fork->();
exit;
}
$main::Fork = undef;
}
my $conn_host = gethostbyaddr($sock->peeraddr,AF_INET) || $sock->peerhost;
my $res = HTTP::Response->new(200,'OK');
$res->header(
date => time2str(),
Server => 'PubSubServer',
Content_Type => 'text/xml',
);
$res->content($res_xml);
$sock->send_response($res);
# ---------------------------------------------------------------------
#
#
#
# ------------------- Define the method subroutines
#
#
#
# ---- the TEST method-----
# Takes no argument, just returns a message
sub test {
return "The pubsub server seems to be running";
};
# ---------------
# ---- the ECHO TEST method-----
# Takes one argument, and returns it
sub echotest {
my ($echotest) = @_;
return "Pubsub server: You said $echotest";
};
#---------------------
# ---- pleaseNotify -----
# The main method - see the pod, read on for details.
sub pleaseNotify {
# Take the input and split it into five parameters:
#
# $parameterName - the name of the procedure that the cloud should call to notify the workstation of changes,
# $port - the TCP port the workstation is listening on,
# $path - the path to its responder,
# $protocol - a string indicating which protocol to use (xml-rpc or soap, case-sensitive),
# $rssUrl - and a list of urls of RSS files to be watched.
my ($parameterName, $port, $path, $protocol, $rssUrl) = @_;
# Work out the time, in Unix-Time-Since-Epoch.
my $time = time();
# Append the variables to the datafile, comma seperated, then add new-line.
open (DATA, ">>notifyServer.txt");
print DATA "$host,$parameterName,$port,$path,$protocol,$rssUrl,$time\n";
close DATA;
# Return true value to user as per spec.
};
# ----------------
};
};