[vlma-devel] commit: Avoid storing data into instance fields. (Adrien Grand )
git version control
git at videolan.org
Thu Jan 1 23:54:47 CET 2009
vlma | branch: master | Adrien Grand <jpountz at videolan.org> | Thu Jan 1 23:47:00 2009 +0100| [068c47d600bd298d522f020309751680b6a18c7c] | committer: Adrien Grand
Avoid storing data into instance fields.
It make instances unusable concurrently.
> http://git.videolan.org/gitweb.cgi/vlma.git/?a=commit;h=068c47d600bd298d522f020309751680b6a18c7c
---
.../videolan/vlma/watcher/RtspStreamWatcher.java | 56 ++++++++-----------
1 files changed, 24 insertions(+), 32 deletions(-)
diff --git a/vlma-core/src/main/java/org/videolan/vlma/watcher/RtspStreamWatcher.java b/vlma-core/src/main/java/org/videolan/vlma/watcher/RtspStreamWatcher.java
index 77cfacf..4b82852 100644
--- a/vlma-core/src/main/java/org/videolan/vlma/watcher/RtspStreamWatcher.java
+++ b/vlma-core/src/main/java/org/videolan/vlma/watcher/RtspStreamWatcher.java
@@ -48,29 +48,20 @@ public class RtspStreamWatcher implements StreamWatcher {
private static final Logger logger = Logger.getLogger(DirectMulticastStreamWatcher.class);
/**
- * The thread used to wait.
- */
- private Thread waitThread;
-
- /**
* The time leaved to the socket to get RTSP data.
*/
private static final int WAIT_SPAN = 500;
/**
- * The socket used to connect to the RTSP server
- */
- private Socket mySocket = null;
-
- /**
* Check if the media is played or not by asking the RTSP server
*/
- public synchronized boolean isPlayed(Program program) {
+ public boolean isPlayed(Program program) {
String url = vlmaService.getUrl(program);
PrintWriter out = null;
BufferedReader in = null;
+ Socket mySocket;
try {
int port = configuration.getInt("vlma.streaming.rtsp.port");
mySocket = new Socket(program.getPlayer().getAddress(), port);
@@ -91,7 +82,9 @@ public class RtspStreamWatcher implements StreamWatcher {
boolean isPlayed = false;
/* Check server answer */
- startWaitingForTheSocket();
+ Thread waitThread = new Waiter(mySocket);
+ waitThread.setName("waitThread");
+ waitThread.start();
while(true) {
/* The waiter thread allow to exit this loop by closing the socket */
String line;
@@ -112,12 +105,8 @@ public class RtspStreamWatcher implements StreamWatcher {
}
}
- /* Close the socket */
- try {
- mySocket.close();
- } catch (IOException e) {
- /* Do nothing */
- }
+ /* Stop the thread if it hasn't already be done */
+ waitThread.interrupt();
/* Some log output */
if( isPlayed )
@@ -125,9 +114,6 @@ public class RtspStreamWatcher implements StreamWatcher {
else
logger.debug("The RTSP media " + program.getSapName() + " is NOT available.");
- /* Stop the thread if it hasn't already be done */
- waitThread.interrupt();
-
return isPlayed;
}
@@ -135,28 +121,34 @@ public class RtspStreamWatcher implements StreamWatcher {
* This wait WAIT_SPAN milliseconds for the socket to receive
* RTSP data
*/
- private Runnable waiter = new Runnable() {
+ private static class Waiter extends Thread {
+
+ private Socket socket;
+
+ public Waiter(Socket socket) {
+ this.socket = socket;
+ }
+
public void run() {
/* We start waiting */
try {
Thread.sleep(WAIT_SPAN);
/* Now the socket must stop receiving data */
try {
- mySocket.close();
+ socket.close();
+ } catch (IOException e) {
+ /* Do nothing */
+ }
+ } catch (InterruptedException e) {
+ } finally {
+ try {
+ socket.close();
} catch (IOException e) {
/* Do nothing */
}
- } catch (InterruptedException e) { }
+ }
}
- };
- /**
- * Start the waiting thread
- */
- private synchronized void startWaitingForTheSocket() {
- waitThread = new Thread(waiter);
- waitThread.setName("waitThread");
- waitThread.start();
}
/**
More information about the vlma-devel
mailing list