remove KAGE server address.
[chise/ruby.git] / src / kageserver.rb
1 #!/usr/bin/env ruby
2
3 $LOAD_PATH << '../ruby/src'
4 require 'chise'
5 include CHISE
6 require 'singleton'
7 require 'net/http'
8 require 'uri'
9 require 'network' #漢字間接続のネットワークを計算する
10
11 class KageServer #======================================================================
12   include Singleton
13   TYPES = "skeleton mincho gothic".split
14   SKELETON = 0
15   MINCHO = 1
16   GOTHIC = 2
17   HOME_DIR = "d:/work/chise/"
18   CACHE_DIR = HOME_DIR + "cache_kage"
19   URL  = "http://192.168.2.60:5100/"
20   def initialize(url=URL)
21     @url = url
22     @glyphs = []
23     @use_cache = true #デフォルト: cacheに存在する場合はcacheから引き出す。
24     @offline = false #テスト用
25     @offline = true #テスト用
26     Dir.mkdir(CACHE_DIR) unless FileTest.directory?(CACHE_DIR)
27   end
28   attr_accessor :url, :use_cache
29   def filename(num, type=SKELETON) sprintf("u%04x.%s", num, TYPES[type]) end
30   def cache_file(num, type=SKELETON) CACHE_DIR+"/"+filename(num, type)+".svg" end
31
32   def list_cache
33     ar = []
34     Dir.chdir(CACHE_DIR)
35     Dir.glob("*.svg").each {|file|
36       if file =~ /^u([0-9a-fA-F]+).skeleton.svg$/
37         code = $1.hex
38         ar << code
39       end
40     }
41     ar
42   end
43
44   def get(num, type=SKELETON)
45     return open(cache_file(num, type)).read if FileTest.exist?(cache_file(num, type))
46     svg = get_http(num, type)
47     return svg if svg
48     return nil
49   end
50   def get_http(num, type=SKELETON)
51     return nil if @offline
52     uri = URI.parse(URL + filename(num, type))
53     p ['uri', uri.to_s]
54     Net::HTTP.version_1_1   # declear to use 1.1 features.
55     Net::HTTP.start( uri.host, uri.port ) {|http|
56       response, body = http.get('/'+filename(num, type)+".svg")
57       if body
58         if error?(body)
59           p ['error', uri.to_s]
60           return nil
61         else
62           store_cache(num, type, body)
63           return body
64         end
65       end
66     }
67     return nil
68   end
69   def store_cache(num, type, svg)
70     open(cache_file(num, type), "w") {|f|
71       f.print svg
72     }
73   end
74   def error?(svg)
75     (svg =~ /<!-- error -->/)
76   end
77   def read_list
78     h = {}
79     open("kage-list.txt"){|f|
80       while line = f.gets
81         if line =~ /u([0-9a-f]+)\.skeleton/
82           code = $1
83           num = code.hex
84           error = false
85           error = true if line =~ /error/
86           h[num] = error
87         end
88       end
89     }
90     return h
91   end
92   def get_all
93     #error_h = read_list
94     STDOUT.binmode
95     @kn = KanjiNetwork.new
96     @kl = KanjiList.instance
97     #list = @kl.awase(0)
98     #list = @kl.awase(1)
99     list = @kl.joyo
100     @kn.make_network(list)
101     nodes, edges = @kn.nodes_and_edges
102     ar = []
103     nodes.each {|ch|
104       char = ch.char
105       num = char.to_i
106       #next if 0xffff < num
107       ar << num
108     }
109     get_ar(ar)
110   end
111   def get_ar(ar)
112     ar.each {|num| #intの数列
113       char = Character.get(num)
114       ch = char.to_s
115       er = char.to_er
116       #TYPES.each_with_index {|type, index|
117       #(0..2).each {|index|
118       (0..0).each {|index|
119         result = get(num, index) #cacheに保存するべしと。
120         next if result
121         err = "error"
122         print "#{er}    #{ch}   #{err}\n"
123       }
124     }
125   end
126   def test_kanji
127     char = "&CDP-8BA5;".de_er
128     #p char.inspect_all
129     #str = (char.to_s+"真")
130     str = (char.to_s+"直")
131     p str.find
132   end
133 end
134
135 if $0 == __FILE__ #======================================================================
136   ks = KageServer.instance
137   #print ks.get(0x4e03)
138   ks.get_all
139 end
140
141 #----------------------------------------------------------------------end.