Created
April 5, 2017 00:16
-
-
Save yusukemihara/01d7bbd4f86989200e463d78df2a66ae to your computer and use it in GitHub Desktop.
cpanda new
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # coding:utf-8 | |
| require 'json' | |
| require 'pp' | |
| require 'tempfile' | |
| def cpanda(command,opt,json=nil) | |
| out = nil | |
| arr = [ | |
| opt[:cpanda], | |
| "#{opt[:config_name]}", | |
| command, | |
| ] | |
| out = nil | |
| case command | |
| when "send_event" | |
| Tempfile.open('cpanda_json_send_event') do |file| | |
| if json | |
| file.write(json) | |
| file.close | |
| arr << file.path | |
| end | |
| exec = arr.join(' ') | |
| out = `#{exec}` | |
| end | |
| else | |
| arr << " --session_id #{opt[:session_id]}" | |
| arr << " --rpc_id #{opt[:rpc_id]}" | |
| arr << " --rpc_uri #{opt[:rpc_uri]}" | |
| arr << " --rest_uri #{opt[:rest_uri]}" | |
| exec = arr.join(' ') | |
| out = `#{exec}` | |
| end | |
| if out | |
| begin | |
| JSON.parse(out) | |
| rescue Exception | |
| {} | |
| end | |
| else | |
| {} | |
| end | |
| end | |
| def make_event_json(window,widget,event,screen_data) | |
| { | |
| "event_data" => { | |
| "window" => window, | |
| "widget" => widget, | |
| "event" => event, | |
| "screen_data" => screen_data, | |
| } | |
| }.to_json | |
| end | |
| def deep_set(target,path,data) | |
| return unless path | |
| return unless path.size > 0 | |
| case path.size | |
| when 1 | |
| target[path[0]] = data | |
| else | |
| unless target[path[0]] | |
| target[path[0]] = {} | |
| end | |
| deep_set(target[path[0]],path[1..-1],data) | |
| end | |
| end | |
| #opt = { | |
| # :cpanda => '/usr/lib/panda/bin/cpanda', | |
| # :user => 'ormaster', | |
| # :password => 'ormaster', | |
| # :auth_uri => 'http://localhost:8000/rpc/', | |
| # :ginbee => false | |
| #} | |
| opt = { | |
| :cpanda => '/tmp/opt/bin/cpanda', | |
| :config_name => 'default' | |
| } | |
| sessions = {} | |
| th = [] | |
| 10.times do | |
| th << Thread.new do | |
| # 接続 | |
| res = cpanda('start_session',opt) | |
| opt2 = { | |
| :cpanda => opt[:cpanda], | |
| :config_name => opt[:config_name], | |
| :session_id => res['session_id'], | |
| :rpc_id => 1, | |
| :rpc_uri => res['app_rpc_endpoint_uri'], | |
| :rest_uri => res['app_rpc_endpoint_uri'] | |
| } | |
| cpanda('get_window',opt2) | |
| sleep 1 | |
| 10.times do | |
| cpanda('list_downloads',opt2) | |
| cpanda('get_message',opt2) | |
| sleep 1 | |
| end | |
| sleep 1 | |
| # 切断 | |
| cpanda('end_session',opt2) | |
| end | |
| end | |
| th.each do |t| t.join end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment