Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 173 lines (143 sloc) 5.392 kb
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
#!/usr/bin/env perl

use v5.014;
use strict;
use warnings;

my $root;

# cargo-culted from the pocketio source
BEGIN {
    use File::Basename ();
    use File::Spec ();

    $root = File::Basename::dirname(__FILE__);
    $root = File::Spec->rel2abs($root);
}

use PocketIO;
use PocketIO::Pool::Redis;

use JSON;
use Plack::App::File;
use Plack::Builder;
use Plack::Middleware::Static;

builder {
    # file includes
    mount '/' =>
        Plack::App::File->new(file => "$root/templates/index.html");

    mount '/static/style.css' =>
        Plack::App::File->new(file => "$root/static/style.css");

    mount '/static/ace/ace.js' =>
        Plack::App::File->new(file => "$root/static/ace/ace.js");

    mount '/static/ace/theme-monokai.js' =>
        Plack::App::File->new(file => "$root/static/ace/theme-monokai.js");

    mount '/static/ace/mode-perl.js' =>
        Plack::App::File->new(file => "$root/static/ace/mode-perl.js");

    mount '/static/script.js' =>
        Plack::App::File->new(file => "$root/static/script.js");

    mount '/socket.io/socket.io.js' =>
        Plack::App::File->new(file => "$root/static/socket.io.js");

    mount '/socket.io/static/flashsocket/WebSocketMain.swf' =>
        Plack::App::File->new(file => "$root/static/WebSocketMain.swf");

    mount '/socket.io/static/flashsocket/WebSocketMainInsecure.swf' =>
        Plack::App::File->new(file =>
            "$root/static/WebSocketMainInsecure.swf");

    # actual event handling

    mount '/socket.io' => PocketIO->new(
        pool => PocketIO::Pool::Redis->new,
        handler => sub {
            my $self = shift;

            $self->on(
                'start' => sub {
                    use Data::UUID;
                    use Redis::Handle;
                    use YAML qw(Dump);

                    my ($self,$code) = @_;
                    my $uuid = Data::UUID->new->create_str();
                    tie local *RUNNER, 'Redis::Handle', 'bluequeue';
                    print RUNNER Dump({
                        type => 'run',
                        uuid => $uuid,
                        code => $code,
                    });
                    close RUNNER;
                    $self->send(PocketIO::Message->new(
                            type => 'event',
                            data => {
                                name => 'started',
                                args => {
                                    uuid => $uuid,
                                    success => 1,
                                },
                            },
                        )
                    );
                }
            );

            $self->on(
                'recv' => sub {
                    use Redis::Handle;
                    my ($self,$uuid) = @_;

                    my $in = tie local *APPIN, 'Redis::Handle', "$uuid:out";

                    $in->poll_once(sub {
                        my @messages = @_;
                        map {
                            $self->send(PocketIO::Message->new(
                                    type => 'event',
                                    data => {
                                        name => 'recvd',
                                        args => {
                                            response => $_,
                                            success => 1,
                                        },
                                    },
                                )
                            );
                        } @messages;
                        close APPIN;
                    });
                }
            );

            $self->on(
                'kill' => sub {
                    use Redis::Handle;
                    use YAML qw/Dump/;

                    my ($self,$uuid,$signal) = @_;

                    tie local *RUNNER, 'Redis::Handle', 'bluequeue';
                    print RUNNER Dump({
                        type => 'signal',
                        uuid => $uuid,
                        signal => $signal,
                    });
                    close RUNNER;

                    $self->send(PocketIO::Message->new(
                            type => 'event',
                            data => {
                                name => 'signalled',
                                args => {
                                    success => 1,
                                    uuid => $uuid,
                                },
                            },
                        )
                    );
                }
            );

            $self->on(
                'send' => sub {
                    use Redis::Handle;
                    my ($self,$arr) = @_;
                    my ($uuid,$input) = @$arr;

                    tie local *APPOUT, 'Redis::Handle', "$uuid:in";
                    print APPOUT "$input";
                    close APPOUT;

                    $self->send(PocketIO::Message->new(
                            type => 'event',
                            data => {
                                name => 'sent',
                                args => {
                                    success => 1,
                                },
                            }
                        )
                    );
                }
            );
        }
    );
}
Something went wrong with that request. Please try again.