2
2
using Granite.Services;
3
public class Arrive.Model.Aria2 : Object {
4
private static int REFRESH_TIME=1000;
6
public int num_waiting;
7
public int num_stopped;
8
public int download_speed;
9
public int upload_speed;
10
private string aria_ip="http://localhost";
11
private string aria_port="6800";
12
public string aria_uri;
13
public bool is_listened;
14
public Arrive.Model.DownloadList download_list;
15
public Arrive.Model.FinishedList finished_list;
23
//if(ip==null)aria_ip="http://localhost" else aria_ip = ip;
24
//if(port==null)aria_port="6800" else aria_port = port;
25
aria_uri = aria_ip+":"+aria_port+"/rpc";
27
download_list=new DownloadList();
28
finished_list = new Arrive.Model.FinishedList();
3
namespace Arrive.Model {
4
public class Aria2 : Object {
5
private static int REFRESH_TIME=1000;
7
public int num_waiting;
8
public int num_stopped;
9
public int download_speed;
10
public int upload_speed;
11
private string aria_ip="http://localhost";
12
private string aria_port="6800";
13
public string aria_uri;
14
public bool is_listened;
15
public DownloadList download_list;
16
public FinishedList finished_list;
31
var refresh_timer = new TimeoutSource(REFRESH_TIME);
32
refresh_timer.set_callback(()=>{
36
refresh_timer.attach(null);
37
message ("version = %s",get_version());
42
private void start_aria2c() {
43
//~ var sc = new Granite.Services.SimpleCommand(Environment.get_home_dir(),"aria2c --enable-rpc");
45
//~ message("aria output %s",sc.output_str);
47
//max connection and split size are hardcoded for now
48
//TODO:create preferences dialog to set max-connection-per-server and min-split-size (and almost everything)
49
GLib.Process.spawn_command_line_async ("aria2c --enable-rpc --max-connection-per-server 5 --min-split-size 1M --pause=true");
51
}catch(GLib.SpawnError error)
53
critical("cant start aria2c");
55
//~ delay(1000);//FIXME:need to wait for aria to load
56
Thread.usleep(500000);
57
if(get_version()==""){
58
critical("cant start or bind port, try to restart");
63
private void refresh_status() {
67
//TODO:Parse getGlobalOption response
68
private void get_global_option() {
69
Soup.Message msg = XMLRPC.request_new(aria_uri,"aria2.getGlobalOption");
70
string data = send_message (msg);
72
private void get_global_stat() {
73
Soup.Message msg = XMLRPC.request_new(aria_uri,"aria2.getGlobalStat");
74
string data = send_message (msg);
77
if(Soup.XMLRPC.parse_method_response(data, -1, out v)) {
78
HashTable<string,Value?> ht;
81
ht = (HashTable<string,Value?>) v;
83
val=ht.get("numStopped");
84
num_stopped=int.parse(val.get_string());
86
val = ht.get("numWaiting");
87
num_waiting=int.parse(val.get_string());
89
val = ht.get("numActive");
90
num_active=int.parse(val.get_string());
92
val = ht.get("downloadSpeed");
93
download_speed=int.parse(val.get_string());
95
val = ht.get("uploadSpeed");
96
upload_speed=int.parse(val.get_string());
99
debug("Error while processing tellStatus response");
102
private string get_version() {
104
Soup.Message msg = XMLRPC.request_new(aria_uri,"aria2.getVersion");
105
string data = send_message (msg);
108
if(Soup.XMLRPC.parse_method_response(data,-1,out v)){
109
HashTable<string,Value?> ht;
112
ht = (HashTable<string,Value?>) v;
114
val=ht.get("version");
115
version=val.get_string();
118
debug("Error while processing getVersion response");
122
public void shutdown() {
123
Soup.Message msg = XMLRPC.request_new(aria_uri,"aria2.shutdown");
126
private void force_shutdown() {
127
Soup.Message msg = XMLRPC.request_new(aria_uri,"aria2.forceShutdown");
130
//TODO:using system.multicall to call more than one method at once
131
private void system_multicall() {
133
private string send_message(Soup.Message message) {
134
var session = new SessionSync();
135
session.send_message(message);
137
string data = (string) message.response_body.flatten().data;
24
//if(ip==null)aria_ip="http://localhost" else aria_ip = ip;
25
//if(port==null)aria_port="6800" else aria_port = port;
26
aria_uri = aria_ip+":"+aria_port+"/rpc";
28
download_list = new DownloadList ();
29
finished_list = new FinishedList ();
32
var refresh_timer = new TimeoutSource (REFRESH_TIME);
33
refresh_timer.set_callback (()=>{
37
refresh_timer.attach (null);
38
message ("version = %s",get_version());
43
private void start_aria2c (){
44
//~ var sc = new Granite.Services.SimpleCommand(Environment.get_home_dir(),"aria2c --enable-rpc");
46
//~ message("aria output %s",sc.output_str);
48
//max connection and split size are hardcoded for now
49
//TODO:create preferences dialog to set max-connection-per-server and min-split-size (and almost everything)
50
GLib.Process.spawn_command_line_async ("aria2c --enable-rpc --max-connection-per-server 5 --min-split-size 1M --pause=true");
52
} catch (GLib.SpawnError error)
54
critical ("cant start aria2c");
56
//~ delay(1000);//FIXME:need to wait for aria to load
57
Thread.usleep (500000);
58
if (get_version() == ""){
59
critical ("cant start or bind port, try to restart");
64
private void refresh_status () {
68
//TODO:Parse getGlobalOption response
69
private void get_global_option () {
70
Soup.Message msg = XMLRPC.request_new (aria_uri, "aria2.getGlobalOption");
71
string data = send_message (msg);
73
private void get_global_stat () {
74
Soup.Message msg = XMLRPC.request_new (aria_uri, "aria2.getGlobalStat");
75
string data = send_message (msg);
78
if (Soup.XMLRPC.parse_method_response (data, -1, out v)) {
79
HashTable<string,Value?> ht;
82
ht = (HashTable<string,Value?>) v;
84
val = ht.get ("numStopped");
85
num_stopped = int.parse (val.get_string ());
87
val = ht.get ("numWaiting");
88
num_waiting = int.parse (val.get_string ());
90
val = ht.get ("numActive");
91
num_active = int.parse (val.get_string ());
93
val = ht.get ("downloadSpeed");
94
download_speed = int.parse (val.get_string ());
96
val = ht.get ("uploadSpeed");
97
upload_speed = int.parse (val.get_string ());
100
debug ("Error while processing tellStatus response");
103
private string get_version () {
105
Soup.Message msg = XMLRPC.request_new (aria_uri,"aria2.getVersion");
106
string data = send_message (msg);
109
if (Soup.XMLRPC.parse_method_response (data,-1,out v)){
110
HashTable<string,Value?> ht;
113
ht = (HashTable<string,Value?>) v;
115
val = ht.get("version");
116
version = val.get_string();
119
debug ("Error while processing getVersion response");
123
public void shutdown () {
124
Soup.Message msg = XMLRPC.request_new (aria_uri,"aria2.shutdown");
127
private void force_shutdown() {
128
Soup.Message msg = XMLRPC.request_new (aria_uri,"aria2.forceShutdown");
131
//TODO:using system.multicall to call more than one method at once
132
private void system_multicall () {
134
private string send_message (Soup.Message message) {
135
var session = new SessionSync ();
136
session.send_message (message);
138
string data = (string) message.response_body.flatten ().data;