Class: Aniview::Daemon
- Inherits:
-
Object
- Object
- Aniview::Daemon
- Defined in:
- lib/daemon.rb
Instance Method Summary collapse
- #checkFeeds ⇒ Object
- #help ⇒ Object
-
#initialize(args) ⇒ Daemon
constructor
A new instance of Daemon.
- #parseArgs(args) ⇒ Object
- #quit ⇒ Object
- #run ⇒ Object
- #uptime ⇒ Object
Constructor Details
#initialize(args) ⇒ Daemon
Returns a new instance of Daemon.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/daemon.rb', line 17 def initialize(args) @pref = Interface::Pref.new @c = Client::AniClient.new @pref args << "start" if args.length < 1 parseArgs args @schedule = Interface::Schedule.new @pref @delugec = Interface::DelugeC.new(@pref) @subscription = Interface::Subscription.new(@pref, @schedule, @delugec, @c) begin @s = TCPServer.new @pref.get("daemon")["port"] rescue Errno::EADDRINUSE puts "error, something is already listening on #{@pref.get("daemon")["port"]}, maybe the \nserver is already running?" quit end @looped = 0 @matches = 0 @lastchecked = "never" $stdout.reopen(@pref.parseDir(@pref.get("daemon_log_file")), "w") $stderr.reopen(@pref.parseDir(@pref.get("daemon_log_file")), "w") $stdout.sync = true $stderr.sync = true end |
Instance Method Details
#checkFeeds ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/daemon.rb', line 96 def checkFeeds Thread.abort_on_exception = true Thread.new do while true @subscription.updateFeed if @subscription.feed == nil @lastchecked = "offline" else @lastchecked = Time.now.to_s @schedule.load @matches += @subscription.matchAll(verbose: true) end #p @schedule.getAll @looped += 1 sleep Integer(@pref.get("rss_feed")["refresh_interval"]) end end end |
#help ⇒ Object
90 91 92 93 94 |
# File 'lib/daemon.rb', line 90 def help cmds = ["help", "start", "stop", "info", "up?", "nani"] puts "commands:" puts cmds.sort end |
#parseArgs(args) ⇒ Object
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/daemon.rb', line 46 def parseArgs args args.each { |a| case a when "help" help quit when "start" if @c.server? puts "already running" quit end when "stop" if @c.server? puts "quitting" @c.stopDaemon quit else puts "server isn't running" quit end when "info" if @c.server? puts @c.info quit else puts "server isn't running" quit end when "up?" puts @c.server? quit when "nani" puts @c.sendMsg("what?") quit when "items" p @c.getItems quit else puts "unknown command, dude!" quit end } end |
#quit ⇒ Object
117 118 119 120 |
# File 'lib/daemon.rb', line 117 def quit @checkThread.exit unless @checkThread == nil exit end |
#run ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/daemon.rb', line 126 def run puts "--------" puts "server started" puts "server listening on port " + @pref.get("daemon")["port"] trap(:QUIT) do quit end @checkThread = checkFeeds @started = Time.now loop do lastloop = false client = @s.accept = client.gets.chomp.split(" ") puts "got msg #{} from client" response = "okay." .each_with_index { |m, i| case m when "up?" response = "true" when "quit" lastloop = true response = "quitting" when "info" response = "uptime: #{uptime}, loops: #{@looped}, matches, #{@matches}" when "lastchecked" response = @lastchecked when "items" response = Util.encode_object(@schedule.getAll) when "itemshash" response = @schedule.getAllCereal else response = "what?" end } response += "\n" puts "responding with #{response}" client.puts response client.close quit if lastloop end end |
#uptime ⇒ Object
122 123 124 |
# File 'lib/daemon.rb', line 122 def uptime Util.format_duration(Time.now - @started) end |