~vikoadi/arrive/0.1

« back to all changes in this revision

Viewing changes to src/Model/Aria2.vala

  • Committer: Viko Adi Rahmawan
  • Date: 2013-05-10 12:18:17 UTC
  • Revision ID: vikoadi@gmail.com-20130510121817-xic6cs3t2gb3wi0m
code tidieing

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
using Soup;
2
2
using Granite.Services;
3
 
public class Arrive.Model.Aria2 : Object {
4
 
    private static int REFRESH_TIME=1000;
5
 
    public int num_active;
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;
16
 
    
17
 
    public Aria2() {
18
 
        num_active = 0;
19
 
        num_waiting = 0;
20
 
        num_stopped = 0;
21
 
        download_speed = 0;
22
 
        upload_speed = 0;
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";
26
 
        start_aria2c();
27
 
        download_list=new DownloadList();
28
 
        finished_list = new Arrive.Model.FinishedList();
29
 
        get_global_option();
 
3
namespace Arrive.Model {
 
4
    public class Aria2 : Object {
 
5
        private static int REFRESH_TIME=1000;
 
6
        public int num_active;
 
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;
30
17
        
31
 
        var refresh_timer = new TimeoutSource(REFRESH_TIME);
32
 
        refresh_timer.set_callback(()=>{
33
 
            refresh_status();
34
 
            return true;
35
 
        });
36
 
        refresh_timer.attach(null);
37
 
        message ("version = %s",get_version());
38
 
    }
39
 
    ~Aria2(){
40
 
        shutdown();
41
 
    }
42
 
    private void start_aria2c() {
43
 
//~         var sc = new Granite.Services.SimpleCommand(Environment.get_home_dir(),"aria2c --enable-rpc");
44
 
//~         sc.run();
45
 
//~         message("aria output %s",sc.output_str);
46
 
        try{
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");
50
 
            is_listened = true;
51
 
        }catch(GLib.SpawnError error)
52
 
        {
53
 
            critical("cant start aria2c");
54
 
        }
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");
59
 
                        is_listened = false;
60
 
                        shutdown();
61
 
                }
62
 
    }
63
 
    private void refresh_status() {
64
 
        get_global_stat();
65
 
        get_global_option();
66
 
    }
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);
71
 
    }
72
 
    private void get_global_stat() {
73
 
        Soup.Message msg = XMLRPC.request_new(aria_uri,"aria2.getGlobalStat");
74
 
        string data = send_message (msg);
75
 
        try {
76
 
            Value v;
77
 
            if(Soup.XMLRPC.parse_method_response(data, -1, out v)) {
78
 
                HashTable<string,Value?> ht;
79
 
                Value val;
80
 
                
81
 
                ht = (HashTable<string,Value?>) v;
82
 
                
83
 
                val=ht.get("numStopped");
84
 
                num_stopped=int.parse(val.get_string());
85
 
                
86
 
                val = ht.get("numWaiting");
87
 
                num_waiting=int.parse(val.get_string());
88
 
                
89
 
                val = ht.get("numActive");
90
 
                num_active=int.parse(val.get_string());
91
 
                
92
 
                val = ht.get("downloadSpeed");
93
 
                download_speed=int.parse(val.get_string());
94
 
                
95
 
                val = ht.get("uploadSpeed");
96
 
                upload_speed=int.parse(val.get_string());
97
 
            }
98
 
        } catch(Error e) {
99
 
            debug("Error while processing tellStatus response");
100
 
        }
101
 
    }
102
 
    private string get_version() {
103
 
        string version = "";
104
 
        Soup.Message msg = XMLRPC.request_new(aria_uri,"aria2.getVersion");
105
 
        string data = send_message (msg);
106
 
        try{
107
 
            Value v;
108
 
            if(Soup.XMLRPC.parse_method_response(data,-1,out v)){
109
 
                HashTable<string,Value?> ht;
110
 
                Value val;
111
 
                
112
 
                ht = (HashTable<string,Value?>) v;
113
 
                
114
 
                val=ht.get("version");
115
 
                version=val.get_string();
116
 
            }
117
 
        }catch(Error e){
118
 
            debug("Error while processing getVersion response");
119
 
        }
120
 
        return version;
121
 
    }
122
 
    public void shutdown() {
123
 
        Soup.Message msg = XMLRPC.request_new(aria_uri,"aria2.shutdown");
124
 
        send_message (msg);
125
 
    }
126
 
    private void force_shutdown() {
127
 
        Soup.Message msg = XMLRPC.request_new(aria_uri,"aria2.forceShutdown");
128
 
        send_message (msg);
129
 
    }
130
 
    //TODO:using system.multicall to call more than one method at once
131
 
    private void system_multicall() {
132
 
    }
133
 
    private string send_message(Soup.Message message) {
134
 
        var session = new SessionSync();
135
 
        session.send_message(message);
136
 
 
137
 
        string data = (string) message.response_body.flatten().data;
138
 
 
139
 
        return data;
 
18
        public Aria2 () {
 
19
            num_active = 0;
 
20
            num_waiting = 0;
 
21
            num_stopped = 0;
 
22
            download_speed = 0;
 
23
            upload_speed = 0;
 
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";
 
27
            start_aria2c ();
 
28
            download_list = new DownloadList ();
 
29
            finished_list = new FinishedList ();
 
30
            get_global_option ();
 
31
            
 
32
            var refresh_timer = new TimeoutSource (REFRESH_TIME);
 
33
            refresh_timer.set_callback (()=>{
 
34
                refresh_status ();
 
35
                return true;
 
36
            });
 
37
            refresh_timer.attach (null);
 
38
            message ("version = %s",get_version());
 
39
        }
 
40
        ~Aria2 (){
 
41
            shutdown ();
 
42
        }
 
43
        private void start_aria2c (){
 
44
    //~         var sc = new Granite.Services.SimpleCommand(Environment.get_home_dir(),"aria2c --enable-rpc");
 
45
    //~         sc.run();
 
46
    //~         message("aria output %s",sc.output_str);
 
47
            try {
 
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");
 
51
                is_listened = true;
 
52
            } catch (GLib.SpawnError error)
 
53
            {
 
54
                critical ("cant start aria2c");
 
55
            }
 
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");
 
60
                            is_listened = false;
 
61
                            shutdown ();
 
62
                    }
 
63
        }
 
64
        private void refresh_status () {
 
65
            get_global_stat ();
 
66
            get_global_option ();
 
67
        }
 
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);
 
72
        }
 
73
        private void get_global_stat () {
 
74
            Soup.Message msg = XMLRPC.request_new (aria_uri, "aria2.getGlobalStat");
 
75
            string data = send_message (msg);
 
76
            try {
 
77
                Value v;
 
78
                if (Soup.XMLRPC.parse_method_response (data, -1, out v)) {
 
79
                    HashTable<string,Value?> ht;
 
80
                    Value val;
 
81
                    
 
82
                    ht = (HashTable<string,Value?>) v;
 
83
                    
 
84
                    val = ht.get ("numStopped");
 
85
                    num_stopped = int.parse (val.get_string ());
 
86
                    
 
87
                    val = ht.get ("numWaiting");
 
88
                    num_waiting = int.parse (val.get_string ());
 
89
                    
 
90
                    val = ht.get ("numActive");
 
91
                    num_active = int.parse (val.get_string ());
 
92
                    
 
93
                    val = ht.get ("downloadSpeed");
 
94
                    download_speed = int.parse (val.get_string ());
 
95
                    
 
96
                    val = ht.get ("uploadSpeed");
 
97
                    upload_speed = int.parse (val.get_string ());
 
98
                }
 
99
            } catch (Error e) {
 
100
                debug ("Error while processing tellStatus response");
 
101
            }
 
102
        }
 
103
        private string get_version () {
 
104
            string version = "";
 
105
            Soup.Message msg = XMLRPC.request_new (aria_uri,"aria2.getVersion");
 
106
            string data = send_message (msg);
 
107
            try{
 
108
                Value v;
 
109
                if (Soup.XMLRPC.parse_method_response (data,-1,out v)){
 
110
                    HashTable<string,Value?> ht;
 
111
                    Value val;
 
112
                    
 
113
                    ht = (HashTable<string,Value?>) v;
 
114
                    
 
115
                    val = ht.get("version");
 
116
                    version = val.get_string();
 
117
                }
 
118
            }catch (Error e){
 
119
                debug ("Error while processing getVersion response");
 
120
            }
 
121
            return version;
 
122
        }
 
123
        public void shutdown () {
 
124
            Soup.Message msg = XMLRPC.request_new (aria_uri,"aria2.shutdown");
 
125
            send_message (msg);
 
126
        }
 
127
        private void force_shutdown() {
 
128
            Soup.Message msg = XMLRPC.request_new (aria_uri,"aria2.forceShutdown");
 
129
            send_message (msg);
 
130
        }
 
131
        //TODO:using system.multicall to call more than one method at once
 
132
        private void system_multicall () {
 
133
        }
 
134
        private string send_message (Soup.Message message) {
 
135
            var session = new SessionSync ();
 
136
            session.send_message (message);
 
137
 
 
138
            string data = (string) message.response_body.flatten ().data;
 
139
 
 
140
            return data;
 
141
        }
140
142
    }
141
143
}