seapig-server 0.0.7 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 26da727b4e61d1544cdaa389bc3f518163f6b5c9
4
- data.tar.gz: 213ea20302a34b0d6e7c628eccb41bef973cad3d
3
+ metadata.gz: a12affdf7b5e5fae828d4ec4711c9912dbb877e6
4
+ data.tar.gz: 0cff8ba163170572dc32683835dfd8a5b5492474
5
5
  SHA512:
6
- metadata.gz: 430a61c256dcc1cafcdff881488f318edea8b1739ca0aefb848122a3d9711affb4d14b14650dcb1892ed5e86a673c8c7e3f64ba281a0fc3a9df5ec2d71e959a4
7
- data.tar.gz: 818047817d947900e460a66bccafbe92bf40523024ed4c19cd9b7478cbc3b2265048bb903459522c347005a23b7aa683608e69079ec334d44123d328813d57da
6
+ metadata.gz: 2e3e2ee217ad73eedbfb54bbff9e19267a721fa78e2b4123658a0e03fb3b98ed58434bce5a72c0fe82c680413daa33bd931f38c0cc5e0f6d6d4081ddd7ba25b9
7
+ data.tar.gz: 61398b760963440170c373f8e1d6a4a29179070a5625353449afe15b4a477c165d0e945e69528ea009ead439916cb9c1eab0d7c65b10e22156c6faf8eb07b84c
data/bin/seapig-server CHANGED
@@ -7,6 +7,9 @@ require 'jsondiff'
7
7
  require 'hana'
8
8
  require 'set'
9
9
 
10
+ DEBUG = (ARGV[0] == "debug")
11
+ INFO = (DEBUG or ARGV[0] == "info")
12
+
10
13
 
11
14
  class String
12
15
 
@@ -28,7 +31,8 @@ class SeapigObject
28
31
  attr_reader :id, :version, :valid
29
32
 
30
33
  @@objects_by_id = Hash.new { |hash,object_id|
31
- puts "Creating: "+(object = SeapigObject.new(object_id)).id
34
+ object = SeapigObject.new(object_id)
35
+ puts "Creating: "+object.id if DEBUG
32
36
  hash[object_id] = object
33
37
  }
34
38
 
@@ -71,21 +75,21 @@ class SeapigObject
71
75
  @@objects_by_id.values.each { |object| object.version.keys.each { |key| used_object_ids << key } if object.version.kind_of?(Hash) } # objects that others depend on
72
76
  @@objects_by_id.keys.select { |id| not used_object_ids.include?(id) }.each { |id|
73
77
  Client.all.select { |client| client.consumes.find { |object| object.id.starexp? and id =~ object.id.starexp } }.each { |client|
74
- puts "Destroying: "+id
78
+ puts "Destroying: "+id if DEBUG
75
79
  client.socket.send JSON.dump(action: 'object-destroy', id: id)
76
80
  }
77
- puts "Deleting: "+id
81
+ puts "Deleting: "+id if DEBUG
78
82
  @@objects_by_id.delete(id)
79
83
  }
80
84
  end
81
85
 
82
86
 
83
87
  def patch(patch, value, from_version, new_version)
84
- print "Patching:\n        version:"+@version.inspect+"\n        from_version: "+from_version.inspect+"\n        new_version: "+new_version.inspect+"\n        patch_size: "+(patch and patch.size.to_s or "nil")+"\n        --> "
88
+ print "Patching:\n        version:"+@version.inspect+"\n        from_version: "+from_version.inspect+"\n        new_version: "+new_version.inspect+"\n        patch_size: "+(patch and patch.size.to_s or "nil")+"\n        --> " if DEBUG
85
89
  if from_version == @version or from_version == 0
86
- puts 'clean'
90
+ puts 'clean' if DEBUG
87
91
  if value and @stall_after
88
- puts '        UNSTALL'
92
+ puts '        UNSTALL' if DEBUG
89
93
  @version = @stall_after
90
94
  @stall_after = nil
91
95
  end
@@ -95,11 +99,11 @@ class SeapigObject
95
99
  begin
96
100
  Hana::Patch.new(patch).apply(@object) if patch
97
101
  rescue Exception => e
98
- puts "Patching failed!\n        Old object: "+old_object.inspect+"\n        Patch: "+patch.inspect
102
+ puts "Patching failed!\n        Old object: "+old_object.inspect+"\n        Patch: "+patch.inspect if DEBUG
99
103
  raise e
100
104
  end
101
105
  if value == false and not @stall_after
102
- puts '        STALL'
106
+ puts '        STALL' if DEBUG
103
107
  @stall_after = old_version
104
108
  end
105
109
  @object.merge!(value) if value
@@ -107,13 +111,13 @@ class SeapigObject
107
111
  Client.all.each { |client| upload(client, old_version, old_object) }
108
112
  SeapigObject.all.each { |object| object.check_validity }
109
113
  elsif from_version > @version
110
- puts "lost some updates, reinitializing object"
114
+ puts "lost some updates, reinitializing object" if DEBUG
111
115
  @version = 0
112
116
  @object.clear
113
117
  @valid = false
114
118
  #assign mb?
115
119
  else
116
- puts "late update, ignoring"
120
+ puts "late update, ignoring" if DEBUG
117
121
  end
118
122
  end
119
123
 
@@ -121,13 +125,15 @@ class SeapigObject
121
125
  def upload(client, old_version, old_object, patch = nil)
122
126
  return false if @stall_after
123
127
  return false if not client.consumes.find { |object| (object == self) or self.matches?(object.id) }
128
+ old_version, old_object = [0, {}] if not client.versions[self] == old_version
124
129
  json = JSON.dump(
125
130
  action: 'object-update',
126
131
  id: @id,
127
132
  old_version: old_version,
128
133
  new_version: @version,
129
134
  patch: (patch or JsonDiff.generate(old_object, @object)))
130
- puts "Sending %8iB %s to %s"%[json.size, self.id, client.id]
135
+ puts "Sending %8iB %s to %s"%[json.size, self.id, client.id] if DEBUG
136
+ client.versions[self] = @version
131
137
  client.socket.send json
132
138
  end
133
139
 
@@ -149,7 +155,7 @@ end
149
155
 
150
156
  class Client
151
157
 
152
- attr_reader :produces, :consumes, :socket, :producing, :index
158
+ attr_reader :produces, :consumes, :socket, :producing, :index, :versions
153
159
  attr_accessor :options
154
160
 
155
161
  @@clients_by_socket = {}
@@ -168,11 +174,12 @@ class Client
168
174
 
169
175
  def initialize(socket)
170
176
  @index = @@count += 1
171
- puts 'Creating client: '+@index.to_s
177
+ puts 'Client connected: '+@index.to_s if DEBUG
172
178
  @socket = socket
173
179
  @options = {}
174
180
  @produces = []
175
181
  @consumes = []
182
+ @versions = {}
176
183
  @producing = nil
177
184
  @@clients_by_socket[socket] = self
178
185
  end
@@ -184,7 +191,7 @@ class Client
184
191
 
185
192
 
186
193
  def destroy
187
- puts 'Destroying client: '+@index.to_s
194
+ puts 'Client disconnected: '+@index.to_s if DEBUG
188
195
  @@clients_by_socket.delete(@socket)
189
196
  SeapigObject.gc
190
197
  Client.all.find { |client| client.assign(@producing) } if @producing and SeapigObject.all.include?(@producing)
@@ -209,25 +216,26 @@ class Client
209
216
 
210
217
  def consumer_unregister(object)
211
218
  @consumes.delete(object)
219
+ @versions.delete(object)
212
220
  SeapigObject.gc
213
221
  end
214
222
 
215
223
 
216
224
  def assign(object)
217
- puts 'Assign? %20s <> %-30s - %s'%[self.id, object.id, [object.valid,object.id.starexp?,@producing,Client.all.find { |client| client.producing == object },(not @produces.find { |pattern| object.id =~ pattern.starexp })].map { |b| b and 'T' or 'F' }.join('')]
225
+ puts 'Assign? %20s <> %-30s - %s'%[self.id, object.id, [object.valid,object.id.starexp?,@producing,Client.all.find { |client| client.producing == object },(not @produces.find { |pattern| object.id =~ pattern.starexp })].map { |b| b and 'T' or 'F' }.join('')] if DEBUG
218
226
  return true if object.valid
219
227
  return true if object.id.starexp?
220
228
  return false if @producing
221
229
  return true if Client.all.find { |client| client.producing == object }
222
230
  return false if not @produces.find { |pattern| object.id =~ pattern.starexp }
223
- puts 'Assigning: '+object.id+' to: '+self.id
231
+ puts 'Assigning: '+object.id+' to: '+self.id if DEBUG
224
232
  @socket.send JSON.dump(action: 'object-produce', id: object.id)
225
233
  @producing = object
226
234
  end
227
235
 
228
236
 
229
237
  def release(object)
230
- puts 'Releasing: '+object.id+' from: '+self.id
238
+ puts 'Releasing: '+object.id+' from: '+self.id if DEBUG
231
239
  @producing = nil if @producing == object
232
240
  end
233
241
 
@@ -254,7 +262,6 @@ class Client
254
262
  end
255
263
 
256
264
 
257
-
258
265
  processing_times = []
259
266
  processing_times_sum = 0
260
267
 
@@ -267,7 +274,9 @@ EM.run {
267
274
  started_at = Time.new
268
275
  client = Client[client_socket]
269
276
  message = JSON.load message
270
- puts "-"*80 + ' ' + Time.new.to_s + "\nMessage: %-20s %-30s %s:%s"%[client.id, message['action'], (message["id"] and "ID" or "Pattern"),(message["id"] or message["pattern"])]
277
+ puts "-"*80 + ' ' + Time.new.to_s if DEBUG
278
+ print "Message: %-20s %-30s %-50s"%[client.id, message['action'], JSON.dump(message.select { |k,v| ['pattern','id','options'].include?(k) })] if INFO
279
+ puts if DEBUG
271
280
  object = SeapigObject[message['id']] if message['id']
272
281
  case message['action']
273
282
  when 'object-producer-register'
@@ -296,10 +305,14 @@ EM.run {
296
305
  end
297
306
  processing_times << (Time.new.to_f - started_at.to_f)
298
307
  processing_times_sum += processing_times[-1]
299
- puts "Clients:\n"+Client.all.map { |client| "        %-20s produces:%s consumes:%s"%[client.id,client.produces.inspect,client.consumes.map { |obj| obj.id }] }.join("\n")+"\n"
300
- puts "Objects:\n"+SeapigObject.all.map { |object| "        %s"%[object.inspect] }.join("\n")+"\n"
301
- puts "Processing:\n        time: %.3fs\n        count: %i\n        average: %.3fs\n        total: %.3fs"%[processing_times[-1], processing_times.size, processing_times_sum / processing_times.size, processing_times_sum]
302
- STDOUT.flush
308
+ if DEBUG
309
+ puts "Clients:\n"+Client.all.map { |client| "        %-20s produces:%s consumes:%s"%[client.id,client.produces.inspect,client.consumes.map { |obj| obj.id }] }.join("\n")+"\n"
310
+ puts "Objects:\n"+SeapigObject.all.map { |object| "        %s"%[object.inspect] }.join("\n")+"\n"
311
+ puts "Processing:\n        time: %.3fs\n        count: %i\n        average: %.3fs\n        total: %.3fs"%[processing_times[-1], processing_times.size, processing_times_sum / processing_times.size, processing_times_sum]
312
+ STDOUT.flush
313
+ end
314
+ puts "ct:%3i t:%.3fs Σt:%.3fs t̅:%.3fs"%[processing_times.size, processing_times[-1], processing_times_sum, processing_times_sum / processing_times.size,] if INFO and not DEBUG
315
+
303
316
  }
304
317
 
305
318
 
@@ -1,3 +1,3 @@
1
1
  module Seapig
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: seapig-server
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - yunta
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-28 00:00:00.000000000 Z
11
+ date: 2016-06-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: websocket-eventmachine-server