Class: PasteHub::ClientSync
- Inherits:
-
Object
- Object
- PasteHub::ClientSync
- Defined in:
- lib/pastehub/clientsync.rb
Constant Summary collapse
- ICONSOCKET =
"/tmp/pastehub_icon"
Instance Method Summary collapse
- #addNoitfyCallback(receiveNotifyFunc) ⇒ Object
- #clipboard_check ⇒ Object
-
#exist_sync_data? ⇒ Boolean
return: pathname of sync data of other host.
- #get_latest_entry ⇒ Object
-
#get_other_hostfiles ⇒ Object
host’s sync data file excludes local hostname.
- #get_sync_entry_body(path) ⇒ Object
-
#initialize(hostname, polling_interval) ⇒ ClientSync
constructor
A new instance of ClientSync.
- #notifyToReceive(message) ⇒ Object
- #path_to_hostname(path) ⇒ Object
- #sync_main ⇒ Object
- #touch ⇒ Object
Constructor Details
#initialize(hostname, polling_interval) ⇒ ClientSync
Returns a new instance of ClientSync.
111 112 113 114 115 116 |
# 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() end |
Instance Method Details
#addNoitfyCallback(receiveNotifyFunc) ⇒ Object
118 119 120 |
# File 'lib/pastehub/clientsync.rb', line 118 def addNoitfyCallback( receiveNotifyFunc ) @receiveNotifyFunc = receiveNotifyFunc end |
#clipboard_check ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/pastehub/clientsync.rb', line 229 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 |
#exist_sync_data? ⇒ Boolean
return: pathname of sync data of other host
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/pastehub/clientsync.rb', line 138 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_entry ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/pastehub/clientsync.rb', line 172 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_hostfiles ⇒ Object
host’s sync data file excludes local hostname
128 129 130 131 132 133 134 135 |
# File 'lib/pastehub/clientsync.rb', line 128 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
162 163 164 165 166 167 168 169 170 |
# File 'lib/pastehub/clientsync.rb', line 162 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
122 123 124 125 |
# File 'lib/pastehub/clientsync.rb', line 122 def notifyToReceive( ) @receiveNotifyFunc.call( ) if @receiveNotifyFunc @status.inc( ) end |
#path_to_hostname(path) ⇒ Object
158 159 160 |
# File 'lib/pastehub/clientsync.rb', line 158 def path_to_hostname( path ) File.basename( path, ".dat" ) end |
#sync_main ⇒ Object
201 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 |
# File 'lib/pastehub/clientsync.rb', line 201 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 |
#touch ⇒ Object
197 198 199 |
# File 'lib/pastehub/clientsync.rb', line 197 def touch( ) @last_modify_time = Time.now() end |