Class: DeepConnect::Organizer

Inherits:
Object
  • Object
show all
Extended by:
SingleForwardable
Defined in:
lib/deep-connect/organizer.rb,
lib/deep-connect/organizer.rb

Constant Summary collapse

IPADDR_REGEXP =
/(::ffff:)?([0-9]+\.){3}[0-9]+|[0-9a-f]+:([0-9a-f]*:)[0-9a-f]*/
@@ABSOLUTE_IMMUTABLE_CLASSES =
[
  NilClass,
  TrueClass,
  FalseClass,
  Symbol,
  Fixnum,
  Bignum,
  Range,
  Rational
]
@@DEFAULT_IMMUTABLE_CLASSES =
[
  Numeric,
  String,
  Regexp,
  MatchData,
  Range,
  Time,
  File::Stat,
  Matrix,
  Vector,
  Matrix::Scalar
]
@@IMMUTABLE_CLASSES =
@@ABSOLUTE_IMMUTABLE_CLASSES + 
@@DEFAULT_IMMUTABLE_CLASSES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOrganizer

Returns a new instance of Organizer.



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
# File 'lib/deep-connect/organizer.rb', line 46

def initialize
  @shallow_connect = false

  @accepter = Accepter.new(self)
  @evaluator = Evaluator.new(self)

  @services = {}
  @services_mx = Mutex.new
  @services_cv = ConditionVariable.new

  @messageqs = {}
  @messageqs_mx = Mutex.new
  @messageqs_cv = ConditionVariable.new

  @deep_spaces = {}
  @deep_spaces_mon = Monitor.new
  @deep_spaces_cv = @deep_spaces_mon.new_cond

  @cron = Cron.new(self)

  @when_connect_proc = proc{true}
  @when_disconnect_proc = proc{}

  @local_id_mutex = Mutex.new
  @local_id_cv = ConditionVariable.new
  @local_id = nil
end

Instance Attribute Details

#accepterObject (readonly)

Returns the value of attribute accepter.



77
78
79
# File 'lib/deep-connect/organizer.rb', line 77

def accepter
  @accepter
end

#evaluatorObject (readonly)

Returns the value of attribute evaluator.



78
79
80
# File 'lib/deep-connect/organizer.rb', line 78

def evaluator
  @evaluator
end

#shallow_connectObject Also known as: shallow_connect?

Returns the value of attribute shallow_connect.



74
75
76
# File 'lib/deep-connect/organizer.rb', line 74

def shallow_connect
  @shallow_connect
end

Class Method Details

.absolute_immutable_classesObject



336
337
338
# File 'lib/deep-connect/organizer.rb', line 336

def self.absolute_immutable_classes
  @@ABSOLUTE_IMMUTABLE_CLASSES
end

.default_immutable_classesObject



339
340
341
# File 'lib/deep-connect/organizer.rb', line 339

def self.default_immutable_classes
  @@DEFAULT_IMMUTABLE_CLASSES
end

.immutable_classesObject



342
343
344
# File 'lib/deep-connect/organizer.rb', line 342

def self.immutable_classes
  @@IMMUTABLE_CLASSES
end

Instance Method Details

#close_deep_space(deep_space) ⇒ Object Also known as: close_deepspace



145
146
147
# File 'lib/deep-connect/organizer.rb', line 145

def close_deep_space(deep_space)
  disconnect_deep_space(deep_space)
end

#connect_deep_space_with_port(port, local_id = nil) ⇒ Object Also known as: connect_deepspace_with_port

sessionサービス開始



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/deep-connect/organizer.rb', line 170

def connect_deep_space_with_port(port, local_id = nil)
  @deep_spaces_mon.synchronize do
	deep_space = DeepSpace.new(self, port, local_id)
	port.attach(deep_space.session)
#      uuid = session.peer_id unless uuid
	if @deep_spaces[deep_space.peer_uuid]
	# ポート番号が再利用されているときは, 既存の方はすでにおなくな
	# りになっている
	  old = @deep_spaces[deep_space.peer_uuid]
	  puts "INFO: port no recyicled"
	  puts "INFO: disconnect recycled deep_space: #{old}"

	  disconnect_deep_space(old, :SESSION_CLOSED)
	end
	unless @when_connect_proc.call deep_space, port
	  puts "CONNECT Canceld DeepSpace: #{deep_space.peer_uuid}" if Conf.DEBUG
	  connect_ev = Event::ConnectResult.new(false)
	  port.export connect_ev

	  disconnect_deep_space(deep_space)
	  DC::Raise ConnectCancel, deep_space
	end

	connect_ev = Event::ConnectResult.new(true)
	port.export connect_ev

	ev = port.import
	if ev.kind_of?(Event::ConnectResult)
	  unless ev.result
 DC::Raise ConnectionRefused, deep_space
	  end
	else
	  DC::Raise ProtocolError, deep_space
	end

	@deep_spaces[deep_space.peer_uuid] = deep_space

	puts "CONNECT DeepSpace: #{deep_space.peer_uuid}" if Conf.DEBUG
	deep_space.connect
	deep_space
  end
end

#deep_space(peer_id, &block) ⇒ Object Also known as: deepspace



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/deep-connect/organizer.rb', line 150

def deep_space(peer_id, &block)
  @deep_spaces_mon.synchronize do
	if deep_space = @deep_spaces[peer_id]
	  return deep_space
	end

	# セッションを自動的に開く
	begin
	  deep_space = open_deep_space(*peer_id)
	  block.call deep_space if block_given?
	  deep_space
	rescue ConnectionRefused, Errno::ECONNREFUSED
	  puts "WARN: クライアント(#{peer_id}への接続が拒否されました"
	  return DeepSpaceNoConnection.new(peer_id)
	end
  end
end

#deep_spacesObject



84
85
86
# File 'lib/deep-connect/organizer.rb', line 84

def deep_spaces
  @deep_spaces
end

#disconnect_deep_space(deep_space, *opts) ⇒ Object



214
215
216
217
218
219
220
# File 'lib/deep-connect/organizer.rb', line 214

def disconnect_deep_space(deep_space, *opts)
  @deep_spaces_mon.synchronize do
	@deep_spaces.delete(deep_space.peer_uuid)
  end
  deep_space.disconnect(*opts)
  @when_disconnect_proc.call(deep_space, opts)
end

#export_mq(name) ⇒ Object Also known as: register_mq

MQ



265
266
267
268
269
# File 'lib/deep-connect/organizer.rb', line 265

def export_mq(name)
  @messageqs_mx.synchronize do
    @messageqs[name] = DeepMQ::SV.new(self)
  end
end

#get_mq(name, waitp = false) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/deep-connect/organizer.rb', line 272

def get_mq(name, waitp = false)
  @messageqs_mx.synchronize do
    until @messageqs.key?(name)
      if waitp
        @messageqs_cv.wait(@messageqs_mx)
      else
        return nil
      end
    end
    @messageqs[name]
  end
end

#id2obj(id) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/deep-connect/organizer.rb', line 291

def id2obj(id)
  @deep_spaces_mon.synchronize do
	for peer_id, s in @deep_spaces
	  if o = s.root(id) and !o.kind_of?(IllegalObject)
#	  if o = s.root(id) and o != :__DEEPCONNECT_NO_VALUE__
 return o
	  end
	end
# 	begin
# 	  ObjectSpace._id2ref(id)
# 	rescue
# 	end
# 	sleep 5
	IllegalObject.new(id)
  end
#      DC::InternalError "deep_spaceにid(=#{id})をobject_idとするオブジェクトが登録されていません.)"
end

#keep_aliveObject



231
232
233
234
235
236
237
238
# File 'lib/deep-connect/organizer.rb', line 231

def keep_alive
  puts "KEEP ALIVE: Start" if Conf.DISPLAY_KEEP_ALIVE
  for uuid, deep_space in @deep_spaces.dup
	unless deep_space.session.keep_alive
	  disconnect_deep_space(deep_space, :SESSION_CLOSED)
	end
  end
end

#local_idObject



88
89
90
91
92
93
94
95
# File 'lib/deep-connect/organizer.rb', line 88

def local_id
  @local_id_mutex.synchronize do
	while !@local_id
	  @local_id_cv.wait(@local_id_mutex)
	end
  end
  @local_id
end

#open_deep_space(host, port) ⇒ Object Also known as: open_deepspace

client sesssion開始



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/deep-connect/organizer.rb', line 112

def open_deep_space(host, port)

  @deep_spaces_mon.synchronize do
	ipaddr = nil
	if IPADDR_REGEXP !~ host
	  Resolv.each_address(host) do |addr|
 ipaddr = IPAddr.new(addr)
 ipaddr = ipaddr.native.to_s

 peer_id = [ipaddr, port]
 if deep_space = @deep_spaces[peer_id]
   return deep_space
 end
	  end
	else
	  ipaddr = IPAddr.new(host)
	  ipaddr = ipaddr.native.to_s

	  peer_id = [ipaddr, port]
	  if deep_space = @deep_spaces[peer_id]
 return deep_space
	  end
	end

	sock = TCPSocket.new(ipaddr, port)
	port = Port.new(sock)
	init_session_ev = Event::InitSessionEvent.new(local_id)
	port.export init_session_ev
	connect_deep_space_with_port(port)
  end
end

#register_service(name, obj) ⇒ Object Also known as: export

services



242
243
244
245
246
247
# File 'lib/deep-connect/organizer.rb', line 242

def register_service(name, obj)
  @services_mx.synchronize do
	@services[name] = obj
	@services_cv.broadcast
  end
end

#release_object(obj) ⇒ Object



285
286
287
288
289
# File 'lib/deep-connect/organizer.rb', line 285

def release_object(obj)
  for id, dspace in @deep_spaces.dup
	dspace.release_object(obj)
  end
end

#service(name, waitp = false) ⇒ Object Also known as: import



250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/deep-connect/organizer.rb', line 250

def service(name, waitp = false)
  @services_mx.synchronize do
	until @services.key?(name)
	  if waitp
 @services_cv.wait(@services_mx)
	  else
 return :DEEPCONNECT_NO_SUCH_SERVICE 
	  end
	end
	@services[name]
  end
end

#start(service) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/deep-connect/organizer.rb', line 97

def start(service)
  @accepter.open(service)
  @local_id = @accepter.port_number
  @local_id_cv.broadcast

  @accepter.start
  @cron.start
end

#stopObject



106
107
108
# File 'lib/deep-connect/organizer.rb', line 106

def stop
  @accepter.stop
end

#tickObject



80
81
82
# File 'lib/deep-connect/organizer.rb', line 80

def tick
  @cron.tick
end

#when_connected(&block) ⇒ Object



222
223
224
# File 'lib/deep-connect/organizer.rb', line 222

def when_connected(&block)
  @when_connect_proc = block
end

#when_disconnected(&block) ⇒ Object



226
227
228
# File 'lib/deep-connect/organizer.rb', line 226

def when_disconnected(&block)
  @when_disconnect_proc = block
end