/**
 *    Copyright 2014 Renren.com
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.renren.Wario.plugin;

import java.util.Scanner;

public class DefaultPlugin extends IPlugin {

	private String[] numbers;
	private String[] addresses;
	private final int maxOutStanding = 15;

	private String mode;

	@Override
	public void run() {
		readContext();

		String message = "";
		
		if (!client.isAvailable() || !client.state.ruok()) {
				message += "Client " + client.getConnectionString() + " is down!\n";
		}

		client.state.update();

		String newMode = client.state.getMode();
		if (mode != null && !mode.equals(newMode)) {
			mode = newMode;
			message += "Client " + client.getConnectionString() + " has changed mode to " + newMode + ".\n";
		}

		if (client.state.getOutStanding() > maxOutStanding) {
			message += "Client " + client.getConnectionString() + " exceed max outstanding.\n";
		}

		numbers = args[0].split(",");
		addresses = args[1].split(",");
		if(!"".equals(message)) {
			for(String number : numbers) {
				msgSender.sendMessage(number, message);
			}
			for(String address : addresses) {
				mailSender.sendMail(address, message);
			}
		}
		
		writeContext();
	}

	/**
	 * clusterContext: 
	 * connectionString1#Mode 
	 * connectionString2#Mode
	 * connectionString3#Mode
	 */
	private void readContext() {
		String text = new String(clusterContext);
		boolean exists = false;
		Scanner scanner = new Scanner(text);
		while (scanner.hasNext()) {
			String line = scanner.next();
			if (line.startsWith(client.getConnectionString())) {
				exists = true;
				String[] args = line.split("#");
				mode = args[1];
			}
		}
		scanner.close();

		if (!exists) {
			mode = client.state.getMode();
			String newLine = client.getConnectionString() + "#" + mode + "\n";
			text += newLine;
			clusterContext = text.getBytes();
		}
	}

	private void writeContext() {
		String res = "";
		String text = new String(clusterContext);
		Scanner scanner = new Scanner(text);
		while (scanner.hasNext()) {
			String line = scanner.next();
			if (line.startsWith(client.getConnectionString())) {
				String newLine = client.getConnectionString() + "#" + mode + "\n";
				res += newLine;
			} else {
				res += line;
			}
		}
		scanner.close();
		clusterContext = res.getBytes();
	}
}

@@ -777,16 +777,18 @@
 resses;%0A
+%09%0A
 %09private
@@ -819,12 +819,46 @@
 g = 
-1
+30;%0A%09private final int testTime = 
 5;%0A
+%09
 %0A%09pr
@@ -950,18 +950,16 @@
 e = %22%22;%0A
-%09%09
 %0A%09%09if (!
@@ -1105,25 +1105,24 @@
 e.update();%0A
-%0A
 %09%09String new
@@ -1330,40 +1330,264 @@
 %0A%09%09i
-f (client.state.getOutStanding()
+nt outStanding = 0;%0A%09%09for(int i = 0; i %3C testTime; ++ i) %7B%0A%09%09%09client.state.update();%0A%09%09%09outStanding += client.state.getOutStanding();%0A%09%09%09try %7B%0A%09%09%09%09Thread.sleep(1000);%0A%09%09%09%7D catch (InterruptedException e) %7B%0A%09%09%09%09e.printStackTrace();%0A%09%09%09%7D%0A%09%09%7D%0A%09%09if (outStanding
  %3E m
@@ -1599,16 +1599,27 @@
 Standing
+ * testTime
 ) %7B%0A%09%09%09m

Fix outstanding alert