package com.fourinone;_x000D_
_x000D_
import java.util.List;_x000D_
import java.util.ArrayList;_x000D_
_x000D_
public class MigrantWorker extends WorkerParallel implements ParkStatg_x000D_
{_x000D_
	String host,workerType,workerjarname;_x000D_
	int port;_x000D_
	private int selfIndex=-1;_x000D_
	private volatile boolean _interrupted;_x000D_
	_x000D_
	void waitWorkingByService(String workerType)_x000D_
	{_x000D_
		//publish service and reg heatbeat_x000D_
		//try{_x000D_
			//Worker wk = new WorkerService(this);_x000D_
			/*BeanContext.startWorker(workerType, this);_x000D_
			ParkPatternExector.createWorkerTypeNode(workerType, nodevalue);*/_x000D_
			_x000D_
			String[] wkcfg = ConfigContext.getWorkerConfig();_x000D_
			waitWorkingByService(wkcfg[0], Integer.parseInt(wkcfg[1]), workerType);_x000D_
		//}catch(RemoteException e){_x000D_
		//	System.out.println("waitWorking:"+e);_x000D_
		//}_x000D_
	}_x000D_
	_x000D_
	void waitWorkingByService(String host, int port, String workerType)//remove protected_x000D_
	{_x000D_
		//String[] wkcfg = ConfigContext.getWorkerConfig();_x000D_
		//startWorker(wkcfg[0], Integer.parseInt(wkcfg[1]), sn, mwk);_x000D_
		this.host = host;_x000D_
		this.port = port;_x000D_
		this.workerType = workerType;_x000D_
		//BeanContext.startWorker(host, port, workerType, this);_x000D_
		BeanContext.startWorker(host, port, workerType, this, this.getClass().equals(MigrantWorker.class));_x000D_
		ParkPatternExector.createWorkerTypeNode(workerType, host+":"+port);_x000D_
	}_x000D_
	_x000D_
	void waitWorkingByPark(String workerType)_x000D_
	{_x000D_
		ObjectBean ob = ParkPatternExector.createWorkerTypeNode(workerType, "wk_pk");_x000D_
		_x000D_
		while(true)_x000D_
		{_x000D_
			ObjectBean lastestOb = ParkPatternExector.getLastestObjectBean(ob);_x000D_
			WareHouse whouse = doTask((WareHouse)lastestOb.toObject());_x000D_
			ob = ParkPatternExector.updateObjectBean(lastestOb, whouse);_x000D_
		}_x000D_
	}_x000D_
	_x000D_
	protected Workman[] getWorkerElse()_x000D_
	{_x000D_
		return getWorkerElse(this.workerType);_x000D_
	}_x000D_
	_x000D_
	protected Workman[] getWorkerElse(String workerType)_x000D_
	{_x000D_
		/*List<Workman> wklist = new ArrayList<Workman>();_x000D_
		if(parallelPatternFlag!=1)_x000D_
		{_x000D_
			List<String[]> wslist = getWorkersService(host, port, workerType);_x000D_
			for(String[] wsinfo:wslist)_x000D_
				wklist.add(BeanContext.getWorkerElse(wsinfo[0], Integer.parseInt(wsinfo[1]), wsinfo[2]));_x000D_
		}_x000D_
		return wklist.toArray(new Workman[wklist.size()]);*/_x000D_
		return getWorkers(host, port, workerType);_x000D_
	}_x000D_
	_x000D_
	protected Workman getWorkerIndex(int index)_x000D_
	{_x000D_
		return getWorkerIndex(this.workerType, index);_x000D_
	}_x000D_
	_x000D_
	protected Workman getWorkerIndex(String workerType, int index)_x000D_
	{_x000D_
		//if(parallelPatternFlag!=1)_x000D_
		//{_x000D_
			List<String[]> wslist = getWorkersService(workerType);_x000D_
			if(index>=0&&index<wslist.size())_x000D_
			{_x000D_
				String[] wsinfo = wslist.get(index);_x000D_
				return BeanContext.getWorkman(wsinfo[0], Integer.parseInt(wsinfo[1]), wsinfo[2]);_x000D_
			}else return null;_x000D_
		//}_x000D_
		//return null;_x000D_
	}_x000D_
	_x000D_
	protected Workman[] getWorkerAll()_x000D_
	{_x000D_
		return getWorkerAll(this.workerType);_x000D_
	}_x000D_
	_x000D_
	protected Workman[] getWorkerAll(String workerType)_x000D_
	{_x000D_
		return getWorkers(null, 0, workerType);_x000D_
	}_x000D_
	_x000D_
	private Workman[] getWorkers(String host, int port, String workerType)_x000D_
	{_x000D_
		//System.out.println(host+":"+port);_x000D_
		List<Workman> wklist = new ArrayList<Workman>();_x000D_
		if(parallelPatternFlag!=1)_x000D_
		{_x000D_
			List<String[]> wslist = getWorkersService(workerType);_x000D_
			for(int i=0;i<wslist.size();i++)_x000D_
			{_x000D_
				String[] wsinfo = wslist.get(i);_x000D_
				//System.out.println(wsinfo[0]+":"+wsinfo[1]);_x000D_
				if(!wsinfo[0].equals(host)||Integer.parseInt(wsinfo[1])!=port)_x000D_
					wklist.add(BeanContext.getWorkman(wsinfo[0], Integer.parseInt(wsinfo[1]), wsinfo[2]));_x000D_
				else_x000D_
					selfIndex = i;_x000D_
			}_x000D_
		}_x000D_
		return wklist.toArray(new Workman[wklist.size()]);_x000D_
	}	_x000D_
	_x000D_
	protected int getSelfIndex()_x000D_
	{_x000D_
		if(selfIndex==-1)_x000D_
		{_x000D_
			List<String[]> wslist = getWorkersService(workerType);_x000D_
			for(int i=0;i<wslist.size();i++)_x000D_
			{_x000D_
				String[] wsinfo = wslist.get(i);_x000D_
				if(wsinfo[0].equals(host)&&Integer.parseInt(wsinfo[1])==port)_x000D_
					return i;_x000D_
			}_x000D_
		}_x000D_
		return selfIndex;_x000D_
	}_x000D_
	_x000D_
	protected Workman getWorkerElse(String workerType, String host, int port)_x000D_
	{_x000D_
		if(this.host.equals(host)&&this.port==port)_x000D_
			return null;_x000D_
			_x000D_
		List<String[]> wslist = getWorkersService(workerType);_x000D_
		if(wslist!=null){_x000D_
			for(int i=0;i<wslist.size();i++)_x000D_
			{_x000D_
				String[] wsinfo = wslist.get(i);_x000D_
				//System.out.println(wsinfo[0]+":"+wsinfo[1]);_x000D_
				_x000D_
				if(wsinfo[0].equals(host)&&Integer.parseInt(wsinfo[1])==port)_x000D_
					return BeanContext.getWorkman(wsinfo[0], Integer.parseInt(wsinfo[1]), wsinfo[2]);_x000D_
			}_x000D_
		}_x000D_
		return null;_x000D_
	}_x000D_
	_x000D_
	synchronized boolean receiveMaterials(WareHouse inhouse)_x000D_
	{_x000D_
		return receive(inhouse);_x000D_
	}_x000D_
	_x000D_
	protected boolean receive(WareHouse inhouse)_x000D_
	{_x000D_
		return true;_x000D_
	}_x000D_
	_x000D_
	protected WareHouse doTask(WareHouse inhouse)_x000D_
	{_x000D_
		WareHouse outhouse = new WareHouse();_x000D_
		return outhouse;_x000D_
	}_x000D_
	_x000D_
	protected boolean isInterrupted(){_x000D_
		return _interrupted;_x000D_
	}_x000D_
	_x000D_
	void interrupted(boolean _interrupted){_x000D_
		this._interrupted = _interrupted;_x000D_
	}_x000D_
	_x000D_
	public void setWorkerJar(String workerjarname){_x000D_
		this.workerjarname = workerjarname;_x000D_
	}_x000D_
	_x000D_
	public String getWorkerJar(){_x000D_
		return workerjarname;_x000D_
	}_x000D_
	_x000D_
	void setHost(String host){_x000D_
		this.host = host;_x000D_
	}_x000D_
	_x000D_
	String getHost(){_x000D_
		return host;_x000D_
	}_x000D_
	_x000D_
	void setWorkerType(String workerType){_x000D_
		this.workerType = workerType;_x000D_
	}_x000D_
	_x000D_
	String getWorkerType(){_x000D_
		return workerType;_x000D_
	}_x000D_
	_x000D_
	void setPort(int port){_x000D_
		this.port = port;_x000D_
	}_x000D_
	_x000D_
	int getPort(){_x000D_
		return port;_x000D_
	}_x000D_
	_x000D_
	void setSelfIndex(int selfIndex){_x000D_
		this.selfIndex = selfIndex;_x000D_
	}_x000D_
	_x000D_
	/*int getSelfIndex(){_x000D_
		return selfIndex;_x000D_
	}*/_x000D_
	_x000D_
	public static void main(String[] args){_x000D_
		MigrantWorker mw = new MigrantWorker();_x000D_
		//System.out.println(args.length);_x000D_
		if(args.length==4)_x000D_
			mw.setWorkerJar(args[3]);_x000D_
		mw.waitWorking(args[0],Integer.parseInt(args[1]),args[2]);_x000D_
	}_x000D_
}
/*int getSelfIndex(){_x000D_
		return selfIndex;_x000D_
	}*/
package com.fourinone;_x000D_
_x000D_
import java.util.List;_x000D_
import java.util.ArrayList;_x000D_
_x000D_
public class MigrantWorker extends WorkerParallel implements ParkStatg_x000D_
{_x000D_
	String host,workerType,workerjarname;_x000D_
	int port;_x000D_
	private int selfIndex=-1;_x000D_
	private volatile boolean _interrupted;_x000D_
	_x000D_
	void waitWorkingByService(String workerType)_x000D_
	{_x000D_
		//publish service and reg heatbeat_x000D_
		//try{_x000D_
			//Worker wk = new WorkerService(this);_x000D_
			/*BeanContext.startWorker(workerType, this);_x000D_
			ParkPatternExector.createWorkerTypeNode(workerType, nodevalue);*/_x000D_
			_x000D_
			String[] wkcfg = ConfigContext.getWorkerConfig();_x000D_
			waitWorkingByService(wkcfg[0], Integer.parseInt(wkcfg[1]), workerType);_x000D_
		//}catch(RemoteException e){_x000D_
		//	System.out.println("waitWorking:"+e);_x000D_
		//}_x000D_
	}_x000D_
	_x000D_
	void waitWorkingByService(String host, int port, String workerType)//remove protected_x000D_
	{_x000D_
		//String[] wkcfg = ConfigContext.getWorkerConfig();_x000D_
		//startWorker(wkcfg[0], Integer.parseInt(wkcfg[1]), sn, mwk);_x000D_
		this.host = host;_x000D_
		this.port = port;_x000D_
		this.workerType = workerType;_x000D_
		//BeanContext.startWorker(host, port, workerType, this);_x000D_
		BeanContext.startWorker(host, port, workerType, this, this.getClass().equals(MigrantWorker.class));_x000D_
		ParkPatternExector.createWorkerTypeNode(workerType, host+":"+port);_x000D_
	}_x000D_
	_x000D_
	void waitWorkingByPark(String workerType)_x000D_
	{_x000D_
		ObjectBean ob = ParkPatternExector.createWorkerTypeNode(workerType, "wk_pk");_x000D_
		_x000D_
		while(true)_x000D_
		{_x000D_
			ObjectBean lastestOb = ParkPatternExector.getLastestObjectBean(ob);_x000D_
			WareHouse whouse = doTask((WareHouse)lastestOb.toObject());_x000D_
			ob = ParkPatternExector.updateObjectBean(lastestOb, whouse);_x000D_
		}_x000D_
	}_x000D_
	_x000D_
	protected Workman[] getWorkerElse()_x000D_
	{_x000D_
		return getWorkerElse(this.workerType);_x000D_
	}_x000D_
	_x000D_
	protected Workman[] getWorkerElse(String workerType)_x000D_
	{_x000D_
		/*List<Workman> wklist = new ArrayList<Workman>();_x000D_
		if(parallelPatternFlag!=1)_x000D_
		{_x000D_
			List<String[]> wslist = getWorkersService(host, port, workerType);_x000D_
			for(String[] wsinfo:wslist)_x000D_
				wklist.add(BeanContext.getWorkerElse(wsinfo[0], Integer.parseInt(wsinfo[1]), wsinfo[2]));_x000D_
		}_x000D_
		return wklist.toArray(new Workman[wklist.size()]);*/_x000D_
		return getWorkers(host, port, workerType);_x000D_
	}_x000D_
	_x000D_
	protected Workman getWorkerIndex(int index)_x000D_
	{_x000D_
		return getWorkerIndex(this.workerType, index);_x000D_
	}_x000D_
	_x000D_
	protected Workman getWorkerIndex(String workerType, int index)_x000D_
	{_x000D_
		//if(parallelPatternFlag!=1)_x000D_
		//{_x000D_
			List<String[]> wslist = getWorkersService(workerType);_x000D_
			if(index>=0&&index<wslist.size())_x000D_
			{_x000D_
				String[] wsinfo = wslist.get(index);_x000D_
				return BeanContext.getWorkman(wsinfo[0], Integer.parseInt(wsinfo[1]), wsinfo[2]);_x000D_
			}else return null;_x000D_
		//}_x000D_
		//return null;_x000D_
	}_x000D_
	_x000D_
	protected Workman[] getWorkerAll()_x000D_
	{_x000D_
		return getWorkerAll(this.workerType);_x000D_
	}_x000D_
	_x000D_
	protected Workman[] getWorkerAll(String workerType)_x000D_
	{_x000D_
		return getWorkers(null, 0, workerType);_x000D_
	}_x000D_
	_x000D_
	private Workman[] getWorkers(String host, int port, String workerType)_x000D_
	{_x000D_
		//System.out.println(host+":"+port);_x000D_
		List<Workman> wklist = new ArrayList<Workman>();_x000D_
		if(parallelPatternFlag!=1)_x000D_
		{_x000D_
			List<String[]> wslist = getWorkersService(workerType);_x000D_
			for(int i=0;i<wslist.size();i++)_x000D_
			{_x000D_
				String[] wsinfo = wslist.get(i);_x000D_
				//System.out.println(wsinfo[0]+":"+wsinfo[1]);_x000D_
				if(!wsinfo[0].equals(host)||Integer.parseInt(wsinfo[1])!=port)_x000D_
					wklist.add(BeanContext.getWorkman(wsinfo[0], Integer.parseInt(wsinfo[1]), wsinfo[2]));_x000D_
				else_x000D_
					selfIndex = i;_x000D_
			}_x000D_
		}_x000D_
		return wklist.toArray(new Workman[wklist.size()]);_x000D_
	}	_x000D_
	_x000D_
	protected int getSelfIndex()_x000D_
	{_x000D_
		if(selfIndex==-1)_x000D_
		{_x000D_
			List<String[]> wslist = getWorkersService(workerType);_x000D_
			for(int i=0;i<wslist.size();i++)_x000D_
			{_x000D_
				String[] wsinfo = wslist.get(i);_x000D_
				if(wsinfo[0].equals(host)&&Integer.parseInt(wsinfo[1])==port)_x000D_
					return i;_x000D_
			}_x000D_
		}_x000D_
		return selfIndex;_x000D_
	}_x000D_
	_x000D_
	protected Workman getWorkerElse(String workerType, String host, int port)_x000D_
	{_x000D_
		if(this.host.equals(host)&&this.port==port)_x000D_
			return null;_x000D_
			_x000D_
		List<String[]> wslist = getWorkersService(workerType);_x000D_
		if(wslist!=null){_x000D_
			for(int i=0;i<wslist.size();i++)_x000D_
			{_x000D_
				String[] wsinfo = wslist.get(i);_x000D_
				//System.out.println(wsinfo[0]+":"+wsinfo[1]);_x000D_
				_x000D_
				if(wsinfo[0].equals(host)&&Integer.parseInt(wsinfo[1])==port)_x000D_
					return BeanContext.getWorkman(wsinfo[0], Integer.parseInt(wsinfo[1]), wsinfo[2]);_x000D_
			}_x000D_
		}_x000D_
		return null;_x000D_
	}_x000D_
	_x000D_
	synchronized boolean receiveMaterials(WareHouse inhouse)_x000D_
	{_x000D_
		return receive(inhouse);_x000D_
	}_x000D_
	_x000D_
	protected boolean receive(WareHouse inhouse)_x000D_
	{_x000D_
		return true;_x000D_
	}_x000D_
	_x000D_
	protected WareHouse doTask(WareHouse inhouse)_x000D_
	{_x000D_
		WareHouse outhouse = new WareHouse();_x000D_
		return outhouse;_x000D_
	}_x000D_
	_x000D_
	protected boolean isInterrupted(){_x000D_
		return _interrupted;_x000D_
	}_x000D_
	_x000D_
	void interrupted(boolean _interrupted){_x000D_
		this._interrupted = _interrupted;_x000D_
	}_x000D_
	_x000D_
	public void setWorkerJar(String workerjarname){_x000D_
		this.workerjarname = workerjarname;_x000D_
	}_x000D_
	_x000D_
	public String getWorkerJar(){_x000D_
		return workerjarname;_x000D_
	}_x000D_
	_x000D_
	void setHost(String host){_x000D_
		this.host = host;_x000D_
	}_x000D_
	_x000D_
	String getHost(){_x000D_
		return host;_x000D_
	}_x000D_
	_x000D_
	void setWorkerType(String workerType){_x000D_
		this.workerType = workerType;_x000D_
	}_x000D_
	_x000D_
	String getWorkerType(){_x000D_
		return workerType;_x000D_
	}_x000D_
	_x000D_
	void setPort(int port){_x000D_
		this.port = port;_x000D_
	}_x000D_
	_x000D_
	int getPort(){_x000D_
		return port;_x000D_
	}_x000D_
	_x000D_
	void setSelfIndex(int selfIndex){_x000D_
		this.selfIndex = selfIndex;_x000D_
	}_x000D_
	_x000D_
	/*int getSelfIndex(){_x000D_
<SUF>*/_x000D_
	_x000D_
	public static void main(String[] args){_x000D_
		MigrantWorker mw = new MigrantWorker();_x000D_
		//System.out.println(args.length);_x000D_
		if(args.length==4)_x000D_
			mw.setWorkerJar(args[3]);_x000D_
		mw.waitWorking(args[0],Integer.parseInt(args[1]),args[2]);_x000D_
	}_x000D_
}