void LLPluginProcessChild::idle(void)
{
	bool idle_again;
	do
	{
		if(APR_STATUS_IS_EOF(mSocketError))
		{
			// Plugin socket was closed.  This covers both normal plugin termination and host crashes.
			setState(STATE_ERROR);
		}
		else if(mSocketError != APR_SUCCESS)
		{
			LL_INFOS("PluginChild") << "message pipe is in error state (" << mSocketError << "), moving to STATE_ERROR"<< LL_ENDL;
			setState(STATE_ERROR);
		}

		if((mState > STATE_INITIALIZED) && (mMessagePipe == NULL))
		{
			// The pipe has been closed -- we're done.
			// TODO: This could be slightly more subtle, but I'm not sure it needs to be.
			LL_INFOS("PluginChild") << "message pipe went away, moving to STATE_ERROR"<< LL_ENDL;
			setState(STATE_ERROR);
		}

		// If a state needs to go directly to another state (as a performance enhancement), it can set idle_again to true after calling setState().
		// USE THIS CAREFULLY, since it can starve other code.  Specifically make sure there's no way to get into a closed cycle and never return.
		// When in doubt, don't do it.
		idle_again = false;
		
		if(mInstance != NULL)
		{
			// Provide some time to the plugin
			mInstance->idle();
		}
		
		switch(mState)
		{
			case STATE_UNINITIALIZED:
			break;

			case STATE_INITIALIZED:
				if(mSocket->blockingConnect(mLauncherHost))
				{
					// This automatically sets mMessagePipe
					new LLPluginMessagePipe(this, mSocket);
					
					setState(STATE_CONNECTED);
				}
				else
				{
					// connect failed
					setState(STATE_ERROR);
				}
			break;
			
			case STATE_CONNECTED:
				sendMessageToParent(LLPluginMessage(LLPLUGIN_MESSAGE_CLASS_INTERNAL, "hello"));
				setState(STATE_PLUGIN_LOADING);
			break;
						
			case STATE_PLUGIN_LOADING:
				if(!mPluginFile.empty())
				{
					mInstance = new LLPluginInstance(this);
					if(mInstance->load(mPluginDir, mPluginFile) == 0)
					{
						mHeartbeat.start();
						mHeartbeat.setTimerExpirySec(HEARTBEAT_SECONDS);
						mCPUElapsed = 0.0f;
						setState(STATE_PLUGIN_LOADED);
					}
					else
					{
						setState(STATE_ERROR);
					}
				}
			break;
			
			case STATE_PLUGIN_LOADED:
				{
					setState(STATE_PLUGIN_INITIALIZING);
					LLPluginMessage message("base", "init");
					sendMessageToPlugin(message);
				}
			break;
			
			case STATE_PLUGIN_INITIALIZING:
				// waiting for init_response...
			break;
			
			case STATE_RUNNING:
				if(mInstance != NULL)
				{
					// Provide some time to the plugin
					LLPluginMessage message("base", "idle");
					message.setValueReal("time", PLUGIN_IDLE_SECONDS);
					sendMessageToPlugin(message);
					
					mInstance->idle();
					
					if(mHeartbeat.hasExpired())
					{
						
						// This just proves that we're not stuck down inside the plugin code.
						LLPluginMessage heartbeat(LLPLUGIN_MESSAGE_CLASS_INTERNAL, "heartbeat");
						
						// Calculate the approximage CPU usage fraction (floating point value between 0 and 1) used by the plugin this heartbeat cycle.
						// Note that this will not take into account any threads or additional processes the plugin spawns, but it's a first approximation.
						// If we could write OS-specific functions to query the actual CPU usage of this process, that would be a better approximation.
						heartbeat.setValueReal("cpu_usage", mCPUElapsed / mHeartbeat.getElapsedTimeF64());
						
						sendMessageToParent(heartbeat);

						mHeartbeat.reset();
						mHeartbeat.setTimerExpirySec(HEARTBEAT_SECONDS);
						mCPUElapsed = 0.0f;
					}
				}
				// receivePluginMessage will transition to STATE_UNLOADING
			break;

			case STATE_UNLOADING:
				if(mInstance != NULL)
				{
					sendMessageToPlugin(LLPluginMessage("base", "cleanup"));
					delete mInstance;
					mInstance = NULL;
				}
				setState(STATE_UNLOADED);
			break;

			// Special case for when the plugin knows it's cleaned up -- MC
			case STATE_UNLOADING_CLEANED:
				if (mInstance)
				{
					delete mInstance;
					mInstance = NULL;
				}
				setState(STATE_UNLOADED);
			break;
			
			case STATE_UNLOADED:
				killSockets();
				setState(STATE_DONE);
			break;

			case STATE_ERROR:
				// Close the socket to the launcher
				killSockets();				
				// TODO: Where do we go from here?  Just exit()?
				setState(STATE_DONE);
			break;
			
			case STATE_DONE:
				// just sit here.
			break;
		}
	
	} while (idle_again);
}
void DemoMediaPlugin::receiveMessage(const char *message_string)
{
//	std::cerr << "DemoMediaPlugin::receiveMessage: received message: \"" << message_string << "\"" << std::endl;
	LLPluginMessage message_in;
	
	if(message_in.parse(message_string) >= 0)
	{
		std::string message_class = message_in.getClass();
		std::string message_name = message_in.getName();
		if(message_class == LLPLUGIN_MESSAGE_CLASS_BASE)
		{
			if(message_name == "init")
			{
				LLPluginMessage message("base", "init_response");
				LLSD versions = LLSD::emptyMap();
				versions[LLPLUGIN_MESSAGE_CLASS_BASE] = LLPLUGIN_MESSAGE_CLASS_BASE_VERSION;
				versions[LLPLUGIN_MESSAGE_CLASS_MEDIA] = LLPLUGIN_MESSAGE_CLASS_MEDIA_VERSION;
				// Normally a plugin would only specify one of these two subclasses, but this is a demo...
				versions[LLPLUGIN_MESSAGE_CLASS_MEDIA_BROWSER] = LLPLUGIN_MESSAGE_CLASS_MEDIA_BROWSER_VERSION;
				versions[LLPLUGIN_MESSAGE_CLASS_MEDIA_TIME] = LLPLUGIN_MESSAGE_CLASS_MEDIA_TIME_VERSION;
				message.setValueLLSD("versions", versions);
				sendMessage(message);
				
				// Plugin gets to decide the texture parameters to use.
				mDepth = 3;
				
				message.setMessage(LLPLUGIN_MESSAGE_CLASS_MEDIA, "texture_params");
				message.setValueS32("depth", mDepth);
				message.setValueU32("internalformat", GL_RGB);
				message.setValueU32("format", GL_RGB);
				message.setValueU32("type", GL_UNSIGNED_BYTE);
				message.setValueBoolean("coords_opengl", false);	// true == use OpenGL-style coordinates, false == (0,0) is upper left.
				sendMessage(message);
			}
			else if(message_name == "idle")
			{
				// no response is necessary here.
				update();
			}
			else if(message_name == "shutdown")
			{
				sendMessage(LLPluginMessage("base", "shutdown_response"));
				
				mDeleteMe = true;
			}
			else if(message_name == "shm_added")
			{
				SharedSegmentInfo info;
				info.mAddress =  (void*)message_in.getValueU32("address");
				info.mSize = (size_t)message_in.getValueS32("size");
				std::string name = message_in.getValue("name");
				
				
				std::cerr << "DemoMediaPlugin::receiveMessage: shared memory added, name: " << name 
					<< ", size: " << info.mSize 
					<< ", address: " << info.mAddress 
					<< std::endl;

				mSharedSegments.insert(SharedSegmentMap::value_type(name, info));
			
			}
			else if(message_name == "shm_remove")
			{
				std::string name = message_in.getValue("name");
				
				std::cerr << "DemoMediaPlugin::receiveMessage: shared memory remove, name = " << name << std::endl;

				SharedSegmentMap::iterator iter = mSharedSegments.find(name);
				if(iter != mSharedSegments.end())
				{
					if(mPixels == iter->second.mAddress)
					{
						// This is the currently active pixel buffer.  Make sure we stop drawing to it.
						mPixels = NULL;
					}
					mSharedSegments.erase(iter);
				}
				else
				{
					std::cerr << "DemoMediaPlugin::receiveMessage: unknown shared memory region!" << std::endl;
				}

				// Send the response so it can be cleaned up.
				LLPluginMessage message("base", "shm_remove_response");
				message.setValue("name", name);
				sendMessage(message);
			}
			else
			{
				std::cerr << "DemoMediaPlugin::receiveMessage: unknown base message: " << message_name << std::endl;
			}
		}
		else if(message_class == LLPLUGIN_MESSAGE_CLASS_MEDIA)
		{
			if(message_name == "size_change")
			{
				std::string name = message_in.getValue("name");
				S32 width = message_in.getValueS32("width");
				S32 height = message_in.getValueS32("height");
				S32 texture_width = message_in.getValueS32("texture_width");
				S32 texture_height = message_in.getValueS32("texture_height");
				
				LLPluginMessage message(LLPLUGIN_MESSAGE_CLASS_MEDIA, "size_change_response");
				message.setValue("name", name);
				message.setValueS32("width", width);
				message.setValueS32("height", height);
				message.setValueS32("texture_width", texture_width);
				message.setValueS32("texture_height", texture_height);
				sendMessage(message);

				if(!name.empty())
				{
					// Find the shared memory region with this name
					SharedSegmentMap::iterator iter = mSharedSegments.find(name);
					if(iter != mSharedSegments.end())
					{
						std::cerr << "Got size change, new size is " << width << " by " << height << std::endl;
						std::cerr << "    texture size is " << texture_width << " by " << texture_height << std::endl;
						
						mPixels = (unsigned char*)iter->second.mAddress;
						mWidth = width;
						mHeight = height;
						mTextureWidth = texture_width;
						mTextureHeight = texture_height;
						
						clear();
					}
				}
			}
			else if(message_name == "mouse_event")
			{
				std::string event = message_in.getValue("event");
				S32 x = message_in.getValueS32("x");
				S32 y = message_in.getValueS32("y");
// 				std::string modifiers = message.getValue("modifiers");

//				std::cerr << "DemoMediaPlugin::receiveMessage: mouse event \"" << event 
//					<< "\", coords " << x << ", " << y
//					<< std::endl;
				
				if(event == "down")
				{
					mouseDown(x, y);
				}
				else if(event == "up")
				{
					mouseUp(x, y);
				}
				else if(event == "move")
				{
					mouseMove(x, y);
				}
			}
			else
			{
				std::cerr << "DemoMediaPlugin::receiveMessage: unknown media message: " << message_string << std::endl;
			}
		}
		else if(message_class == LLPLUGIN_MESSAGE_CLASS_MEDIA_BROWSER)
		{
			if(message_name == "focus")
			{
				// foo = message_in.getValueBoolean("focused");
			}
			else if(message_name == "clear_cache")
			{
			}
			else if(message_name == "clear_cookies")
			{
			}
			else if(message_name == "enable_cookies")
			{
				// foo = message_in.getValueBoolean("enable");
			}
			else if(message_name == "proxy_setup")
			{
				// foo = message_in.getValueBoolean("enable");
				// bar = message_in.getValue("host");
				// baz = message_in.getValueS32("port");
			}
			else if(message_name == "browse_stop")
			{
			}
			else if(message_name == "browse_reload")
			{
				// foo = message_in.getValueBoolean("ignore_cache");
			}
			else if(message_name == "browse_forward")
			{
			}
			else if(message_name == "browse_back")
			{
			}
			else if(message_name == "set_status_redirect")
			{
				// foo = message_in.getValueS32("code");
				// bar = message_in.getValue("url");
			}
			else
			{
				std::cerr << "DemoMediaPlugin::receiveMessage: unknown media_browser message: " << message_string << std::endl;
			}
		}
		else
		{
			std::cerr << "DemoMediaPlugin::receiveMessage: unknown message class: " << message_class << std::endl;
		}

	}
}