Sha256: 1f88bd0d11b2fb9f8b75ae35a7a5f832199398718bba026a73dbbb243c4be1b1

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

require 'forwardable'
require 'mkfifo'

module Fluent
  module PluginNamedPipe
    class Fifo
      extend Forwardable

      READ_TIMEOUT = 1

      def initialize(file_path, mode = :r)
        if !File.exist?(file_path)
          File.mkfifo(file_path)
          File.chmod(0666, file_path)
        end

        @file_path = file_path
        @mode = mode
        self.open

        @buf = ''
      end

      def_delegators :@pipe, :read, :write, :close, :flush

      def open
        m = {:r => 'r+', :w => 'w+'}[@mode]
        @pipe = File.open(@file_path, m)
      end

      def readline
        res = IO.select([@pipe], [], [], READ_TIMEOUT)
        return nil if res.nil?

        while nil == (idx = @buf.index("\n")) do
          tmp = ''
          begin
            s = @pipe.sysread(0xffff, tmp)
            @buf << s
          rescue EOFError
            # reopen
            @pipe.close
            @pipe.open
          end
        end

        line = @buf[0, idx + 1]
        @buf = @buf[idx + 1, @buf.length - line.length]
        return line
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fluent-plugin-named_pipe-0.2.0 lib/fluent/plugin/named_pipe/fifo.rb