Sha256: d6c99d6884e547e6ccdf81b807194ef0c80a721613327e9a080810abfde2b62f

Contents?: true

Size: 1.64 KB

Versions: 1

Compression:

Stored size: 1.64 KB

Contents

#!/usr/local/bin/ruby -w

# = A Command Line Client (clc) for the Sync Server
#
# code: tml, drak
#
# (c) 2004 Navel, all rights reserved.
# $Id: clc.rb 71 2004-10-18 10:50:22Z gmosx $

require "logger"
require "socket"
require "thread"
require "timeout"
require "getoptlong"

@opts = GetoptLong.new(
  [ "--server", "-s", GetoptLong::REQUIRED_ARGUMENT ],
  [ "--port", "-p",  GetoptLong::REQUIRED_ARGUMENT ],
  [ "--file", "-f", "-l", GetoptLong::REQUIRED_ARGUMENT ]
)

def prompt
	printf(Time.now.strftime("%H:%M:%S> "))
end

def connect(srv, port)
	begin
		@socket = TCPSocket.new(srv, port)
	rescue
		$log.error "Cannot connect to server, is it online?"
		exit(0)
	end
	$log.info "Connected to #{@socket.addr[3]}:#{@socket.addr[1]}"
end

def send(str)
	$log.info "write: #{str}"
	@socket.send("#{str}\000", 0)
end

def read()
	begin
		loop do
			timeout(0.7) do
				while @socket.readline("\000")
					$log.info "read: #{$_}"
				end
			end
		end
	rescue TimeoutError
		#drink it
	end
end

def write(cmd)
	if cmd = cmd.strip() and not cmd.empty?
		send(cmd)
	 else
		 $log.error "Syntax error"
	 end
end

if __FILE__ == $0

	$log = Logger.new(STDERR)

	@opts.each { |opt, arg|
		case opt
			when "--file"
				@file = File.new(arg)
			when "--server"
				@server = arg
			when "--port"
				port = port.to_i
		end
	}

	@server ||= "localhost"
	@port ||= 2121
	
	connect(@server, @port)
	
	reader = Thread.new {
		loop do
			read()
		end
	}

	writer = Thread.new {
		# replay input file
		if @file
			@file.each_line { |cmd|
				write(cmd)				
			}
		end

		# normal rep loop
		loop do
			prompt()
			cmd = gets()
			write(cmd)
		end
	}

	reader.join()
	writer.join()
	
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nitro-0.1.2 lib/n/sync/clc.rb