2011-07-13 17:53:17 +00:00
|
|
|
/*******************************************************************************
|
|
|
|
|
* Copyright (c) 2011 Ziver Koc
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
* THE SOFTWARE.
|
|
|
|
|
******************************************************************************/
|
2011-02-15 19:37:35 +00:00
|
|
|
package zutil.net.nio.worker.grid;
|
2009-02-26 17:10:57 +00:00
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
|
import java.util.Queue;
|
|
|
|
|
|
2010-10-27 18:02:44 +00:00
|
|
|
import zutil.io.MultiPrintStream;
|
2011-02-15 19:37:35 +00:00
|
|
|
import zutil.net.nio.NioClient;
|
|
|
|
|
import zutil.net.nio.message.GridMessage;
|
|
|
|
|
import zutil.net.nio.worker.ThreadedEventWorker;
|
|
|
|
|
import zutil.net.nio.worker.WorkerDataEvent;
|
2009-02-26 17:10:57 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This class is the client part of the grid.
|
|
|
|
|
* It connects to a grid server and requests new job.
|
|
|
|
|
* And then sends back the result to the server.
|
|
|
|
|
*
|
|
|
|
|
* @author Ziver
|
|
|
|
|
*/
|
2010-08-14 15:38:56 +00:00
|
|
|
@SuppressWarnings({ "unchecked", "rawtypes" })
|
2009-02-26 17:10:57 +00:00
|
|
|
public class GridClient extends ThreadedEventWorker {
|
|
|
|
|
private static LinkedList<GridJob> jobQueue;
|
|
|
|
|
private static GridThread thread;
|
|
|
|
|
private static NioClient network;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a new GridClient object and registers itself at the server
|
|
|
|
|
* and sets itself as a worker in NioClient
|
|
|
|
|
*
|
|
|
|
|
* @param thread the Thread interface to run for the jobs
|
|
|
|
|
* @param network the NioClient to use to communicate to the server
|
|
|
|
|
*/
|
|
|
|
|
public GridClient(GridThread thread, NioClient network){
|
|
|
|
|
jobQueue = new LinkedList<GridJob>();
|
|
|
|
|
GridClient.thread = thread;
|
|
|
|
|
GridClient.network = network;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Starts up the client and a couple of GridThreads.
|
|
|
|
|
* And registers itself as a worker in NioClient
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
public void initiate() throws IOException{
|
|
|
|
|
network.setDefaultWorker(this);
|
2010-08-14 15:38:56 +00:00
|
|
|
network.send(new GridMessage(GridMessage.REGISTER));
|
2009-02-26 17:10:57 +00:00
|
|
|
|
|
|
|
|
for(int i=0; i<Runtime.getRuntime().availableProcessors() ;i++){
|
|
|
|
|
Thread t = new Thread(thread);
|
|
|
|
|
t.start();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void messageEvent(WorkerDataEvent e) {
|
|
|
|
|
// ignores other messages than GridMessage
|
|
|
|
|
if(e.data instanceof GridMessage){
|
2010-08-14 15:38:56 +00:00
|
|
|
GridMessage msg = (GridMessage)e.data;
|
2009-02-26 17:10:57 +00:00
|
|
|
switch(msg.messageType()){
|
|
|
|
|
// Receive data from Server
|
|
|
|
|
case GridMessage.INIT_DATA:
|
|
|
|
|
thread.setInitData(msg.getData());
|
|
|
|
|
break;
|
|
|
|
|
case GridMessage.COMP_DATA:
|
2010-08-14 15:38:56 +00:00
|
|
|
jobQueue.add(new GridJob(msg.getJobQueueID(), (Queue)msg.getData()));
|
2009-02-26 17:10:57 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Register whit the server that the job is done
|
|
|
|
|
*
|
|
|
|
|
* @param jobID is the job id
|
|
|
|
|
* @param correct if the answer was right
|
|
|
|
|
* @param result the result of the computation
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
public static void jobDone(int jobID, boolean correct, Object result) throws IOException{
|
|
|
|
|
if(correct)
|
2010-08-14 15:38:56 +00:00
|
|
|
network.send(new GridMessage(GridMessage.COMP_SUCCESSFUL, jobID, result));
|
2009-02-26 17:10:57 +00:00
|
|
|
else
|
2010-08-14 15:38:56 +00:00
|
|
|
network.send(new GridMessage(GridMessage.COMP_INCORRECT, jobID, result));
|
2009-02-26 17:10:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Registers with the server that there was an
|
|
|
|
|
* error when computing this job
|
|
|
|
|
*
|
|
|
|
|
* @param jobID is the job id
|
|
|
|
|
*/
|
|
|
|
|
public static void jobError(int jobID){
|
|
|
|
|
try{
|
2010-08-14 15:38:56 +00:00
|
|
|
network.send(new GridMessage(GridMessage.COMP_SUCCESSFUL, jobID));
|
2009-02-26 17:10:57 +00:00
|
|
|
}catch(Exception e){e.printStackTrace();}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return a new job to compute
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
public static synchronized GridJob getNextJob() throws IOException{
|
|
|
|
|
if(jobQueue.isEmpty()){
|
2010-08-14 15:38:56 +00:00
|
|
|
network.send(new GridMessage(GridMessage.NEW_DATA));
|
2009-02-26 17:10:57 +00:00
|
|
|
while(jobQueue.isEmpty()){
|
|
|
|
|
try{Thread.sleep(100);}catch(Exception e){}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
MultiPrintStream.out.println("Starting job");
|
|
|
|
|
return jobQueue.poll();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|