Class: NServer::Server

Inherits:
GServer
  • Object
show all
Defined in:
lib/nserver.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Server

Create a NServer::Server instance

Options:

port

Port for the server to listen on. Default is 10001

host

Host (IP) for the server to listen on. Default is 127.0.0.1 Note that if you specify a non-localhost IP here you must specifiy an allow list. Also accepts 0.0.0.0 to listen on all available interfaces.

allow

Comma seperated list of IPs to accept connections from. Default is only 127.0.0.1 IP ranges are allowed, in a x.x.x.x/y format.

check_interval

Interval to check for messages. Default is 10s.

tray_icon

GTK Icon name to use for the systray icon. Default is ‘gnome-gmush’.

msg_timeout

Message display timeout. Default is 5000ms (5s).



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/nserver.rb', line 30

def initialize( opts = {} )
  opts[:port] ||= 10001
  opts[:host] ||= "127.0.0.1"
  
  super(opts[:port], opts[:host])

  opts[:check_interval] ||= 10
  opts[:tray_icon] ||= "gnome-gmush"
  opts[:msg_timeout] ||= 5000
  opts[:allow] = ( opts[:allow] or "127.0.0.1" ).split(',').collect {|i| IPAddr.new( i ) }

  @check_interval = opts[:check_interval]

  @msg_list = [ Message.new("NServer Ready.") ]
  @allowed_clients = opts[:allow]

  Notify.init("Notification Server")
  tray_icon = Gtk::StatusIcon.new
  tray_icon.icon_name = opts[:tray_icon]
  #tray_icon.file = File.join( File.dirname(__FILE__), 'icons', 'default.png')
  tray_icon.tooltip = "NServer"

  @notification = Notify::Notification::new("X", nil, nil, tray_icon)
  @notification.timeout = opts[:msg_timeout]

  Thread.new { Gtk.main }
  sleep 5
  tray_icon.embedded? || raise("Failed to setup tray icon.")

  ## Setup msg thread
  @msg_thread = Thread.new(self, @check_interval) do |server, intv|
    loop do
      server.process
      sleep(intv)
    end
  end
end

Instance Method Details

#add_message(msg, priority = :normal) ⇒ Object

Add a message to the message queue



76
77
78
# File 'lib/nserver.rb', line 76

def add_message(msg, priority = :normal)
  @msg_list << Message.new(msg, priority)
end

#notify(msg) ⇒ Object

Display the message



69
70
71
72
73
# File 'lib/nserver.rb', line 69

def notify(msg)
  @notification.update("Message Received", msg.text, nil)
  @notification.urgency = msg.priority
  @notification.show
end

#processObject

Display the next message, if any.



81
82
83
84
85
# File 'lib/nserver.rb', line 81

def process
  if @msg_list.size > 0
    notify(@msg_list.shift)
  end
end

#serve(io) ⇒ Object

Get and add a message to the list. GServer method.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/nserver.rb', line 88

def serve(io)
  unless @allowed_clients.include?( IPAddr.new(io.peeraddr.last) )
    io.puts "#{io.addr.last} is not allowed."
    return
  end
  io.puts("Enter message, terminate with line break.")
  msg = io.gets.strip

  if msg == "EXITNOW"
    notify("Shutting down.")
    self.shutdown
  else
    priority = :normal
    if msg =~ /^(.*?):/
      if Message::PRIORITIES.include?( $1.downcase.to_sym )
        priority = $1.downcase.to_sym
        msg.gsub!( /^#{$1}:/, '' )
      end
    end
    add_message(msg, priority)
    io.puts "OK"
  end
end

#shutdownObject

On server shutdown, also do GUI shutdown stuff.



113
114
115
116
117
# File 'lib/nserver.rb', line 113

def shutdown
  Notify.uninit
  Gtk.main_quit
  self.stop
end