Class: Pounder::Server

Inherits:
Object
  • Object
show all
Includes:
Command
Defined in:
lib/pounder/server.rb,
lib/pounder/server/command.rb

Defined Under Namespace

Modules: Command

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Command

#cmd_DELE, #cmd_LIST, #cmd_PASS, #cmd_QUIT, #cmd_RETR, #cmd_STAT, #cmd_USER

Class Method Details

.invoke(options) ⇒ Object



5
6
7
# File 'lib/pounder/server.rb', line 5

def self.invoke(options)
  new.listen(options)
end

Instance Method Details

#listen(options) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/pounder/server.rb', line 9

def listen(options)
  server = TCPServer.new(options[:port])
  puts "#{self.class}##{__method__} port=#{options[:port]}"

  maildir_path = "#{Dir::pwd}/.pounder"
  puts "Maildir: #{maildir_path}"
  maildir = Maildir.new(maildir_path)

  puts "From: #{options[:from_address]}" if options[:from_address]

  while true
    Thread.fork(server.accept) do |sock|
      p sock
      service(sock, sock, maildir, options)
    end
  end
end


44
45
46
# File 'lib/pounder/server.rb', line 44

def print_line(str)
  @output.print "#{str}\r\n"
end


48
49
50
# File 'lib/pounder/server.rb', line 48

def print_line_message(str)
  @output.print str.sub(/\A\./, "..")
end

#read_cmdObject



52
53
54
55
# File 'lib/pounder/server.rb', line 52

def read_cmd
  cmd_name, *args = @input.gets.split
  ["cmd_#{cmd_name.chomp}", args]
end

#service(input, output, maildir, options) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pounder/server.rb', line 27

def service(input, output, maildir, options)
  @input  = input
  @output = output
  print_line "+OK pounder"

  while cmds = read_cmd()
    cmd_name = cmds.shift
    args     = cmds.shift
    unless respond_to?(cmd_name)
      print_line "-ERR unknown command"
      next
    end

    __send__(cmd_name, maildir: maildir, args: args, options: options)
  end
end

#terminateObject



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/pounder/server.rb', line 57

def terminate
  begin
    @input.close_read
  rescue
  end

  begin
    @output.close_write
  rescue
  end

  Thread.exit
end