2013-12-13 09:19:49 -04:30
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2013 Miguel Angel Astor Romero
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
2013-11-05 11:44:26 -04:30
|
|
|
package ve.ucv.ciens.ccg.nxtcam.network;
|
|
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
|
import java.io.BufferedWriter;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
import java.io.OutputStreamWriter;
|
|
|
|
|
import java.net.InetAddress;
|
|
|
|
|
import java.net.Socket;
|
|
|
|
|
|
|
|
|
|
import ve.ucv.ciens.ccg.nxtcam.camera.CameraImageMonitor;
|
2013-11-06 11:49:11 -04:30
|
|
|
import ve.ucv.ciens.ccg.nxtcam.utils.Logger;
|
2013-11-05 11:44:26 -04:30
|
|
|
import ve.ucv.ciens.ccg.nxtcam.utils.ProjectConstants;
|
|
|
|
|
|
|
|
|
|
public class ImageTransferThread extends Thread{
|
|
|
|
|
private final String TAG = "IM_THREAD";
|
|
|
|
|
private final String CLASS_NAME = ImageTransferThread.class.getSimpleName();
|
|
|
|
|
|
2013-11-21 10:13:38 -04:30
|
|
|
private boolean pause, done;
|
2013-11-05 11:44:26 -04:30
|
|
|
private Object threadPauseMonitor;
|
|
|
|
|
private CameraImageMonitor camMonitor;
|
|
|
|
|
private Socket socket;
|
|
|
|
|
private BufferedWriter writer;
|
|
|
|
|
private BufferedReader reader;
|
|
|
|
|
private byte[] image;
|
2013-11-06 11:49:11 -04:30
|
|
|
private String serverIp;
|
2013-11-05 11:44:26 -04:30
|
|
|
|
2013-11-06 11:49:11 -04:30
|
|
|
public ImageTransferThread(String serverIp){
|
|
|
|
|
this.serverIp = serverIp;
|
2013-11-05 11:44:26 -04:30
|
|
|
pause = false;
|
|
|
|
|
done = false;
|
|
|
|
|
threadPauseMonitor = new Object();
|
|
|
|
|
socket = null;
|
|
|
|
|
writer = null;
|
|
|
|
|
reader = null;
|
|
|
|
|
camMonitor = CameraImageMonitor.getInstance();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void run(){
|
2013-11-06 11:49:11 -04:30
|
|
|
connectToServer();
|
|
|
|
|
if(socket.isConnected()){
|
2013-11-21 10:13:38 -04:30
|
|
|
Logger.log_e(TAG, CLASS_NAME + ".run() :: Not connected to a server. Finishing thread.");
|
2013-11-05 11:44:26 -04:30
|
|
|
}else{
|
|
|
|
|
while(!done){
|
|
|
|
|
checkPause();
|
|
|
|
|
image = camMonitor.getImageData();
|
|
|
|
|
// TODO: implement image transfer protocol.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-06 11:49:11 -04:30
|
|
|
private void connectToServer(){
|
2013-11-05 11:44:26 -04:30
|
|
|
try{
|
2013-11-21 10:13:38 -04:30
|
|
|
Logger.log_i(TAG, CLASS_NAME + ".connectToServer() :: Connecting to the server at " + serverIp);
|
2013-11-05 11:44:26 -04:30
|
|
|
socket = new Socket(InetAddress.getByName(serverIp), ProjectConstants.SERVER_TCP_PORT_1);
|
|
|
|
|
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
|
|
|
|
|
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
2013-11-21 10:13:38 -04:30
|
|
|
Logger.log_i(TAG, CLASS_NAME + ".connectToServer() :: Connection successful.");
|
2013-11-05 11:44:26 -04:30
|
|
|
}catch(IOException io){
|
2013-11-21 10:13:38 -04:30
|
|
|
Logger.log_e(TAG, CLASS_NAME + ".connectToServer() :: Connection failed with message: " + io.getMessage());
|
2013-11-06 11:49:11 -04:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void disconnect(){
|
|
|
|
|
if(socket != null && socket.isConnected()){
|
|
|
|
|
try{
|
|
|
|
|
socket.close();
|
|
|
|
|
}catch (IOException io) {
|
2013-11-21 10:13:38 -04:30
|
|
|
Logger.log_e(TAG, CLASS_NAME + ".connectToServer() :: " + io.getMessage());
|
2013-11-06 11:49:11 -04:30
|
|
|
}
|
2013-11-05 11:44:26 -04:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public synchronized void finish(){
|
|
|
|
|
done = true;
|
2013-11-21 10:13:38 -04:30
|
|
|
Logger.log_i(TAG, CLASS_NAME + ".finish() :: Finishing thread.");
|
2013-11-05 11:44:26 -04:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void checkPause(){
|
|
|
|
|
synchronized (threadPauseMonitor){
|
|
|
|
|
while(pause){
|
2013-11-21 10:13:38 -04:30
|
|
|
Logger.log_d(TAG, CLASS_NAME + ".checkPause() :: Pause requested.");
|
2013-11-05 11:44:26 -04:30
|
|
|
try{ threadPauseMonitor.wait(); }catch(InterruptedException ie){}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public synchronized void pauseThread(){
|
|
|
|
|
pause = true;
|
2013-11-21 10:13:38 -04:30
|
|
|
Logger.log_d(TAG, CLASS_NAME + ".pauseThread() :: Pausing thread.");
|
2013-11-05 11:44:26 -04:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public synchronized void resumeThread(){
|
2013-11-21 10:13:38 -04:30
|
|
|
Logger.log_d(TAG, CLASS_NAME + ".resumeThread() :: Resuming thread.");
|
2013-11-05 11:44:26 -04:30
|
|
|
synchronized (threadPauseMonitor) {
|
|
|
|
|
pause = false;
|
|
|
|
|
threadPauseMonitor.notifyAll();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isConnected(){
|
2013-11-06 11:49:11 -04:30
|
|
|
if(socket != null && socket.isConnected())
|
|
|
|
|
return true;
|
|
|
|
|
else
|
|
|
|
|
return false;
|
2013-11-05 11:44:26 -04:30
|
|
|
}
|
|
|
|
|
}
|