Sha256: fa16c8ed470aea5ef8c17fb3b8ab553095513c10a2c1814c1b30fb8bc71c28f8

Contents?: true

Size: 1.73 KB

Versions: 1

Compression:

Stored size: 1.73 KB

Contents

require 'pp'

class ClientTCPSocketPackagedPipe
  MAX_PKG_SIZE=100000
  def initialize(host,port)
    @socket=TCPSocket.new(host,port)
    @socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
    @inPackages=[]
    recvLoop
  end
  
  def writePackage(str)
    @socket.write(str)
    @socket.flush
  end
  def recvPackage
    @inPackages.shift
  end
  
  private
  def recvLoop
    Thread.new {
      begin
        loop do
          data=@socket.recv(MAX_PKG_SIZE)
          if data.length>0
            @inPackages << data
          else
            break
          end
        end
      rescue Object=>e
        pp e,e.backtrace
      end
    }
  end
end

class ServerTCPSocketPackagedPipe
  attr_accessor :socket
  
  class Server<GServer
    def initialize(listen,port,client)
      super(port,listen,1)
      @client=client
    end
    def serve(io)
      puts "SERVE"
      io.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
      @conn=io
      puts "set sock"
      @client.socket=io
      puts "loop..."
      receiveLoop
      @client.socket=nil
    end
    def receiveLoop
      begin
       loop do
       puts "recv..."
       curdata=@conn.recv(1012)
       puts "..."
         if curdata.length>0
           pp "GOT DATA (at #{@conn.addr}) #{curdata.length}"
         else
           puts "NO MORE DATA"
           break
         end
         
         @client.push(curdata)
         
       end
      rescue Exception=>e
        #glog e,e.backtrace,self
        #unless e.inspect.to_s=="#<RuntimeError: stop>"
          pp e,e.backtrace,self
        #end
      end

    end
  end

  def initialize(listen,port)
    @socket=nil
    @server=Server.new(listen,port,self)
    @packages=[]
    
  end
  
  def writePackage(str)
    
  end
end


Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
appswarm-0.0.1 lib/appswarm/tools/pipes.rb