Class: Mamemose::WebSocket::Server

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/mamemose/websocket.rb

Constant Summary collapse

@@update_send_message =
"updated"

Instance Method Summary collapse

Methods included from Util

#debug

Constructor Details

#initializeServer

Returns a new instance of Server.



16
17
18
19
20
# File 'lib/mamemose/websocket.rb', line 16

def initialize
  @connections = []
  @mutex = Mutex::new
  @tag = "WebSocket"
end

Instance Method Details

#fullpathsObject



101
102
103
# File 'lib/mamemose/websocket.rb', line 101

def fullpaths
  @connections.map{ |con| con[:fullpath] }
end

#get_mtime(fullpath) ⇒ Object



105
106
107
# File 'lib/mamemose/websocket.rb', line 105

def get_mtime(fullpath)
  File.mtime(fullpath) if File.exists?(fullpath)
end

#startObject



22
23
24
25
26
27
28
29
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
# File 'lib/mamemose/websocket.rb', line 22

def start
  Thread.new do
    debug(@tag, "start watcher...")
    watcher
  end

  EventMachine::WebSocket.start(:host => '0.0.0.0', :port => WS_PORT) do |ws|
    ws.onopen {
      debug(@tag, "connected.")
      ws.send("connected.")
    }

    ws.onmessage { |fullpath|
      # receive url from client
      debug(@tag, "receive: #{fullpath}")
      if File.exists?(fullpath)
        # connections are managed as tuple of (socket, url, mtime_cache)
        con = {:ws => ws, :fullpath => fullpath, :mtime_cache => get_mtime(fullpath)}
        @mutex.synchronize do
          @connections.push(con) unless @connections.index(con)
          debug(@tag, "added path to watch: #{fullpath}. now watch #{fullpaths.to_s}")
        end
      end
    }

    ws.onclose {
      debug(@tag, "closed.")
      # when a connection is closed, delete it from @connections
      @mutex.synchronize do
        @connections.delete_if { |con| con[:ws] == ws }
        debug(@tag, "closed and removed path. now watch #{fullpaths.to_s}")
      end
    }
  end
end

#watcherObject



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
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/mamemose/websocket.rb', line 58

def watcher
  loop do
    # gather paths to watch using mutex
    watch_fullpaths = []
    @mutex.synchronize do
      watch_fullpaths = fullpaths
    end

    # gather mtimes of watch_fullpaths
    mtimes = {}
    watch_fullpaths.uniq.each do |fullpath|
      if File.exists?(fullpath)
        # get mtime
        mtimes[fullpath] = get_mtime(fullpath)
      else
        # file no longer exists. remove the entry
        @mutex.synchronize do
          @connections.delete_if { |con| con[:fullpath] == fullpath }
          debug(@tag, "detected deletion: #{fullpath} and updated the list. now watch #{fullpaths.to_s}")
        end
      end
    end

    # push notification if watching file is updated
    to_notify = []
    @mutex.synchronize do
      @connections.each do |con|
        fullpath = con[:fullpath]
        if mtimes[fullpath] && con[:mtime_cache] < mtimes[fullpath]
          debug(@tag, "detected update: #{fullpath}. pushing...")
          con[:mtime_cache] = mtimes[fullpath]
          to_notify << con
        end
      end
    end
    to_notify.each do |con|
      con[:ws].send(@@update_send_message)
    end

    sleep 1
  end
end