Advertisement
Guest User

Untitled

a guest
Sep 20th, 2014
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.42 KB | None | 0 0
  1.    
  2.  
  3.     import java.io.BufferedReader;
  4.     import java.io.FileWriter;
  5.     import java.io.IOException;
  6.     import java.io.InputStream;
  7.     import java.io.InputStreamReader;
  8.     import java.net.ServerSocket;
  9.     import java.net.Socket;
  10.     import java.io.FileNotFoundException;
  11.     import java.io.PrintWriter;
  12.     import java.io.UnsupportedEncodingException;
  13.     import java.util.Arrays;
  14.     import java.util.Calendar;
  15.     import java.io.BufferedWriter;
  16.      
  17.     import javax.swing.JOptionPane;
  18.      
  19.     import java.io.*;
  20.     import java.net.*;
  21.      
  22.     public class Camera extends Thread {
  23.      
  24.             // public static boolean first = true;
  25.             long TimeoutStart;
  26.             int TIMEOUT = 6000;
  27.             int PositionfromSocket = 5;
  28.             long timeDiff;
  29.             // int[] line=new int[26];
  30.             long receivedTime;
  31.             String indexOfReceived;
  32.             String rec;
  33.      
  34.             // static number m = null;
  35.      
  36.             BufferedReader in;
  37.             BufferedReader d;
  38.             BufferedWriter bw;
  39.             Socket clientSocket;
  40.             ServerSocket serverSocket;
  41.             int[] a = new int[26];
  42.             int j = 0;
  43.      
  44.             public Camera() throws IOException {
  45.      
  46.                     bw = new BufferedWriter(new FileWriter("output.txt", true));// open file
  47.                                                                                                                                             // to write
  48.                                                                                                                                             // to it
  49.      
  50.                     try {
  51.                             serverSocket = new ServerSocket(64540);
  52.                     } catch (IOException e1) {
  53.                             e1.printStackTrace();
  54.                     }
  55.             }
  56.      
  57.             public void run() {
  58.                     try {
  59.      
  60.                             // clientSocket = serverSocket.accept();
  61.                             // d = new BufferedReader(
  62.                             // new InputStreamReader(clientSocket.getInputStream()));
  63.                             clientSocket = serverSocket.accept();
  64.                             in = (BufferedReader) new BufferedReader(new InputStreamReader(
  65.                                             clientSocket.getInputStream()));
  66.      
  67.                     } catch (IOException e1) {
  68.                             // TODO Auto-generated catch block
  69.                             e1.printStackTrace();
  70.                     }
  71.                     int prev = 999;
  72.                     long timeCounter = System.currentTimeMillis();
  73.                     while (true) {
  74.                             try {
  75.      
  76.                                     for (int i = 0; i < 25; i++) {
  77.                                             int x = in.read() - '0';
  78.                                             a[j] = x;
  79.      
  80.                                             System.out.print(a[j]);
  81.                                             j++;
  82.                                            
  83.                                            
  84.                                     }
  85.                                     System.out.print("\n");
  86.                                     System.out.println("out");
  87.                                     j=0;
  88.                                     for(int i=0; i<25; i++)
  89.                                             System.out.print(a[i]);
  90.                                     System.out.print("\n");
  91.                                    
  92.                                     //System.out.println(" ");
  93.                                     // for(int i=0;i<26;i++)
  94.                                     // {
  95.                                     // //line[i]=in.read()-'0';
  96.                                     // bw.write(d.read());// add this line to file
  97.                                     //
  98.                                     //
  99.                                     // }
  100.                                     // if(in.readLine()!=null) System.out.println("line is null");
  101.                                     // line="11111111111111111111111111";
  102.                                     // System.out.println(in.read()-'0');
  103.      
  104.                                     receivedTime = System.currentTimeMillis();// get time now in
  105.                                                                                                                             // miliseconds
  106.                                     // PositionfromSocket = Integer.parseInt(line.substring(0, 1));
  107.                                     // // get
  108.                                     // the
  109.                                     // position
  110.                                     // of
  111.                                     // the
  112.                                     // player
  113.                                     // indexOfReceived = line.substring(14);// get the index
  114.                                     // rec = line.substring(0, 1);// add the position to new line
  115.                                     // rec += receivedTime + "";// add the received time
  116.                                     // rec += indexOfReceived;// add the index
  117.                                     // for(int i=0;i<line.length;i++){
  118.                                     // System.out.print(line[i]);
  119.                                     // bw.write(line[i]);// add this line to file
  120.                                     // }
  121.                                     // for(int i=0;i<line.length;i++)
  122.                                     // bw.write((char)line[i]);
  123.                                     // bw.newLine();
  124.                                     // bw.newLine();
  125.                                     bw.close();
  126.      
  127.                                     // PositionfromSocket = in.read()-'0';
  128.      
  129.                                     // System.out.println("Message time: "+(System.currentTimeMillis()-timeCounter));
  130.                                     timeCounter = System.currentTimeMillis();
  131.                                     // System.out.println("Socket Position "+PositionfromSocket);
  132.                                     // if(PositionfromSocket<1 || PositionfromSocket>9)
  133.                                     // { PositionfromSocket = prev;
  134.                                     // }
  135.                                     // else if((PositionfromSocket==prev))
  136.                                     // {
  137.                                     // timeDiff = System.currentTimeMillis() - TimeoutStart;
  138.                                     // ConstantHandler.TimeOutProgress= (int)
  139.                                     // ((TIMEOUT-timeDiff)*100/TIMEOUT);
  140.                                     // if (timeDiff >= TIMEOUT)//test the time out, if we reach
  141.                                     // timeout,
  142.                                     // {
  143.                                     // // ADD Visual Effects?????
  144.                                     // ConstantHandler.ready=true;
  145.                                     // ConstantHandler.position=PositionfromSocket;
  146.                                     // //ConstantHandler.TimeOutProgress=0;
  147.                                     // prev=999;
  148.                                     // //TimeoutStart=System.currentTimeMillis();
  149.                                     // //
  150.                                     // System.out.println("Received Position "+ConstantHandler.position);
  151.                                     // //JOptionPane.showMessageDialog(null,
  152.                                     // ConstantHandler.position);
  153.                                     // }
  154.                                     //
  155.                                     // }
  156.                                     // else
  157.                                     // {
  158.                                     // ConstantHandler.position=PositionfromSocket;
  159.                                     // TimeoutStart=System.currentTimeMillis();
  160.                                     // ConstantHandler.TimeOutProgress=100;
  161.                                     // //ConstantHandler.ready=true;
  162.                                     // prev=PositionfromSocket;
  163.                                     // }
  164.                             } catch (IOException e) {
  165.                                     System.err.println("Can not read from camera module.");
  166.                             }
  167.                     }
  168.             }
  169.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement