Sha256: 386266fa820b9ad405d83dee88da70da7b26f11052122d6e3222d838eea71a3c

Contents?: true

Size: 1.73 KB

Versions: 1

Compression:

Stored size: 1.73 KB

Contents

require File.expand_path('../abstract_node.rb',__FILE__)

class DrbNetNode<AbstractNetNode
  include DRbUndumped
  
  def initialize(port,app)
    begin
      super(port,app,:drb)
      taint
    
      @uri="druby://#{address}:#{port}"
      log @uri  
      @service=DRb.start_service(@uri,self)
      log "listening on #{@uri}"
    rescue Exception=>e
      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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
appswarm-0.0.1 apps/network_node/drb_node.rb