Created
April 20, 2010 01:12
-
-
Save eric/371880 to your computer and use it in GitHub Desktop.
Simple TCP server that can be used as an AGI server for PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# A simple tool to run php-based AGIs | |
# | |
# by Eric Lindvall <[email protected]> | |
# | |
# Usage: agi_handler.rb 4755 'php php_agi.php' | |
# | |
require 'eventmachine' | |
module DataLogger | |
def log(dir, data) | |
puts "#{dir} #{data.strip.gsub(/\n/, "\n ")}" | |
end | |
end | |
module AgiClient | |
include DataLogger | |
def initialize(server) | |
@server = server | |
end | |
def receive_data(data) | |
log('<-', data) | |
@server.send_data(data) | |
end | |
def unbind | |
@server.close_connection_after_writing | |
end | |
end | |
module AgiServer | |
include DataLogger | |
def initialize(agi_command) | |
@agi_command = agi_command | |
end | |
def post_init | |
@client = EM.popen(@agi_command, AgiClient, self) | |
end | |
def receive_data(data) | |
log('->', data) | |
@client.send_data(data) | |
end | |
def unbind | |
@client.close_connection_after_writing | |
end | |
end | |
unless ARGV[1] | |
puts "Usage: #{File.basename($0)} <port> <agi-file>" | |
exit 1 | |
end | |
port, agi_command = *ARGV | |
EM.run { | |
EM.start_server('0.0.0.0', port, AgiServer, agi_command) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment