00001 /* 00002 * Scale Stack 00003 * 00004 * Copyright 2010 Eric Day 00005 * 00006 * Licensed under the Apache License, Version 2.0 (the "License"); 00007 * you may not use this file except in compliance with the License. 00008 * You may obtain a copy of the License at 00009 * 00010 * http://www.apache.org/licenses/LICENSE-2.0 00011 * 00012 * Unless required by applicable law or agreed to in writing, software 00013 * distributed under the License is distributed on an "AS IS" BASIS, 00014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00015 * See the License for the specific language governing permissions and 00016 * limitations under the License. 00017 */ 00018 00024 #include "config.h" 00025 00026 #include <scalestack/kernel/core.h> 00027 #include <scalestack/kernel/module.h> 00028 #include <scalestack/kernel/option.h> 00029 #include <scalestack/proxy/server.h> 00030 #include <scalestack/proxy/server_service.h> 00031 #include <scalestack/network/ip/tcp/connection_service.h> 00032 00033 using namespace std; 00034 00035 namespace scalestack 00036 { 00037 namespace proxy 00038 { 00039 00040 /* 00041 * Public functions. 00042 */ 00043 00044 server_service::server_service(kernel::module& module): 00045 network::stream_service(module), 00046 _mutex(module), 00047 _clients() 00048 { 00049 } 00050 00051 network::stream* server_service::add_stream(void) 00052 { 00053 return new server(_module, *this); 00054 } 00055 00056 void server_service::add_client(client* client) 00057 { 00058 _mutex.lock(); 00059 _clients.push_back(client); 00060 _mutex.unlock(); 00061 static_cast<network::ip::tcp::connection_service&>( 00062 _module.get_core().get_service("proxy::server")).add_connections( 00063 _module.get_option("server_address").get_value()); 00064 } 00065 00066 bool server_service::remove_client(client* client) 00067 { 00068 _mutex.lock(); 00069 for (clients::iterator current = _clients.begin(); 00070 current != _clients.end(); 00071 ++current) 00072 { 00073 if (*current == client) 00074 { 00075 _clients.erase(current); 00076 _mutex.unlock(); 00077 return true; 00078 } 00079 } 00080 _mutex.unlock(); 00081 return false; 00082 } 00083 00084 client* server_service::get_client(void) 00085 { 00086 client* client = NULL; 00087 _mutex.lock(); 00088 if (!_clients.empty()) 00089 { 00090 client = _clients[0]; 00091 _clients.erase(_clients.begin()); 00092 } 00093 _mutex.unlock(); 00094 return client; 00095 } 00096 00097 } /* namespace proxy */ 00098 } /* namespace scalestack */
1.6.3