require File.expand_path('../abstract_node.rb',__FILE__) class DrbNetNodee log e.to_s log e.backtrace.join("\n") end @companions={} end def stop @service.stop_service end def connect(nodeId) log "#{id} connecting #{nodeId}..." return @companions[nodeId] if @companions[nodeId] begin server_uri="druby://#{nodeId.address}:#{nodeId.port}" c=DRbObject.new_with_uri(server_uri) @companions[nodeId]=c log "#{id} ready connected #{nodeId}!" log "NODES:",self.nodes c.connected(self) return c rescue Exception=>e log e.to_s log e.backtrace.join("\n") end nil end def close(nodeId) rnode=@companions[nodeId] return if rnode.nil? eventDisconnected(nodeId) @companions.delete(nodeId) rnode.close(node) end def eventDisconnected(pid) log "#{id} disconnected from #{pid}" end def sendTo(nodeId,*msg) node=@companions[nodeId] if node node.send(*msg) end end def sendAll(*msg) @companions.values.map{|node| log "send to node #{node.node}" log msg result=node.send(*msg) log "sent to node #{node.node}." result } end def info {node.to_s=>@companions.keys} end def connected(from) i=from.node log "#{id} connected from #{i}" @companions[i]=from log "NODES:",self.nodes eventConnected(i) end def nodes @companions.keys end end