web_socket.rb 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. require 'json'
  2. module Session
  3. @path = '/tmp/websocket'
  4. @@user_threads = {}
  5. @@client_threads = {}
  6. def self.create( client_id, session )
  7. path = @path + '/' + client_id.to_s
  8. FileUtils.mkpath path
  9. File.open( path + '/session', 'w' ) { |file|
  10. user = { :id => session['id'] }
  11. file.puts Marshal.dump(user)
  12. }
  13. end
  14. def self.get( client_id )
  15. session_file = @path + '/' + client_id.to_s + '/session'
  16. data = nil
  17. return if !File.exist? session_file
  18. File.open( session_file, 'r' ) { |file|
  19. all = ''
  20. while line = file.gets
  21. all = all + line
  22. end
  23. begin
  24. data = Marshal.load( all )
  25. rescue
  26. return
  27. end
  28. }
  29. return data
  30. end
  31. def self.transaction( client_id, data )
  32. path = @path + '/' + client_id.to_s + '/'
  33. filename = 'transaction-' + Time.new().to_i.to_s + '-' + rand(999999).to_s
  34. if File::exists?( path + filename )
  35. filename = filename + '-1'
  36. if File::exists?( path + filename )
  37. filename = filename + '-1'
  38. if File::exists?( path + filename )
  39. filename = filename + '-1'
  40. if File::exists?( path + filename )
  41. filename = filename + '-' + rand(999999).to_s
  42. end
  43. end
  44. end
  45. end
  46. return false if !File.directory? path
  47. File.open( path + 'a-' + filename, 'w' ) { |file|
  48. file.puts data.to_json
  49. }
  50. FileUtils.mv( path + 'a-' + filename, path + filename)
  51. return true
  52. end
  53. def self.jobs
  54. Thread.abort_on_exception = true
  55. while true
  56. client_ids = self.sessions
  57. client_ids.each { |client_id|
  58. # get current user
  59. user_session = Session.get( client_id )
  60. next if !user_session
  61. next if !user_session[:id]
  62. user = User.find( user_session[:id] )
  63. next if !user
  64. # start user thread
  65. start_user_thread = false
  66. if !@@user_threads[user.id]
  67. start_user_thread = true
  68. @@user_threads[user.id] = Thread.new {
  69. UserState.new(user.id)
  70. @@user_threads[user.id] = nil
  71. # raise "Exception from thread"
  72. }
  73. end
  74. # wait with client thread unil user thread has done some little work
  75. if start_user_thread
  76. sleep 0.4
  77. end
  78. # start client thread
  79. if !@@client_threads[client_id]
  80. @@client_threads[client_id] = Thread.new {
  81. ClientState.new(client_id)
  82. @@client_threads[client_id] = nil
  83. # raise "Exception from thread"
  84. }
  85. end
  86. }
  87. # system settings
  88. sleep 0.4
  89. end
  90. end
  91. def self.sessions
  92. path = @path + '/'
  93. data = []
  94. Dir.foreach( path ) do |entry|
  95. if entry != '.' && entry != '..'
  96. data.push entry
  97. end
  98. end
  99. return data
  100. end
  101. def self.queue( client_id )
  102. path = @path + '/' + client_id.to_s + '/'
  103. data = []
  104. Dir.foreach( path ) do |entry|
  105. if /^transaction/.match( entry )
  106. data.push Session.queue_file( path, entry )
  107. end
  108. end
  109. return data
  110. end
  111. def self.queue_file( path, filename )
  112. file_old = path + filename
  113. file_new = path + 'a-' + filename
  114. FileUtils.mv( file_old, file_new )
  115. data = nil
  116. all = ''
  117. File.open( file_new, 'r' ) { |file|
  118. while line = file.gets
  119. all = all + line
  120. end
  121. }
  122. File.delete( file_new )
  123. data = JSON.parse( all )
  124. return data
  125. end
  126. def self.destory( client_id )
  127. path = @path + '/' + client_id.to_s
  128. FileUtils.rm_rf path
  129. end
  130. end
  131. module CacheIn
  132. @@data = {}
  133. @@data_time = {}
  134. @@expires_in = {}
  135. @@expires_in_ttl = {}
  136. def self.set( key, value, params = {} )
  137. # puts 'CacheIn.set:' + key + '-' + value.inspect
  138. if params[:expires_in]
  139. @@expires_in[key] = Time.now + params[:expires_in]
  140. @@expires_in_ttl[key] = params[:expires_in]
  141. end
  142. @@data[ key ] = value
  143. @@data_time[ key ] = Time.now
  144. end
  145. def self.expired( key, params = {} )
  146. # expire if value never was set
  147. return true if !@@data.include? key
  148. # set re_expire
  149. if params[:re_expire]
  150. if @@expires_in[key]
  151. @@expires_in[key] = Time.now + @@expires_in_ttl[key]
  152. end
  153. return false
  154. end
  155. # check if expired
  156. if @@expires_in[key]
  157. return true if @@expires_in[key] < Time.now
  158. return false
  159. end
  160. # return false if key was set without expires_in
  161. return false
  162. end
  163. def self.get_time( key, params = {} )
  164. data = self.get( key, params )
  165. if data
  166. return @@data_time[key]
  167. end
  168. end
  169. def self.get( key, params = {} )
  170. # puts 'CacheIn.get:' + key + '-' + @@data[ key ].inspect
  171. return if self.expired( key, params )
  172. @@data[ key ]
  173. end
  174. end
  175. class UserState
  176. def initialize( user_id )
  177. @user_id = user_id
  178. @data = {}
  179. @cache_key = 'user_' + user_id.to_s
  180. self.log "---user started user state"
  181. CacheIn.set( 'last_run_' + user_id.to_s , true, { :expires_in => 20.seconds } )
  182. self.fetch
  183. end
  184. def fetch
  185. user = User.find( @user_id )
  186. return if !user
  187. while true
  188. # check if user is still with min one open connection
  189. if !CacheIn.get( 'last_run_' + user.id.to_s )
  190. self.log "---user - closeing thread - no open user connection"
  191. return
  192. end
  193. self.log "---user - fetch user data"
  194. # overview
  195. cache_key = @cache_key + '_overview'
  196. if CacheIn.expired(cache_key)
  197. overview = Ticket.overview(
  198. :current_user_id => user.id,
  199. )
  200. overview_cache = CacheIn.get( cache_key, { :re_expire => true } )
  201. self.log 'fetch overview - ' + cache_key
  202. if overview != overview_cache
  203. self.log 'fetch overview changed - ' + cache_key
  204. # puts overview.inspect
  205. # puts '------'
  206. # puts overview_cache.inspect
  207. CacheIn.set( cache_key, overview, { :expires_in => 3.seconds } )
  208. end
  209. end
  210. # overview lists
  211. overviews = Ticket.overview_list(
  212. :current_user_id => user.id,
  213. )
  214. overviews.each { |overview|
  215. cache_key = @cache_key + '_overview_data_' + overview.meta[:url]
  216. if CacheIn.expired(cache_key)
  217. overview_data = Ticket.overview(
  218. :view => overview.meta[:url],
  219. # :view_mode => params[:view_mode],
  220. :current_user_id => user.id,
  221. :array => true,
  222. )
  223. overview_data_cache = CacheIn.get( cache_key, { :re_expire => true } )
  224. self.log 'fetch overview_data - ' + cache_key
  225. if overview_data != overview_data_cache
  226. self.log 'fetch overview_data changed - ' + cache_key
  227. CacheIn.set( cache_key, overview_data, { :expires_in => 5.seconds } )
  228. end
  229. end
  230. }
  231. # create_attributes
  232. cache_key = @cache_key + '_ticket_create_attributes'
  233. if CacheIn.expired(cache_key)
  234. ticket_create_attributes = Ticket.create_attributes(
  235. :current_user_id => user.id,
  236. )
  237. ticket_create_attributes_cache = CacheIn.get( cache_key, { :re_expire => true } )
  238. self.log 'fetch ticket_create_attributes - ' + cache_key
  239. if ticket_create_attributes != ticket_create_attributes_cache
  240. self.log 'fetch ticket_create_attributes changed - ' + cache_key
  241. CacheIn.set( cache_key, ticket_create_attributes, { :expires_in => 2.minutes } )
  242. end
  243. end
  244. # recent viewed
  245. cache_key = @cache_key + '_recent_viewed'
  246. if CacheIn.expired(cache_key)
  247. recent_viewed = History.recent_viewed(user)
  248. recent_viewed_cache = CacheIn.get( cache_key, { :re_expire => true } )
  249. self.log 'fetch recent_viewed - ' + cache_key
  250. if recent_viewed != recent_viewed_cache
  251. self.log 'fetch recent_viewed changed - ' + cache_key
  252. recent_viewed_full = History.recent_viewed_fulldata(user)
  253. CacheIn.set( cache_key, recent_viewed, { :expires_in => 5.seconds } )
  254. CacheIn.set( cache_key + '_push', recent_viewed_full )
  255. end
  256. end
  257. # activity steam
  258. cache_key = @cache_key + '_activity_stream'
  259. if CacheIn.expired(cache_key)
  260. activity_stream = History.activity_stream( user )
  261. activity_stream_cache = CacheIn.get( cache_key, { :re_expire => true } )
  262. self.log 'fetch activity_stream - ' + cache_key
  263. if activity_stream != activity_stream_cache
  264. self.log 'fetch activity_stream changed - ' + cache_key
  265. activity_stream_full = History.activity_stream_fulldata( user )
  266. CacheIn.set( cache_key, activity_stream, { :expires_in => 0.75.minutes } )
  267. CacheIn.set( cache_key + '_push', activity_stream_full )
  268. end
  269. end
  270. # rss
  271. cache_key = @cache_key + '_rss'
  272. if CacheIn.expired(cache_key)
  273. url = 'http://www.heise.de/newsticker/heise-atom.xml'
  274. rss_items = RSS.fetch( url, 8 )
  275. rss_items_cache = CacheIn.get( cache_key, { :re_expire => true } )
  276. self.log 'fetch rss - ' + cache_key
  277. if rss_items != rss_items_cache
  278. self.log 'fetch rss changed - ' + cache_key
  279. CacheIn.set( cache_key, rss_items, { :expires_in => 2.minutes } )
  280. CacheIn.set( cache_key + '_push', {
  281. head: 'Heise ATOM',
  282. items: rss_items,
  283. })
  284. end
  285. end
  286. self.log "---/user-"
  287. sleep 1
  288. end
  289. end
  290. def log( data )
  291. puts "#{Time.now}:user_id(#{ @user_id }) #{ data }"
  292. end
  293. end
  294. class ClientState
  295. def initialize( client_id )
  296. @client_id = client_id
  297. @data = {}
  298. @pushed = {}
  299. self.log "---client start ws connection---"
  300. self.fetch
  301. self.log "---client exiting ws connection---"
  302. end
  303. def fetch
  304. loop_count = 0
  305. while true
  306. # get connection user
  307. user_session = Session.get( @client_id )
  308. return if !user_session
  309. return if !user_session[:id]
  310. user = User.find( user_session[:id] )
  311. return if !user
  312. loop_count += 1
  313. self.log "---client - looking for data of user #{user.id}"
  314. # remember last run
  315. CacheIn.set( 'last_run_' + user.id.to_s , true, { :expires_in => 20.seconds } )
  316. # overview
  317. cache_key = 'user_' + user.id.to_s + '_overview'
  318. overview_time = CacheIn.get_time( cache_key )
  319. if overview_time && @data[:overview_time] != overview_time
  320. @data[:overview_time] = overview_time
  321. overview = CacheIn.get( cache_key )
  322. self.log "push overview for user #{user.id}"
  323. # send update to browser
  324. self.transaction({
  325. :event => 'navupdate_ticket_overview',
  326. :data => overview,
  327. })
  328. end
  329. # overview_data
  330. overviews = Ticket.overview_list(
  331. :current_user_id => user.id,
  332. )
  333. overviews.each { |overview|
  334. cache_key = 'user_' + user.id.to_s + '_overview_data_' + overview.meta[:url]
  335. overview_data_time = CacheIn.get_time( cache_key )
  336. if overview_data_time && @data[cache_key] != overview_data_time
  337. @data[cache_key] = overview_data_time
  338. overview_data = CacheIn.get( cache_key )
  339. self.log "push overview_data for user #{user.id}"
  340. users = {}
  341. tickets = []
  342. overview_data[:tickets].each {|ticket_id|
  343. self.ticket( ticket_id, tickets, users )
  344. }
  345. # send update to browser
  346. self.transaction({
  347. :data => {
  348. :overview => overview_data[:overview],
  349. :ticket_list => overview_data[:tickets],
  350. :tickets_count => overview_data[:tickets_count],
  351. :collections => {
  352. :User => users,
  353. :Ticket => tickets,
  354. }
  355. },
  356. :event => [ 'loadCollection', 'ticket_overview_rebuild' ],
  357. :collection => 'ticket_overview_' + overview.meta[:url].to_s,
  358. })
  359. end
  360. }
  361. # ticket_create_attributes
  362. cache_key = 'user_' + user.id.to_s + '_ticket_create_attributes'
  363. ticket_create_attributes_time = CacheIn.get_time( cache_key )
  364. if ticket_create_attributes_time && @data[:ticket_create_attributes_time] != ticket_create_attributes_time
  365. @data[:ticket_create_attributes_time] = ticket_create_attributes_time
  366. ticket_create_attributes = CacheIn.get( cache_key )
  367. self.log "push ticket_create_attributes for user #{user.id}"
  368. # send update to browser
  369. self.transaction({
  370. :collection => 'ticket_create_attributes',
  371. :data => ticket_create_attributes,
  372. })
  373. end
  374. # recent viewed
  375. cache_key = 'user_' + user.id.to_s + '_recent_viewed'
  376. recent_viewed_time = CacheIn.get_time( cache_key )
  377. if recent_viewed_time && @data[:recent_viewed_time] != recent_viewed_time
  378. @data[:recent_viewed_time] = recent_viewed_time
  379. recent_viewed = CacheIn.get( cache_key )
  380. self.log "push recent_viewed for user #{user.id}"
  381. # send update to browser
  382. r = CacheIn.get( cache_key + '_push' )
  383. self.transaction({
  384. :event => 'update_recent_viewed',
  385. :data => r,
  386. })
  387. end
  388. # activity stream
  389. cache_key = 'user_' + user.id.to_s + '_activity_stream'
  390. activity_stream_time = CacheIn.get_time( cache_key )
  391. if activity_stream_time && @data[:activity_stream_time] != activity_stream_time
  392. @data[:activity_stream_time] = activity_stream_time
  393. activity_stream = CacheIn.get( cache_key )
  394. self.log "push activity_stream for user #{user.id}"
  395. # send update to browser
  396. r = CacheIn.get( cache_key + '_push' )
  397. self.transaction({
  398. :event => 'activity_stream_rebuild',
  399. :collection => 'activity_stream',
  400. :data => r,
  401. })
  402. end
  403. # rss
  404. cache_key = 'user_' + user.id.to_s + '_rss'
  405. rss_items_time = CacheIn.get_time( cache_key )
  406. if rss_items_time && @data[:rss_time] != rss_items_time
  407. @data[:rss_time] = rss_items_time
  408. rss_items = CacheIn.get( cache_key )
  409. self.log "push rss for user #{user.id}"
  410. # send update to browser
  411. r = CacheIn.get( cache_key + '_push' )
  412. self.transaction({
  413. :event => 'rss_rebuild',
  414. :collection => 'dashboard_rss',
  415. :data => r,
  416. })
  417. end
  418. self.log "---/client-"
  419. # start faster in the beginnig
  420. if loop_count < 20
  421. sleep 0.4
  422. else
  423. sleep 1
  424. end
  425. end
  426. end
  427. # add ticket if needed
  428. def ticket( ticket_id, tickets, users )
  429. if !@pushed[:tickets]
  430. @pushed[:tickets] = {}
  431. end
  432. ticket = Ticket.full_data(ticket_id)
  433. if @pushed[:tickets][ticket_id] != ticket
  434. @pushed[:tickets][ticket_id] = ticket
  435. tickets.push ticket
  436. end
  437. # add users if needed
  438. self.user( ticket['owner_id'], users )
  439. self.user( ticket['customer_id'], users )
  440. self.user( ticket['created_by_id'], users )
  441. end
  442. # add user if needed
  443. def user( user_id, users )
  444. if !@pushed[:users]
  445. @pushed[:users] = {}
  446. end
  447. # get user
  448. user = User.user_data_full( user_id )
  449. # user is already on client and not changed
  450. return if @pushed[:users][ user_id ] == user
  451. @pushed[:users][user_id] = user
  452. # user not on client or different
  453. self.log 'push user ... ' + user['login']
  454. users[ user_id ] = user
  455. end
  456. # send update to browser
  457. def transaction( data )
  458. Session.transaction( @client_id, data )
  459. end
  460. def log( data )
  461. puts "#{Time.now}:client(#{ @client_id }) #{ data }"
  462. end
  463. end