Class: Teien::EventRouter

Inherits:
Object
  • Object
show all
Includes:
Dispatcher
Defined in:
lib/teien/core/event_router.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Dispatcher

#notify, #notify_reversely, #register_receiver

Constructor Details

#initializeEventRouter

Returns a new instance of EventRouter.



13
14
15
16
# File 'lib/teien/core/event_router.rb', line 13

def initialize()
  super()
  @quit = false
end

Instance Attribute Details

#quitObject

Returns the value of attribute quit.



11
12
13
# File 'lib/teien/core/event_router.rb', line 11

def quit
  @quit
end

Instance Method Details

#connect_to_server(ip = nil, port = 11922, tick_period = 0) ⇒ Object

def connect_to_server(ip = nil, port = 11922, tick_period = 0.001)



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/teien/core/event_router.rb', line 77

def connect_to_server(ip = nil, port = 11922, tick_period = 0)
  notify(:setup)

  @last_time = 0
  EM.run do
    EM.add_periodic_timer(tick_period) do
      if @last_time == 0
        @last_time = Time.now.to_f 
      else
        now = Time.now.to_f
        delta = now - @last_time
        @last_time = now

        notify(:update, delta)

        if @quit
          EM.stop 
          notify_reversely(:finalize)
#            Teien::get_component("base_object_manager").finalize()
        end
      end
    end
      
    Signal.trap("INT")  { EM.stop; notify_reversely(:finalize) }
    Signal.trap("TERM") { EM.stop; notify_reversely(:finalize) }

    if (ip)
      EM.connect(ip, port, ClientNetwork, self)
    end
  end
end

#connection_binded(from) ⇒ Object



18
19
20
# File 'lib/teien/core/event_router.rb', line 18

def connection_binded(from)
  notify(:connection_binded, from)
end

#connection_completed(from) ⇒ Object



22
23
24
# File 'lib/teien/core/event_router.rb', line 22

def connection_completed(from)
  notify(:connection_completed, from)
end

#connection_unbinded(from) ⇒ Object



26
27
28
# File 'lib/teien/core/event_router.rb', line 26

def connection_unbinded(from)
  notify(:connection_unbinded, from)
end

#receive_event(event, from) ⇒ Object



30
31
32
# File 'lib/teien/core/event_router.rb', line 30

def receive_event(event, from)
  notify(:receive_event, event, from)
end

#send_event(event, to = nil) ⇒ Object

send a event to receivers and connections. to = nil means to send the event to all connections.



36
37
38
39
40
41
42
43
# File 'lib/teien/core/event_router.rb', line 36

def send_event(event, to = nil)
  if to
    to.send_object(event)
  else
    Network::send_event_to_all(event)
  end
  notify(:receive_event, event, nil)
end

#start_application(tick_period = 0.001) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/teien/core/event_router.rb', line 109

def start_application(tick_period = 0.001)
  notify(:setup)

  @last_time = 0
  EM.run do
    EM.add_periodic_timer(tick_period) do
      if @last_time == 0
        @last_time = Time.now.to_f 

        # dummy connection for local
        Network::add_dummy_connection(nil)
        notify(:connection_binded, nil)
        notify(:connection_completed, nil)
      else
        now = Time.now.to_f
        delta = now - @last_time
        @last_time = now

        notify(:update, delta)

        if @quit
          EM.stop 
          Teien::get_component("base_object_manager").finalize()
        end
      end
    end

    Signal.trap("INT")  { EM.stop; Teien::get_component("base_object_manager").finalize() }
    Signal.trap("TERM") { EM.stop; Teien::get_component("base_object_manager").finalize() }
  end
end

#start_server(ip = nil, port = 11922, tick_period = 0.001) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/teien/core/event_router.rb', line 45

def start_server(ip = nil, port = 11922, tick_period = 0.001)
  notify(:setup)

  @last_time = 0
  EM.run do
    EM.add_periodic_timer(tick_period) do
      if @last_time == 0
        @last_time = Time.now.to_f 
      else
        now = Time.now.to_f
        delta = now - @last_time
        @last_time = now

        notify(:update, delta)

        if @quit
          EM.stop 
          Teien::get_component("base_object_manager").finalize()
        end
      end
    end

    Signal.trap("INT")  { EM.stop; Teien::get_component("base_object_manager").finalize() }
    Signal.trap("TERM") { EM.stop; Teien::get_component("base_object_manager").finalize() }

    if (ip)
      EM.start_server(ip, port, ServerNetwork, self)
    end
  end
end