Module: Sonia::Server

Extended by:
Server
Included in:
Server
Defined in:
lib/sonia/server.rb

Overview

Main Sonia event loop

Starts both WebServer and WebSocket server

Author:

  • Piotr Usewicz

Constant Summary collapse

WEBSOCKET_HOST =

Defaults

"localhost"
WEBSOCKET_PORT =
8080
WEBSERVER_HOST =
"localhost"
WEBSERVER_PORT =
3000

Instance Method Summary collapse

Instance Method Details

#configConfig

Returns configuration from the config file

Returns:


36
37
38
# File 'lib/sonia/server.rb', line 36

def config
  @@config
end

#configure(options) ⇒ Config

Loads the configuration file

Parameters:

  • options (Hash)

Returns:


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

def configure(options)
  @@config = Config.new(YAML.load_file(File.expand_path(options.config)))
end

#initialize_widgetsObject

Goes through configured widgets and initializes them


66
67
68
69
70
71
72
73
74
# File 'lib/sonia/server.rb', line 66

def initialize_widgets
  @widgets = []

  config.widgets.each do |widget, config|
    class_name = "Sonia::Widgets::#{widget.to_s}"
    log.info("Server") { "Created widget #{widget} with #{config.inspect}" }
    @widgets << module_eval(class_name).new(config)
  end
end

#logLogger

Returns [Logger] object

Returns:

  • (Logger)

51
52
53
# File 'lib/sonia/server.rb', line 51

def log
  Sonia.log
end

#run!(args, &block) ⇒ Object

Starts the server

Parameters:

  • args (Hash)

    Startup options


27
28
29
30
31
# File 'lib/sonia/server.rb', line 27

def run!(args, &block)
  @start_block = block
  configure(args)
  serve
end

#serveObject

Starts main [EventMachine] loop


56
57
58
59
60
61
62
63
# File 'lib/sonia/server.rb', line 56

def serve
  EventMachine.run {
    initialize_widgets
    start_web_socket_server
    start_web_server
    @start_block.call
  }
end

#start_web_serverObject

Starts Thin WebServer


131
132
133
134
135
136
137
# File 'lib/sonia/server.rb', line 131

def start_web_server
  Thin::Server.start(
    webserver_host,
    webserver_port,
    ::Sonia::WebServer
  )
end

#start_web_socket_serverObject

Starts WebSocket server


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/sonia/server.rb', line 98

def start_web_socket_server
  EventMachine::WebSocket.start(websocket_options) do |ws|
    ws.onopen {
      @widgets.map { |widget| widget.subscribe!(ws) }

      setup_message = { :setup => @widgets.map { |widget| widget.setup } }
      ws.send Yajl::Encoder.encode(setup_message)

      log.info("Server") { "Sent setup #{setup_message.inspect}" }

      @widgets.each { |widget|
        if widget.respond_to?(:initial_push)
          log.info(widget.widget_name) { "Sending initial push" }
          widget.initial_push
        end
      }
    }

    #ws.onmessage { |msg|
    #@widgets.each do |widget|
    #widget.push "<#{@sids}>: #{msg}"
    #end
    #}

    ws.onclose {
      @widgets.each { |widget| widget.unsubscribe! }
    }
  end

  log.info("Server") { "WebSocket Server running at #{websocket_options[:host]}:#{websocket_options[:port]}" }
end

#webserver_hostString

Returns configured webserver host

Returns:

  • (String)

    Webserver host


142
143
144
# File 'lib/sonia/server.rb', line 142

def webserver_host
  config.webserver.host || WEBSERVER_HOST
end

#webserver_portString

Returns configured webserver port

Returns:

  • (String)

    webserver port


149
150
151
# File 'lib/sonia/server.rb', line 149

def webserver_port
  config.webserver.port || WEBSERVER_PORT
end

#webserver_urlString

Returns WebServer URL

Returns:

  • (String)

    WebServer URL


156
157
158
# File 'lib/sonia/server.rb', line 156

def webserver_url
  "http://#{webserver_host}:#{webserver_port}"
end

#websocket_hostString

Returns configured websocket hostname

Returns:

  • (String)

    Websocket hostname


86
87
88
# File 'lib/sonia/server.rb', line 86

def websocket_host
  config.websocket.host || WEBSOCKET_HOST
end

#websocket_optionsHash

Returns websocket configuration options

Returns:

  • (Hash)

    Websocket's host and port number


79
80
81
# File 'lib/sonia/server.rb', line 79

def websocket_options
  { :host => websocket_host, :port => websocket_port }
end

#websocket_portString

Returns configured websocket port

Returns:

  • (String)

    Websocket port


93
94
95
# File 'lib/sonia/server.rb', line 93

def websocket_port
  config.websocket.port || WEBSOCKET_PORT
end

#websocket_urlString

Returns WebSocket URL

Returns:

  • (String)

    WebSocket URL


163
164
165
# File 'lib/sonia/server.rb', line 163

def websocket_url
  "ws://#{websocket_host}:#{websocket_port}"
end