Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package robot;
- import java.net.*;
- import java.io.*;
- import java.util.Date;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- class Client implements Runnable{
- PrintWriter out = null;
- BufferedReader in = null;
- Socket clientSocket = null;
- Boolean running=true;
- private String state = "start";
- private String username="";
- private long startTime;
- public Client( Socket clientSocket) throws IOException {
- this.clientSocket=clientSocket;
- startTime = System.currentTimeMillis();
- }
- @Override
- public void run() {
- try {
- out = new PrintWriter(clientSocket.getOutputStream(), true);
- in = new BufferedReader(
- new InputStreamReader(
- clientSocket.getInputStream()));
- } catch (IOException e) {
- System.err.println("Couldn't get I/O.");
- System.exit(1);
- }
- String outputLine="";
- while (running) {
- try {
- outputLine=getOutput(in);
- } catch (IOException ex) {
- System.err.println("Couldn't get I/O.");
- }
- if(outputLine.equals("break")){
- stop();
- }else{
- if((new Date()).getTime() - startTime>=45*1000){
- outputLine="502 TIMEOUT";
- stop();
- }
- out.print(outputLine + System.getProperty("line.separator"));
- out.flush();
- }
- }
- System.out.println("thread ending");
- out.close();
- try {
- in.close();
- clientSocket.close();
- } catch (IOException ex) {
- Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public String getInput(BufferedReader in) throws IOException{
- String inputLine="";
- char ch;
- while (!inputLine.contains("\r\n")){
- ch = (char)in.read();
- if(ch != -1 ){
- inputLine+=ch;
- } else{
- inputLine="break";
- break;
- }
- }
- return inputLine.substring(0, inputLine.length()-2);
- }
- public String getOk(BufferedReader in) throws IOException{
- String inputLine="",outputLine="";
- char ch;
- for(int i=0;i<5;i++){
- if((ch = (char)in.read())!=-1){
- inputLine+=ch;
- } else{
- inputLine="break";
- break;
- }
- }
- if(inputLine.equals("FOTO ")){
- ch =(char)in.read();
- String numbers="";
- while(ch!=' '){
- numbers+=ch;
- ch = (char)in.read();
- }
- int number=Integer.parseInt(numbers);
- String foto=null;
- for(int i=0;i<number;i++){
- ch = (char)in.read();
- foto+=ch;
- }
- String control = "";
- int num;
- for(int i=0;i<8;i++){
- ch = (char)in.read();
- num = (int)ch;
- control+=num;
- }
- outputLine=processFoto(foto,control);
- } else if(inputLine.equals("INFO ")) {
- while (!inputLine.contains("\r\n")){
- ch = (char)in.read();
- if(ch != -1 ){
- inputLine+=ch;
- } else{
- inputLine="break";
- break;
- }
- }
- outputLine=processInfo(inputLine);
- } else{
- outputLine="501 SYNTAX ERROR";
- stop();
- }
- return outputLine;
- }
- public String getOutput(BufferedReader in) throws IOException{
- String input,output="";
- switch(state){
- case "start" :
- output="200 LOGIN";
- state="username";
- break;
- case "username" : if((input = getInput(in))=="break"){
- output=input;
- } else{
- output="201 PASSWORD";
- }
- state="password";
- username=input;
- break;
- case "password" : if((input = getInput(in))=="break"){
- output=input;
- } else{
- output = getPassword(input);
- }
- state="ok";
- break;
- case "ok" : output=getOk(in);
- }
- return output;
- }
- public String getPassword(String input){
- int pass=0;
- String output;
- for(char ch : username.toCharArray()){
- pass+=(int) ch;
- }
- if(pass==Integer.parseInt(input)){
- output="202 OK";
- } else{
- output="500 LOGIN FAILED";
- stop();
- }
- return output;
- }
- public String processInfo(String input){
- //ulozit do logu
- return "202 OK";
- }
- public String processFoto(String input,String control){
- int number=0;
- for(char ch:input.toCharArray()){
- number+=(int)ch;
- }
- String outputLine="";
- if(number==Integer.parseInt(control)){
- outputLine="202 OK";
- //ulozeni
- } else{
- outputLine="300 BAD CHECKSUM";
- }
- return outputLine;
- }
- private void stop(){
- running=false;
- }
- }
- public class Robot {
- public static void main(String[] args) throws IOException {
- if (args.length == 0) {
- System.err.println("Server: java robot.Robot <port>");
- System.exit(1);
- } else {
- System.out.println("Starting server...\n");
- Server server=new Server(Integer.parseInt( args[0]));
- }
- }
- }
- class Server{
- private ServerSocket serverSocket = null;
- private int port = 3645;
- boolean running=true;
- public Server(int port) throws IOException {
- try {
- serverSocket = new ServerSocket( port);
- } catch (IOException e) {
- System.err.println("Could not listen on port: " + port);
- System.exit(1);
- }
- while(running){
- Thread thr;
- try {
- Socket clientSocket = serverSocket.accept();
- Client client = new Client(clientSocket);
- thr = new Thread(client);
- thr.start();
- } catch (IOException e) {
- System.err.println("Accept failed.");
- System.exit(1);
- }
- }
- serverSocket.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement