/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package main;
import data.Question;
import data.QuestionList;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import model.Context;
/**
*
* @author User
*/
public class Server {
/**
* @param args the command line arguments
* @throws java.io.IOException
* @throws java.lang.ClassNotFoundException
*/
public static void main(String[] args) throws IOException, ClassNotFoundException {
QuestionList questionList = new QuestionList();
Context context = null;
int attempt = 0;
int rightAnswers = 0;
String result;
ServerSocket server = new ServerSocket(24891);
System.out.println("Server Started");
Socket socket = server.accept();
System.out.println("New client on port" + socket.getPort());
ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream input = new ObjectInputStream(socket.getInputStream());
while (true) {
if (questionList.next() != null) {
context = readQuestion(questionList.next());
}
output.writeObject(context);
output.flush();
context = (Context) input.readObject();
attempt++;
if (questionList.isRight(context.getVariant())) {
result = "Congratulations! It is the right answer";
rightAnswers++;
} else {
result = "It is the wrong answer";
}
result = result.concat(attempt % 10 == 0 ? (" " + rightAnswers + "/" + attempt) : "");
context.setResult(result);
output.writeObject(context);
output.flush();
}
}
private static Context readQuestion(Question question) {
Context c = new Context();
c.setQuestion(question.getQuestion());
c.setVariants(question.getVariants());
return c;
}
}