Class: PasteHub::ClientSync

Inherits:
Object
  • Object
show all
Defined in:
lib/pastehub/clientsync.rb

Constant Summary collapse

ICONSOCKET =
"/tmp/pastehub_icon"

Instance Method Summary collapse

Constructor Details

#initialize(hostname, polling_interval) ⇒ ClientSync

Returns a new instance of ClientSync.



111
112
113
114
115
116
117
118
# File 'lib/pastehub/clientsync.rb', line 111

def initialize( hostname, polling_interval )
  @hostname             = hostname
  @polling_interval     = polling_interval
  @status               = Status.new()
  @last_modify_time     = Time.now()
  @plugin               = PasteHub::Plugin
  @plugin.load_plugins
end

Instance Method Details

#clipboard_checkObject



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/pastehub/clientsync.rb', line 230

def clipboard_check()
  config = PasteHub::Config.instance
  util   = PasteHub::Util.new

  STDERR.puts "Info: clipboardCheck thread start"
  @prevData = ""
  while true
    sleep @polling_interval
    data = PasteHub::AbstractClipboard.hasNew?( )
    if data
      if @prevData != data
        entry = Entry.new( @hostname )
        entry.save( data )
        STDERR.printf( "Info: clipboard to File ([[%s]]] size=%d).\n", util.stringLimit(data,config.notifyMessageMax) , data.size )
        notifyToReceive( data )
        @prevData = data
      end
    end
    STDERR.printf( "." )    if config.getVerbose( )
  end
end

#display_configObject



120
121
122
# File 'lib/pastehub/clientsync.rb', line 120

def display_config
  @plugin.display_config
end

#exist_sync_data?Boolean

return: pathname of sync data of other host

Returns:

  • (Boolean)


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/pastehub/clientsync.rb', line 139

def exist_sync_data?()
  # check directory changes
  config = PasteHub::Config.instance

  paths = get_other_hostfiles()
  result = paths.select { |path|
    fs = File::Stat.new( path )
    fs.mtime

    #printf( "last_modify_time: %s \n", @last_modify_time )
    #printf( "mtime of [%s]: %s \n", path, fs.mtime )
    1 == (fs.mtime <=> @last_modify_time)
  }
  if 0 < result.size
    result[0]
  else
    nil
  end
end

#get_latest_entryObject



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/pastehub/clientsync.rb', line 173

def get_latest_entry( )
  # check directory changes
  config = PasteHub::Config.instance

  paths = get_other_hostfiles()
  result = paths.map { |path|
    hostname = path_to_hostname( path )
    entry = Entry.new( hostname )
    if entry.can_load?()
      ret = entry.load()
      [ ret[0]['create_unixtime'].to_i, ret[0], ret[1] ]
    else
      [ 0, nil, nil ]
    end
  }
  if 0 == result.size()
    nil
  else 
    result = result.sort {|a,b| a[0] <=> b[0]}.reverse
    [
     result[0][1], result[0][2]
    ]
  end
end

#get_other_hostfilesObject

host’s sync data file excludes local hostname



129
130
131
132
133
134
135
136
# File 'lib/pastehub/clientsync.rb', line 129

def get_other_hostfiles()
  config = PasteHub::Config.instance
  arr = Dir.glob( config.localSyncPath + "*.dat" )
  retArr = arr.select { |path|
    not path.match( "/" + @hostname + ".dat$" )
  }
  retArr
end

#get_sync_entry_body(path) ⇒ Object



163
164
165
166
167
168
169
170
171
# File 'lib/pastehub/clientsync.rb', line 163

def get_sync_entry_body( path )
  hostname = path_to_hostname( path )
  entry = Entry.new( hostname )
  if entry.can_load?()
    entry.load()[1]
  else
    nil
  end
end

#notifyToReceive(message) ⇒ Object



124
125
126
# File 'lib/pastehub/clientsync.rb', line 124

def notifyToReceive(message)
  @plugin.distribute_newly_arrived(message)
end

#path_to_hostname(path) ⇒ Object



159
160
161
# File 'lib/pastehub/clientsync.rb', line 159

def path_to_hostname( path )
  File.basename( path, ".dat" )
end

#sync_mainObject



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/pastehub/clientsync.rb', line 202

def sync_main()
  config = PasteHub::Config.instance
  util   = PasteHub::Util.new

  STDERR.puts "Info: sync_main thread start"
  free_counter = 0

  while true
    free_counter += 1

    path = exist_sync_data?()
    if path
      body = get_sync_entry_body( path )
      if body
        STDERR.printf( "Info: push to OS's clipboard ([[%s]] size=%d).\n", util.stringLimit(body,config.notifyMessageMax), body.size )
        PasteHub::AbstractClipboard.push( body.dup )
        touch()
      else
        STDERR.printf( "X" )   if config.getVerbose( )
      end
    else
      STDERR.printf( "x" )    if config.getVerbose( )
    end
    # interval time
    sleep @polling_interval
  end
end

#touchObject



198
199
200
# File 'lib/pastehub/clientsync.rb', line 198

def touch( )
  @last_modify_time     = Time.now()
end