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/module.h> 00027 #include <scalestack/proxy/client.h> 00028 #include <scalestack/proxy/server.h> 00029 #include <scalestack/proxy/server_service.h> 00030 00031 namespace scalestack 00032 { 00033 namespace proxy 00034 { 00035 00036 /* 00037 * Public methods. 00038 */ 00039 00040 server::server(kernel::module& module, server_service& server_service): 00041 network::stream(module), 00042 _size(), 00043 _buffer(), 00044 _client(), 00045 _server_service(server_service) 00046 { 00047 } 00048 00049 void server::shutdown(void) 00050 { 00051 if (_client == NULL) 00052 stop(); 00053 else 00054 _client->shutdown(); 00055 } 00056 00057 void server::run(void) 00058 { 00059 client* client = _client; 00060 _client = NULL; 00061 client->stop(); 00062 stop(); 00063 } 00064 00065 void server::connected(void) 00066 { 00067 _client = _server_service.get_client(); 00068 if (_client == NULL) 00069 shutdown(); 00070 else 00071 _client->set_server(this); 00072 } 00073 00074 size_t server::read(uint8_t* buffer, size_t size) 00075 { 00076 _size = size; 00077 _buffer = buffer; 00078 return _client_write(); 00079 } 00080 00081 void server::flush_write(void) 00082 { 00083 _client->continue_write(); 00084 } 00085 00086 void server::continue_write(void) 00087 { 00088 consume(_client_write()); 00089 } 00090 00091 /* 00092 * Private methods. 00093 */ 00094 00095 size_t server::_client_write(void) 00096 { 00097 size_t written = _client->write(_buffer, _size, true); 00098 _size -= written; 00099 _buffer += written; 00100 return written; 00101 } 00102 00103 } /* namespace proxy */ 00104 } /* namespace scalestack */
1.6.3