summaryrefslogtreecommitdiff
blob: f67d2fb66a43f33ca10ab8a3d64538279c9c25cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Copyright (C) 2010 Robin H.Johnson, Ovechko Kostyantyn <fastinetserver@gmail.com>.
*
* Project: IDFetch.
* Developer: Ovechko Kostyantyn Olexandrovich (Kharkiv State Technical University of Construction and Architecture, Ukraine).
* Mentor: Robin H. Johnson (Gentoo Linux: Developer, Trustee & Infrastructure Lead).
* Mentoring organization: Gentoo Linux.
* Sponsored by GSOC 2010.
*
* This file is part of Segget.
*
* Segget is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* Segget is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Segget; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "network.h"

Tnetwork network_array[MAX_NETWORKS];

void Tnetwork::load_mirror_list(){
	try{
		ifstream file;
		string mirror_list_file_name="network"+toString(network_num)+"_mirrors.conf";
		file.exceptions (ifstream::failbit | ifstream::badbit);
		try{
			file.open(mirror_list_file_name.c_str());
		}
		catch(...){
			error_log("Can NOT open mirror list file: "+mirror_list_file_name+". Network will be disabled");
			priority=0;
			return;
		}
		try{
			//processing file
			string mirror_line;
			while (not(file.eof())) {
				getline(file,mirror_line);
				if (! mirror_line.length()) continue;
				if (mirror_line[0] == '#') continue;
				if (mirror_line[0] == ';') continue;
				Tmirror cur_mirror;
				cur_mirror.url=mirror_line;
				benchmarked_mirror_list.push_back(cur_mirror);
				debug("LOCAL_MIRROR_ADDED:"+mirror_line);
			}
		}
		catch(...){
			error_log("Mirror list file: "+mirror_list_file_name+" was opened, but an error occured while reading from it.");
		}
	}catch(...){
		error_log_no_msg("Error in network.cpp: load_mirror_list()");
	}
}

void Tnetwork::init(uint priority_value){
	try{
		priority=priority_value;
		Tconfig conf("network"+toString(network_num)+".conf");
		conf.set("mode","network_mode",network_mode,0,2);
		conf.set("network_bind","bind_interface",bind_interface);
		conf.set("network_connections","max_connections",max_connections,1,MAX_CONNECTS);
		conf.set("network_connections","connection_timeout",connection_timeout,1,1000);
		conf.set("network_connections","ftp_response_timeout",ftp_response_timeout,1,-1);
		conf.set("network_connections","timeout",time_out,100,-1);
		conf.set("network_connections","low_connection_speed_limit",low_connection_speed_limit,1,-1);
		conf.set("network_connections","low_connection_speed_time",low_connection_speed_time,1,600);
		conf.set("network_connections","max_connection_speed",max_connection_speed,1,-1);

		conf.set("network_user_data","user_agent",user_agent);

		conf.set("network_proxy","proxy_ip_or_name",proxy_ip_or_name);
		conf.set("network_proxy","proxy_port",proxy_port,1,65535);
		conf.set("network_proxy","proxy_off",proxy_off);
		conf.set("network_proxy","proxy_user",proxy_user);
		conf.set("network_proxy","proxy_password",proxy_password);

//		conf.set(use_own_mirror_list_only_on,		"network_mirrors",			"use_own_mirror_list_only_on");

		switch (network_mode){
			case MODE_LOCAL:
				{
					conf.set("network_mirrors","only_local_when_possible",only_local_when_possible);
					load_mirror_list();
					log("Settings: Network"+toString(network_num)+" local mirror_list size:"+toString(mirror_list.size()));
					break;
				};
			case MODE_PROXY_FETCHER:
				{
					conf.set("network_proxy_fetcher","proxy_fetcher_ip",proxy_fetcher_ip);
					if (proxy_fetcher_ip=="none"){
						error_log("Network"+toString(network_num)+" in PROXY_FETCHER mode, but proxy_fetcher_ip="+proxy_fetcher_ip+". Network will be disabled.");
						priority=0;
					}else{
						conf.set("network_proxy_fetcher","proxy_fetcher_port",proxy_fetcher_port,1,65535);
						if (proxy_fetcher_ip==settings.provide_proxy_fetcher_ip && proxy_fetcher_port==settings.provide_proxy_fetcher_port){
							error_log("Error: Network"+toString(network_num)+" settings have the same ip and port for proxy-fetcher as the ones in [provide_proxy_fetcher_to_others] section of segget.conf file.");
							error_log("       Segget instance can NOT serve as a proxy-fetcher for itself - network will be disabled");
							priority=0;
						}else{
							conf.set("network_mirrors","only_local_when_possible",only_local_when_possible);
							load_mirror_list();
							log("Settings: Network"+toString(network_num)+" local fetcher_local_mirrors_list size:"+toString(mirror_list.size()));
						}
					}
					break;
				}
			default: break; // network in MODE_REMOTE
		}
		conf.clear();
	}catch(...){
		error_log("Error in network.cpp: init()");
	}
}

bool Tnetwork::has_free_connections(){
	try{
		if (active_connections_num<max_connections){
			return true;
		}else{
			return false;
		}
	}catch(...){
		error_log_no_msg("Error in network.cpp: has_free_connections()");
		return false;
	}
}

bool Tnetwork::connect(){
	try{
		if (active_connections_num<max_connections){
			active_connections_num++;
			return true;
		}else{
			return false;
		}
	}catch(...){
		error_log_no_msg("Error in network.cpp: connect()");
		return false;
	}
}