OutputStreamWriter outWriter = new OutputStreamWriter(outStream, "utf-8");
Scanner in = new Scanner(inStream);
PrintWriter out = new PrintWriter(outWriter, true /* Auto Flush */);
Response response = new Response();
List<String> question_names = new ArrayList<String>();
boolean done = false;
boolean input_sent = false;
boolean exclusionsAvailable = false;
int numTestCases = 0;
int tcMarker = 0;
// Pre-load the list of available questions
EntityTransaction tx = em.getTransaction();
if (tx.isActive())
{
LOGGER.error("ERROR: Transaction is active");
}
/**
* Database Query that pulls a list of all Questions in the database
* ordered by Question name
*/
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Question> cq = cb.createQuery(Question.class);
Root<Question> questionRoot = cq.from(Question.class);
cq.orderBy(cb.asc(questionRoot.get("name")));
TypedQuery<Question> nameQuery = em.createQuery(cq);
tx.begin();
List<Question> availQuestions = nameQuery.getResultList();
tx.commit();
LOGGER.info("Loaded " + availQuestions.size() + " questions.");
for (Iterator i = availQuestions.iterator(); i.hasNext();)
{
Question question = (Question) i.next();
question_names.add(question.getName().toUpperCase());
}
// Thread variables to track the Question and our location in it
Question question = new Question();
// Initialize the iterator with some dummy (aka empty) data
// Iterator will be properly initialized when the question has been picked
Iterator tcIterator = question.getTestCases().iterator();
TestCase tc = new TestCase();
while (!done)
{
String line;
try
{
line = in.nextLine();
} catch (NoSuchElementException exc)
{
line = null;
}
if (line == null)
{
done = true;
} else
{
if (!input_sent && line.trim().equalsIgnoreCase("DIRECTORY"))
{
for (int i = 0; i < question_names.size(); i++)
{
out.print(question_names.get(i) + "\r\n");
out.flush();
}
out.print("EOF\r\n");
out.flush();
done = true;
} else if (!input_sent
&& question_names.contains(line.trim().toUpperCase()))
{
for (int i = 0; i < availQuestions.size(); i++)
{
Question q = availQuestions.get(i);
// We can set the question here because we know that
// the question exists on the server--we're in this
// block
if (q.getName().equalsIgnoreCase(line.trim()))
{
question = q;
}
}
/**
* The following code must be wrapped in a transaction
* since the application will query the database to return
* the list of test cases associated with the Question.
*/
tx.begin();
LOGGER.debug("Found Question: " + question.getName());
numTestCases = question.getTestCases().size();
out.print(numTestCases + "\r\n");
out.flush();
tcMarker = 1;
// Reinitialize iterator with actual data
tcIterator = question.getTestCases().iterator();
tc = (TestCase) tcIterator.next();
for (int i = 0; i < tc.getInputs().size(); i++)
{
out.print(tc.getInputs().get(i) + "\r\n");
out.flush();
}
tx.commit();
/**
* End of Transaction
*/
out.print("EOF\r\n");
out.flush();
input_sent = true;
if (tc.getExcludes().size() > 0)
{
exclusionsAvailable = true;
}
} else if ((input_sent) && !line.trim().equals("EOF"))
{
// Save the responses received to an array (don't
// persist at this time since these are not graded
if (!line.equals(""))
{
LOGGER.debug("Received: " + line.trim());
response.addAnswer(line.trim());
}
} else if ((input_sent) && line.trim().equals("EOF"))
{
// Evaluate the inputs received (stored in array) against
// the expected values stored in the database and send
// the appropriate response back to client
response.setResult(RageLib.gradeTestCase(tc, response,
question.getVerbatim(), exclusionsAvailable));
if (response.getResult())
{
out.print("CORRECT\r\n");
out.flush();
LOGGER.debug("Correct response received for "
+ question.getName());
} else
{
out.print("INCORRECT\r\n");
out.flush();
LOGGER.debug("Incorrect response received for "
+ question.getName());
}
if (numTestCases == tcMarker)
{
done = true;
} else
{
response.setResult(true);
tcMarker++;
input_sent = false;
/**
* The following code must be wrapped in a transaction
* since the application will query the database to return
* the list of inputs associated with the Test Case.
*/
tx.begin();
tc = (TestCase) tcIterator.next();
for (int i = 0; i < tc.getInputs().size(); i++)
{
out.print(tc.getInputs().get(i) + "\r\n");
out.flush();
}
tx.commit();
/**
* End of Transaction
*/
out.print("EOF\r\n");
out.flush();
input_sent = true;
response.clearAnswers();
exclusionsAvailable = false;
if (tc.getExcludes().size() > 0)
{
exclusionsAvailable = true;
}