File: echo_unix_server.rb

package info (click to toggle)
ruby-celluloid-io 0.15.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 352 kB
  • ctags: 164
  • sloc: ruby: 1,610; makefile: 5
file content (46 lines) | stat: -rw-r--r-- 819 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
require 'bundler/setup'
require 'celluloid/io'

class EchoUNIXServer
  include Celluloid::IO
  finalizer :finalize

  attr_reader :socket_path, :server

  def initialize(socket_path)
    puts "*** start server #{socket_path}"
    @socket_path = socket_path
    @server = UNIXServer.open(socket_path)
    async.run
  end

  def run
    loop { async.handle_connection @server.accept }
  end

  def handle_connection(socket)
    loop do
      data = socket.readline
      puts "*** gets data #{data}"
      socket.write(data)
    end

  rescue EOFError
    puts "*** disconnected"

  ensure
    socket.close
  end

  def finalize
    if @server
      @server.close
      File.delete(@socket_path)
    end
  end

end

supervisor = EchoUNIXServer.supervise("/tmp/sock_test")
trap("INT") { supervisor.terminate; exit }
sleep