Class: LSync::Server

Inherits:
Object
  • Object
show all
Includes:
EventHandler
Defined in:
lib/lsync/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EventHandler

#abort!, #fire, #on, #try

Constructor Details

#initialize(host) ⇒ Server

Returns a new instance of Server.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/lsync/server.rb', line 9

def initialize(host)
  @host = host
  @root = "/"
  
  @platform = "generic"
  
  @shell = Shells::SSH.new
  
  @enabled = true
  @connection = nil
  @pid = nil
end

Instance Attribute Details

#hostObject

The host name (e.g. DNS entry) for the given server



23
24
25
# File 'lib/lsync/server.rb', line 23

def host
  @host
end

#platformObject

The platform of the server, e.g. linux, used for executing actions.



29
30
31
# File 'lib/lsync/server.rb', line 29

def platform
  @platform
end

#rootObject

The root path on the server in which all other directories will be relative to.



26
27
28
# File 'lib/lsync/server.rb', line 26

def root
  @root
end

#shellObject

The shell to use to connect to the server.



32
33
34
# File 'lib/lsync/server.rb', line 32

def shell
  @shell
end

Instance Method Details

#connectObject

Connect to the server using the given #shell and #connection_string.



71
72
73
74
75
76
77
# File 'lib/lsync/server.rb', line 71

def connect
  unless @connection
    @connection, @pid = @shell.connect(self)
  end

  return @connection
end

#connection_string(directory) ⇒ Object

Give a general connection string (e.g “host:/directory” or “/directory” if local).



42
43
44
45
46
47
48
# File 'lib/lsync/server.rb', line 42

def connection_string(directory)
  if is_local?
    return full_path(directory)
  else
    return @host + ":" + full_path(directory).dump
  end
end

#full_path(directory = "./") ⇒ Object

Give the full path for a particular subdirectory.



35
36
37
38
39
# File 'lib/lsync/server.rb', line 35

def full_path(directory = "./")
  p = File.expand_path(directory.to_s, @root)

  return Pathname.new(p).cleanpath.normalize_trailing_slash.to_s
end

#is_local?Boolean

Checks if the host resolves to the local machine.

Returns:

  • (Boolean)


51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/lsync/server.rb', line 51

def is_local?
  return true if @host == "localhost"

  hostname = Socket.gethostname

  begin
    hostname = Socket.gethostbyname(hostname)[0]
  rescue SocketError
    puts $!
  end

  return @host == hostname
end

#to_sObject

String representation of the server for logging.



66
67
68
# File 'lib/lsync/server.rb', line 66

def to_s
  "#{@host}:#{full_path}"
end