Sha256: ce397139f7819d685f1f2dd227fb49564f680bad653b61d4a73bf7402ea34085

Contents?: true

Size: 1.71 KB

Versions: 2

Compression:

Stored size: 1.71 KB

Contents

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

# = A Command Line Client (clc) for the Sync Server
#
# code: 
#	* Anastasios Koutoumanos  <ak@navel.gr>
# * George Moschovitis  <gm@navel.gr>
#
# (c) 2004 Navel, all rights reserved.
# $Id: clc.rb 101 2004-10-22 12:35:39Z 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

2 entries across 2 versions & 1 rubygems

Version Path
nitro-0.2.0 lib/n/sync/clc.rb
nitro-0.3.0 lib/n/sync/clc.rb