Class: Domodoro::Server

Inherits:
EM::Connection
  • Object
show all
Includes:
EM::P::ObjectProtocol
Defined in:
lib/domodoro/server.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(channel, schedule) ⇒ Server

Returns a new instance of Server.



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

def initialize(channel, schedule)
  @channel = channel
  @schedule = schedule
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



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

def channel
  @channel
end

Class Method Details



30
31
32
33
34
35
36
37
38
39
# File 'lib/domodoro/server.rb', line 30

def print_time
  hour = Time.now.hour.to_s.rjust(2, '0')
  min  = Time.now.min.to_s.rjust(2, '0')
  secs = Time.now.sec.to_s.rjust(2, '0')
  $stdout.print "\r"
  $stdout.print " " * 20
  $stdout.print "\r"
  $stdout.print "#{hour}:#{min}:#{secs}"
  $stdout.flush
end

.start(host = '0.0.0.0', port = '9111') ⇒ Object



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

def start(host='0.0.0.0', port='9111')
  schedule = Schedule.new
  schedule.generate!

  puts "#{Time.now} - Domodoro serving at #{host}:#{port}"
  EM.run do
    channel = Channel.new

    EM.start_server(host, port, self, channel, schedule)

    EM.add_periodic_timer(1) do
      if Time.now.sec == 0
        channel.broadcast(timestamp, schedule)
      else
        EM.next_tick do
          print_time
        end
      end
    end
  end
end

.timestampObject



41
42
43
44
45
# File 'lib/domodoro/server.rb', line 41

def timestamp
  hour = Time.now.hour.to_s.rjust(2, '0')
  min  = Time.now.min.to_s.rjust(2, '0')
  [hour, min].join(':')
end

Instance Method Details

#post_initObject



53
54
55
56
57
58
59
# File 'lib/domodoro/server.rb', line 53

def post_init
  @sid = channel.subscribe { |m| send_object(m) }
  send_object({
    :current_action => @schedule.current_action(Server.timestamp),
    :next_action    => @schedule.action_after(Server.timestamp)
  })
end

#unbindObject



61
62
63
# File 'lib/domodoro/server.rb', line 61

def unbind
  channel.unsubscribe @sid
end