PageRenderTime 26ms CodeModel.GetById 1ms app.highlight 20ms RepoModel.GetById 2ms app.codeStats 0ms

/indra/test/llpipeutil.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 183 lines | 136 code | 16 blank | 31 comment | 5 complexity | 7aaf7307f47a1a449799dee51bc11e36 MD5 | raw file
  1/** 
  2 * @file llpipeutil.cpp
  3 * @date 2006-05-18
  4 * @brief Utility pipe fittings for injecting and extracting strings
  5 *
  6 * $LicenseInfo:firstyear=2006&license=viewerlgpl$
  7 * Second Life Viewer Source Code
  8 * Copyright (C) 2010, Linden Research, Inc.
  9 * 
 10 * This library is free software; you can redistribute it and/or
 11 * modify it under the terms of the GNU Lesser General Public
 12 * License as published by the Free Software Foundation;
 13 * version 2.1 of the License only.
 14 * 
 15 * This library is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 18 * Lesser General Public License for more details.
 19 * 
 20 * You should have received a copy of the GNU Lesser General Public
 21 * License along with this library; if not, write to the Free Software
 22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 23 * 
 24 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 25 * $/LicenseInfo$
 26 */
 27
 28
 29#include "linden_common.h"
 30#include "llpipeutil.h"
 31
 32#include <stdlib.h>
 33#include <sstream>
 34
 35#include "llbufferstream.h"
 36#include "lldefs.h"
 37#include "llframetimer.h"
 38#include "llpumpio.h"
 39#include "llrand.h"
 40#include "lltimer.h"
 41
 42F32 pump_loop(LLPumpIO* pump, F32 seconds)
 43{
 44	LLTimer timer;
 45	timer.setTimerExpirySec(seconds);
 46	while(!timer.hasExpired())
 47	{
 48		LLFrameTimer::updateFrameTime();			
 49		pump->pump();
 50		pump->callback();
 51	}
 52	return timer.getElapsedTimeF32();
 53}
 54
 55//virtual 
 56LLIOPipe::EStatus LLPipeStringInjector::process_impl(
 57		const LLChannelDescriptors& channels,
 58		buffer_ptr_t& buffer,
 59		bool& eos,
 60		LLSD& context,
 61		LLPumpIO* pump)
 62{
 63	buffer->append(channels.out(), (U8*) mString.data(), mString.size());
 64	eos = true;
 65	return STATUS_DONE;
 66}
 67
 68
 69LLIOPipe::EStatus LLPipeStringExtractor::process_impl(
 70	const LLChannelDescriptors& channels,
 71    buffer_ptr_t& buffer,
 72    bool& eos,
 73    LLSD& context,
 74    LLPumpIO* pump)
 75{
 76    if(!eos) return STATUS_BREAK;
 77    if(!pump || !buffer) return STATUS_PRECONDITION_NOT_MET;
 78
 79	LLBufferStream istr(channels, buffer.get());
 80	std::ostringstream ostr;
 81	while (istr.good())
 82	{
 83		char buf[1024];		/* Flawfinder: ignore */
 84		istr.read(buf, sizeof(buf));	/* Flawfinder: ignore */
 85		ostr.write(buf, istr.gcount());
 86	}
 87	mString = ostr.str();
 88	mDone = true;
 89	
 90	return STATUS_DONE;
 91}
 92
 93
 94// virtual
 95LLIOPipe::EStatus LLIOFuzz::process_impl(
 96	const LLChannelDescriptors& channels,
 97	buffer_ptr_t& buffer,
 98	bool& eos,
 99	LLSD& context,
100	LLPumpIO* pump)
101{
102	while(mByteCount)
103	{
104		std::vector<U8> data;
105		data.reserve(10000);
106		int size = llmin(10000, mByteCount);
107		std::generate_n(
108			std::back_insert_iterator< std::vector<U8> >(data),
109			size,
110			rand);
111		buffer->append(channels.out(), &data[0], size);
112		mByteCount -= size;
113	}
114	return STATUS_OK;
115}
116
117struct random_ascii_generator
118{
119	random_ascii_generator() {}
120	U8 operator()()
121	{
122		int rv = rand();
123		rv %= (127 - 32);
124		rv += 32;
125		return rv;
126	}
127};
128
129// virtual
130LLIOPipe::EStatus LLIOASCIIFuzz::process_impl(
131	const LLChannelDescriptors& channels,
132	buffer_ptr_t& buffer,
133	bool& eos,
134	LLSD& context,
135	LLPumpIO* pump)
136{
137	while(mByteCount)
138	{
139		std::vector<U8> data;
140		data.reserve(10000);
141		int size = llmin(10000, mByteCount);
142		std::generate_n(
143			std::back_insert_iterator< std::vector<U8> >(data),
144			size,
145			random_ascii_generator());
146		buffer->append(channels.out(), &data[0], size);
147		mByteCount -= size;
148	}
149	return STATUS_OK;
150}
151
152// virtual
153LLIOPipe::EStatus LLIONull::process_impl(
154	const LLChannelDescriptors& channels,
155	buffer_ptr_t& buffer,
156	bool& eos,
157	LLSD& context,
158	LLPumpIO* pump)
159{
160	return STATUS_OK;
161}
162
163// virtual
164LLIOPipe::EStatus LLIOSleeper::process_impl(
165	const LLChannelDescriptors& channels,
166	buffer_ptr_t& buffer,
167	bool& eos,
168	LLSD& context,
169	LLPumpIO* pump)
170{
171	if(!mRespond)
172	{
173		lldebugs << "LLIOSleeper::process_impl() sleeping." << llendl;
174		mRespond = true;
175		static const F64 SLEEP_TIME = 2.0;
176		pump->sleepChain(SLEEP_TIME);
177		return STATUS_BREAK;
178	}
179	lldebugs << "LLIOSleeper::process_impl() responding." << llendl;
180	LLBufferStream ostr(channels, buffer.get());
181	ostr << "huh? sorry, I was sleeping." << std::endl;
182	return STATUS_DONE;
183}