OSDN Git Service

Stub files connecting to a shogigraphic server.
[shogi-server/shogi-server.git] / showgame / controller / main.rb
index 0a86bdb..b71bcfb 100644 (file)
@@ -5,12 +5,17 @@
 #  map '/otherurl'
 # this will force the controller to be mounted on: /otherurl
 
-require File.join(__DIR__, "..", "..", "shogi_server", "piece")
-require File.join(__DIR__, "..", "..", "shogi_server", "piece_ky")
-require File.join(__DIR__, "..", "..", "shogi_server", "board")
-require File.join(__DIR__, "..", "..", "shogi_server", "usi")
+$:.unshift File.join(__DIR__, "..", "..")
+$:.unshift File.join(__DIR__, "..", "gen-rb")
+
+require "shogi_server/piece"
+require "shogi_server/board"
+require "shogi_server/usi"
+
+require "ShogiGraphic"
+require 'thrift/transport/socket'
+require 'thrift/protocol/tbinaryprotocol'
 
-$pos2img = "/home/daigo/cprojects/gpsshogi/gps/sample/graphic/pos2img"
 $pos2img_out_dir = File.join(".", "public", "images")
 
 class MainController < Ramaze::Controller
@@ -47,11 +52,20 @@ class MainController < Ramaze::Controller
   end
 
   def sfen(str)
-    sfen = "sfen %s" % [ShogiServer::Usi.unescape(str)]
-    Ramaze::Log.warn(sfen)
-    result = system($pos2img, "--dir", $pos2img_out_dir, sfen)
-    Ramaze::Log.warn("result fail") unless result
-    @img = "/images/%s.png" % [str]
+    transport = Thrift::BufferedTransport.new(Thrift::Socket.new('localhost', 9090))
+    client    = ShogiGraphic::Client.new(Thrift::BinaryProtocol.new(transport))
+
+    transport.open
+    result = client.usi2png(str)
+    transport.close
+
+    Ramaze::Log.error("Failed to get an image of %s from the Thrift server" % [str]) if !result || result.empty?
+    @img = "/images/%s" % [result]
+  end
+
+  def images(str)
+    file = File.join($pos2img_out_dir, str)
+    send_file(file, mime_type = Ramaze::Tool::MIME.type_for(file))
   end
 
   # the string returned at the end of the function is used as the html body