Class: JIJI::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/jiji/command.rb

Overview

コマンドラインツール

Constant Summary collapse

JIJI_DIR_NAME =

設定ファイル置き場

"~/.jiji"

Instance Method Summary collapse

Instance Method Details

#run(args) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/jiji/command.rb', line 104

def run( args )
  case  args[0]
    when "start"; start
    when "stop";  stop
    when "setting"; setting
    when "restart"
      stop
      start
    else
      name = File.basename( File.expand_path( $0 ))
      puts "usage : #{name} ( setting | start | stop | restart )"
  end
end

#settingObject

初期化



46
47
48
49
50
51
52
53
54
55
56
57
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
100
101
# File 'lib/jiji/command.rb', line 46

def setting
  h = HighLine.new
  user = h.ask("> Please input a user name of CLICK Securities DEMO Trade.")
  pass = h.ask("> Please input a password of CLICK Securities DEMO Trade."){|q| q.echo = '*' }
  dir  = h.ask("> Please input a data directory of jiji. (default: #{JIJI_DIR_NAME} )")
  dir = !dir || dir.empty? ? JIJI_DIR_NAME : dir

  port = h.ask('> Please input a server port. (default: 7000 )')
  port = !port || port.empty? ? "7000" : port
  unless port =~ /\d+/
    puts "[ERROR] setting failed.( illegal port number. port=#{port} )"
    return
  end

  # ディレクトリ作成

  begin
    puts ""
    ex_dir = File.expand_path(JIJI_DIR_NAME)
    mkdir ex_dir
    open( "#{ex_dir}/base", "w" ) {|f|
      f << dir
    }
    puts "create. #{ex_dir}/base"

    # ベースディレクトリの作成

    dir = File.expand_path(dir)
    mkdir(dir) if ( dir != ex_dir )
    mkdir("#{dir}/conf")

    # 設定ファイル

    open( "#{dir}/conf/configuration.yaml", "w" ) {|f|
      f << "---\nserver:\n port: \#{port}\n\nsecurities:\n  account:\nuser:     \"\#{user}\"\npassword: \"\#{pass}\"\n"
    }
    FileUtils.chmod(0600, "#{dir}/conf/configuration.yaml")

    # サンプルエージェント

    ["agents","shared_lib"].each {|d|
      mkdir("#{dir}/#{d}")
      FileUtils.copy( Dir.glob("#{__FILE__}/../../../base/#{d}/*"), "#{dir}/#{d}" )
    }
  rescue Exception
    puts "[ERROR] setting failed.(#{$!.to_s})"
    return
  end

  puts "Setting was completed!"
end

#startObject

サービスを開始



17
18
19
20
21
22
23
24
25
26
# File 'lib/jiji/command.rb', line 17

def start
  begin
    puts "jiji started."
    s = JIJI::FxServer.new( data_dir )
    s.start
  rescue Exception
    puts "[ERROR] start failed.(#{$!.to_s})"
    return
  end
end

#stopObject

サービスを停止



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/jiji/command.rb', line 29

def stop
  begin
    host = ARGV[1] || "localhost"
    conf = JIJI::Registry.new(data_dir)[:conf]
    port = conf.get([:server,:port], 7000).to_i
    service = JSONBroker:: JsonRpcRequestor.new( "system", "http://#{host}:#{port}" )
    service.shutdown
    sleep 10 # 停止完了を待つ。

    puts "jiji stopped."
  rescue Exception
    puts "[ERROR] stop failed.(#{$!.to_s})"
    return
  end

end