haruharu 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. data/README +1 -0
  2. data/RakeFile +13 -0
  3. data/VERSION +1 -0
  4. data/haruharu.gemspec +39 -0
  5. data/lib/haruharu.rb +105 -0
  6. data/test_eew.rb +33 -0
  7. metadata +69 -0
data/README ADDED
@@ -0,0 +1 @@
1
+ 謎の受信機
data/RakeFile ADDED
@@ -0,0 +1,13 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |gemspec|
4
+ gemspec.name = "haruharu"
5
+ gemspec.summary = "about streaming receiver"
6
+ gemspec.email = "sorah@tubusu.net"
7
+ gemspec.homepage = "http://github.com/sorah/haruharu"
8
+ gemspec.description = "about streaming receiver. streaming server is not included."
9
+ gemspec.authors = ["Shota Fukumori (sora_h)"]
10
+ end
11
+ rescue LoadError
12
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
13
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
data/haruharu.gemspec ADDED
@@ -0,0 +1,39 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{haruharu}
8
+ s.version = "1.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Shota Fukumori (sora_h)"]
12
+ s.date = %q{2011-04-26}
13
+ s.description = %q{about streaming receiver. streaming server is not included.}
14
+ s.email = %q{sorah@tubusu.net}
15
+ s.extra_rdoc_files = [
16
+ "README"
17
+ ]
18
+ s.files = [
19
+ "README",
20
+ "RakeFile",
21
+ "VERSION",
22
+ "lib/haruharu.rb",
23
+ "test_eew.rb"
24
+ ]
25
+ s.homepage = %q{http://github.com/sorah/haruharu}
26
+ s.require_paths = ["lib"]
27
+ s.rubygems_version = %q{1.6.2}
28
+ s.summary = %q{about streaming receiver}
29
+
30
+ if s.respond_to? :specification_version then
31
+ s.specification_version = 3
32
+
33
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
34
+ else
35
+ end
36
+ else
37
+ end
38
+ end
39
+
data/lib/haruharu.rb ADDED
@@ -0,0 +1,105 @@
1
+ require 'rubygems'
2
+ require 'json'
3
+ require 'socket'
4
+
5
+ class HaruHaru
6
+ attr_reader :state
7
+
8
+ def initialize(addr,port,token)
9
+ @receive_hooks = []
10
+ @state_hooks = []
11
+ @socket = nil
12
+ @thread = nil
13
+ @state = :initialized
14
+ @buffer = nil
15
+ @addr, @port, @token = addr,port,token
16
+ end
17
+
18
+ def receive(&block)
19
+ @receive_hooks << block
20
+ self
21
+ end
22
+
23
+ def state(&block)
24
+ @state_hooks << block
25
+ self
26
+ end
27
+
28
+ def connect
29
+ call_state(:will_connect)
30
+ @socket = TCPSocket.open(@addr,@port)
31
+ call_state(:will_auth)
32
+ @socket.puts "auth #{@token}"
33
+ call_state(:waiting_auth)
34
+ if IO.select([@socket],[],[],10)
35
+ case @socket.gets
36
+ when /^auth good$/
37
+ call_state(:authed)
38
+ start_thread
39
+ call_state(:connected)
40
+ when /^auth bad$/
41
+ raise AuthenticateFailed
42
+ disconnect
43
+ end
44
+ else
45
+ raise AuthenticateTimeout
46
+ end
47
+ end
48
+
49
+ def disconnect
50
+ @thread.kill
51
+ @socket.close
52
+ @thread = @socket = nil
53
+ call_state(:disconnected)
54
+ end
55
+
56
+ def reconnect
57
+ disconnect
58
+ connect
59
+ end
60
+
61
+ class AuthenticateTimeout < Exception; end
62
+ class AuthenticateFailed < Exception; end
63
+
64
+ private
65
+
66
+ def call_receive(*args)
67
+ @receive_hooks.each{|x| x[*args] }
68
+ end
69
+
70
+ def call_state(state)
71
+ @state = state
72
+ @state_hooks.each{|x| x[state] }
73
+ end
74
+
75
+ def start_thread; @thread = Thread.new do
76
+ loop do
77
+ if IO.select([@socket],[],[],130)
78
+ _ = @socket.gets
79
+ if _
80
+ _.chomp!
81
+ else
82
+ call_state(:connection_lost)
83
+ Thread.new{reconnect}
84
+ break
85
+ end
86
+ case _
87
+ when /^ping$/
88
+ when /^begin$/
89
+ @buffer = []
90
+ when /^end$/
91
+ call_receive(JSON.parse(@buffer.join))
92
+ @buffer = nil
93
+ else
94
+ next unless @buffer
95
+ @buffer << _
96
+ end
97
+ else
98
+ call_state(:timeout)
99
+ Thread.new{reconnect}
100
+ break
101
+ end
102
+ end
103
+ end; @thread.abort_on_exception = true; end
104
+
105
+ end
data/test_eew.rb ADDED
@@ -0,0 +1,33 @@
1
+ #-*- coding: utf-8 -*-
2
+ require 'socket'
3
+
4
+ kunren = []
5
+ kunren.push "auth good"
6
+
7
+ kunren.push <<-EOM
8
+ begin
9
+ {"eew":{"total_messages":1,"is_continue":true,"is_final":false,"type":"通常発表","drill_type":"通常","fixing_reason":"M及び震源位置が変化したため","reported_by":"破滅","reported_at":"2011-04-15 23:34:50 +0900","quake_at":"2011-04-15 23:34:19 +0900","id":"20110415233435","revision":4,"epicenter":{"name":"荒川智則","position":"N0.0 E0.0","probability":"防災科研システム(5点以上)[防災科学技術研究所データ]","probability_jma":"テリトリー法(2点)[気象庁データ]"},"depth":10,"magnitude":5.1,"maximum_intensity":"3(訓練)","depth_probability":"防災科研システム(5点以上)[防災科学技術研究所データ]","magnitude_probability":"全点全相(最大5点)[気象庁データ]","land_or_sea":"海域","is_warning":true},"ebi":null}
10
+ end
11
+ EOM
12
+
13
+ kunren.push <<-EOM
14
+ begin
15
+ {"eew":{"total_messages":1,"is_continue":true,"is_final":false,"type":"通常発表","drill_type":"通常","fixing_reason":"M及び震源位置が変化したため","reported_by":"破滅","reported_at":"2011-04-15 23:34:53 +0900","quake_at":"2011-04-15 23:34:16 +0900","id":"20110415233435","revision":5,"epicenter":{"name":"荒川智則浜","position":"N0.0 E0.0","probability":"防災科研システム(5点以上)[防災科学技術研究所データ]","probability_jma":"テリトリー法(2点)[気象庁データ]"},"depth":10,"magnitude":6.6,"maximum_intensity":"6強(訓練)","depth_probability":"防災科研システム(5点以上)[防災科学技術研究所データ]","magnitude_probability":"全点P相(最大5)[気象庁データ]","land_or_sea":"陸域","is_warning":true},"ebi":[{"area":"荒川浜通り","estimated_intensity":"6弱から6強(訓練)","estimated_arriving_at":null,"has_warning":true,"already_arrived":true},{"area":"某ヒガシニホンバシ","estimated_intensity":"5弱から5強(訓練)","estimated_arriving_at":null,"has_warning":true,"already_arrived":true}]}
16
+ end
17
+ EOM
18
+
19
+ kunren.push "ping"
20
+
21
+ server = TCPServer.open("0.0.0.0",6743)
22
+ i = 0
23
+ socket = server.accept
24
+ Thread.new do
25
+ while buf = socket.gets
26
+ p buf
27
+ end
28
+ end
29
+ while STDIN.gets
30
+ puts "-> #{kunren[i].inspect}"
31
+ socket.puts kunren[i]
32
+ i += 1
33
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: haruharu
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Shota Fukumori (sora_h)
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-04-26 00:00:00 +09:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: about streaming receiver. streaming server is not included.
22
+ email: sorah@tubusu.net
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README
29
+ files:
30
+ - README
31
+ - RakeFile
32
+ - VERSION
33
+ - haruharu.gemspec
34
+ - lib/haruharu.rb
35
+ - test_eew.rb
36
+ has_rdoc: true
37
+ homepage: http://github.com/sorah/haruharu
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options: []
42
+
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ segments:
51
+ - 0
52
+ version: "0"
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.3.7
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: about streaming receiver
68
+ test_files: []
69
+