-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipc_server.rb
90 lines (77 loc) · 2.7 KB
/
ipc_server.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
90
module IpcServer
class << self
attr_accessor :connection
def send_packet(*values)
return unless connection
packet = "#{values.join("~~~")}\n\n\n"
#log :magenta, "[IPC] Sending packet: #{packet.inspect}"
connection.send_data(packet)
end
def proxy_events(*event_names)
event_names.each do |event_name|
on(event_name) do |*args|
send_packet(event_name, *args)
end
end
end
end
proxy_events :initializing, :updating_client, :checking_for_updates, :checking_files, :hashing_status,
:request_delete_files_confirmation, :waiting_for_processes, :up_to_date, :restarting
def post_init
IpcServer.connection = self
log "IPC client connected"
start_application
end
def receive_data(data)
data.split("\n\n\n").each do |line|
#log "[GUI Client] Said: #{line}"
on_packet(*line.split('~~~'))
end
end
def on_packet(name, *values)
case name
when 'pause'
on :pause, values.first == 'True'
when 'confirm_delete_files'
on :delete_files_confirmation_received, values.first == 'True'
when 'shutdown'
send_data "bye\n\n\n"
close_connection
end
end
def unbind
IpcServer.connection = nil
$exiting = true
EM.stop
end
end
on :sync_started do |total_bytes|
IpcServer.send_packet :sync_started, human_readable_byte_size(total_bytes)
end
on :sync_status do |percentage, downloading_count, incomplete_count, downloaded_size, current_speed, time_remaining|
size = human_readable_byte_size(downloaded_size)
time_remaining_in_words = time_remaining ? distance_of_time_in_words(time_remaining) : ''
IpcServer.send_packet :sync_status, percentage, downloading_count, incomplete_count, size, current_speed, time_remaining_in_words
end
on :sync_complete do |downloaded_file_count, duration|
IpcServer.send_packet :sync_complete, downloaded_file_count, distance_of_time_in_words(duration)
end
on :time_remaining_until_next_check do |seconds|
IpcServer.send_packet :time_remaining_until_next_check, distance_of_time_in_words(seconds)
end
def check_parent_process
parent_process = $wmi.ExecQuery("SELECT * FROM win32_process WHERE ProcessId = #{Process.ppid}").each.first
if parent_process
parents_parent_pid = parent_process.ParentProcessId
return if $wmi.ExecQuery("SELECT * FROM win32_process WHERE ProcessId = #{parents_parent_pid}").each.count > 0
log "Parent processes parent (#{parents_parent_pid}) no longer exists, exiting..."
else
log "Parent process (#{Process.ppid}) no longer exists, exiting..."
end
$exiting = true
EM.stop
end
EM.schedule do
check_parent_process
EM.add_periodic_timer(0.5) { check_parent_process }
end