4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "condition.h"
24 #include "loadbalance.h"
29 LoadPackage::LoadPackage()
31 completion_lock = new Condition(0, "LoadPackage::completion_lock");
33 LoadPackage::~LoadPackage()
35 delete completion_lock;
46 LoadClient::LoadClient(LoadServer *server)
49 this->server = server;
52 input_lock = new Condition(0, "LoadClient::input_lock");
53 completion_lock = new Condition(0, "LoadClient::completion_lock");
56 LoadClient::LoadClient()
62 input_lock = new Condition(0, "LoadClient::input_lock");
63 completion_lock = new Condition(0, "LoadClient::completion_lock");
66 LoadClient::~LoadClient()
72 delete completion_lock;
75 int LoadClient::get_package_number()
77 return package_number;
80 LoadServer* LoadClient::get_server()
86 void LoadClient::run()
90 input_lock->lock("LoadClient::run");
98 server->client_lock->lock("LoadClient::run");
99 if(server->current_package < server->total_packages)
101 package_number = server->current_package;
102 package = server->packages[server->current_package++];
103 server->client_lock->unlock();
104 input_lock->unlock();
106 process_package(package);
108 package->completion_lock->unlock();
112 server->client_lock->unlock();
113 completion_lock->unlock();
119 void LoadClient::run_single()
121 if(server->total_packages)
123 for(int i = 0; i < server->total_packages; i++)
125 process_package(server->packages[i]);
130 void LoadClient::process_package(LoadPackage *package)
132 printf("LoadClient::process_package\n");
139 LoadServer::LoadServer(int total_clients, int total_packages)
141 if(total_clients <= 0)
142 printf("LoadServer::LoadServer total_clients == %d\n", total_clients);
143 this->total_clients = total_clients;
144 this->total_packages = total_packages;
148 client_lock = new Mutex("LoadServer::client_lock");
153 LoadServer::~LoadServer()
160 void LoadServer::delete_clients()
164 for(int i = 0; i < total_clients; i++)
169 if(single_client) delete single_client;
175 void LoadServer::delete_packages()
179 for(int i = 0; i < total_packages; i++)
186 void LoadServer::set_package_count(int total_packages)
189 this->total_packages = total_packages;
194 void LoadServer::create_clients()
196 if(!is_single && !clients)
198 clients = new LoadClient*[total_clients];
199 for(int i = 0; i < total_clients; i++)
201 clients[i] = new_client();
202 clients[i]->server = this;
207 if(is_single && !single_client)
209 single_client = new_client();
210 single_client->server = this;
214 void LoadServer::create_packages()
218 packages = new LoadPackage*[total_packages];
219 for(int i = 0; i < total_packages; i++)
220 packages[i] = new_package();
224 LoadPackage* LoadServer::get_package(int number)
226 return packages[number];
229 LoadClient* LoadServer::get_client(int number)
231 return is_single ? single_client : clients[number];
234 int LoadServer::get_total_packages()
236 // if(is_single) return 1;
237 return total_packages;
240 int LoadServer::get_total_clients()
242 if(is_single) return 1;
243 return total_clients;
246 void LoadServer::process_packages()
248 if(total_clients == 1)
265 for(int i = 0; i < total_clients; i++)
267 clients[i]->input_lock->unlock();
270 // Wait for packages to get finished
271 for(int i = 0; i < total_packages; i++)
273 packages[i]->completion_lock->lock("LoadServer::process_packages 1");
276 // Wait for clients to finish before allowing changes to packages
277 for(int i = 0; i < total_clients; i++)
279 clients[i]->completion_lock->lock("LoadServer::process_packages 2");
283 void LoadServer::process_single()
290 single_client->run_single();