Class: MPW::SyncMPW

Inherits:
Object
  • Object
show all
Defined in:
lib/mpw/sync/mpw.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, user, password, suffix, port = nil) ⇒ SyncMPW

Constructor @args: host -> the server host

port -> ther connection port
gpg_key -> the gpg key
password -> the remote password
suffix -> the suffix file


22
23
24
25
26
27
28
29
30
31
# File 'lib/mpw/sync/mpw.rb', line 22

def initialize(host, user, password, suffix, port=nil)
  @error_msg = nil
  @enable    = false

  @host     = host
  @port     = !port.instance_of?(Integer) ? 2201 : port
  @gpg_key  = user
  @password = password
  @suffix   = suffix
end

Instance Attribute Details

#enableObject

Returns the value of attribute enable.



14
15
16
# File 'lib/mpw/sync/mpw.rb', line 14

def enable
  @enable
end

#error_msgObject

Returns the value of attribute error_msg.



13
14
15
# File 'lib/mpw/sync/mpw.rb', line 13

def error_msg
  @error_msg
end

Instance Method Details

#connectObject

Connect to server @rtrn: false if the connection fail



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mpw/sync/mpw.rb', line 35

def connect
  Timeout.timeout(10) do
    begin
      TCPSocket.open(@host, @port) do 
        @enable = true
      end
              rescue Errno::ENETUNREACH
        retry
    end
  end
rescue Timeout::Error
  @error_msg = "#{I18n.t('error.timeout')}\n#{e}"
  @enable    = false
rescue Exception => e
  @error_msg = "#{I18n.t('error.sync.connection')}\n#{e}"
  @enable    = false
else
  return @enable
end

#get(file_tmp) ⇒ Object

Get data on server @args: gpg_password -> the gpg password @rtrn: nil if nothing data or error



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
# File 'lib/mpw/sync/mpw.rb', line 58

def get(file_tmp)
  return false if not @enable

  msg = nil
  TCPSocket.open(@host, @port) do |socket|
    send_msg = {action:  'get',
                gpg_key:  @gpg_key,
                password: @password,
                suffix:   @suffix
               }
    
    socket.puts send_msg.to_json
    msg = JSON.parse(socket.gets)
  end
  
  if not defined?(msg['error'])
    @error_msg = I18n.t('error.sync.communication')
    return false
  elsif not msg['error'].nil?
    @error_msg = I18n.t(msg['error'])
    return false
  end

  File.open(file_tmp, 'w') do |file|
    file << msg['data']
  end

  return true
rescue Exception => e
  @error_msg = "#{I18n.t('error.sync.download')}\n#{e}"
  return false
end

#update(file_gpg) ⇒ Object

Update the remote data @args: data -> the data to send on server @rtrn: false if there is a problem



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
# File 'lib/mpw/sync/mpw.rb', line 94

def update(file_gpg)
  return true if not @enable
  
  data = File.open(file_gpg, 'r').read

  msg = nil
  TCPSocket.open(@host, @port) do |socket|
    send_msg = {action:   'update',
                gpg_key:  @gpg_key,
                password: @password,
                suffix:   @suffix,
                data:     data
               }
    
    socket.puts send_msg.to_json
    msg = JSON.parse(socket.gets)
  end
  
  if not defined?(msg['error'])
    @error_msg = I18n.t('error.sync.communication')
    return false
  elsif msg['error'].nil?
    return true
  else
    @error_msg = I18n.t(msg['error'])
    return false
  end
end