Skip to content

Commit

Permalink
#162 Code formatting. Replaced Semaphore with ReentrantLock.
Browse files Browse the repository at this point in the history
  • Loading branch information
hrosa committed May 31, 2016
1 parent 5c8fc61 commit 4bb34f2
Showing 1 changed file with 19 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import java.net.MalformedURLException;
import java.util.Iterator;
import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.ReentrantLock;

import org.apache.log4j.Logger;
import org.mobicents.media.ComponentType;
Expand Down Expand Up @@ -71,7 +71,7 @@ public class Play extends Signal implements PlayerListener {
private final Event of;

// Concurrency Properties
private final Semaphore terminateSemaphore;
private final ReentrantLock lock;

public Play(String name) {
super(name);
Expand All @@ -83,13 +83,13 @@ public Play(String name) {
this.of.add(new NotifyImmediately("N"));

// Concurrency Properties
this.terminateSemaphore = new Semaphore(1);
this.lock = new ReentrantLock();
}

@Override
public void execute() {
// get access to player
player = this.getPlayer();
player = (Player) getEndpoint().getResource(MediaType.AUDIO, ComponentType.PLAYER);

// check result
if (player == null) {
Expand Down Expand Up @@ -131,14 +131,14 @@ private void startAnnouncementPhase() {
player.setURL(uri);
} catch (MalformedURLException e) {
if (logger.isInfoEnabled()) {
logger.info("Received URL in invalid format , firing of");
logger.warn("Invalid URL format: " + uri);
}
of.fire(this, MSG_RC_301);
complete();
return;
} catch (ResourceUnavailableException e) {
if (logger.isInfoEnabled()) {
logger.info("Received URL can not be found , firing of");
logger.info("URL cannot be found: " + uri);
}
of.fire(this, MSG_RC_312);
complete();
Expand Down Expand Up @@ -180,10 +180,6 @@ public void cancel() {
terminate();
}

private Player getPlayer() {
return (Player) getEndpoint().getResource(MediaType.AUDIO, ComponentType.PLAYER);
}

@Override
public void reset() {
super.reset();
Expand All @@ -194,24 +190,21 @@ public void reset() {
}

private void terminate() {
this.lock.lock();
try {
terminateSemaphore.acquire();
} catch (InterruptedException e) {

}

if (player != null) {
player.removeListener(this);
player.deactivate();
player = null;
}
if (player != null) {
player.removeListener(this);
player.deactivate();
player = null;
}

if (options != null) {
Options.recycle(options);
options = null;
if (options != null) {
Options.recycle(options);
options = null;
}
} finally {
this.lock.unlock();
}

terminateSemaphore.release();
}

private void repeat(long delay) {
Expand All @@ -227,6 +220,7 @@ private void next(long delay) {
startAnnouncementPhase();
}

@Override
public void process(PlayerEvent event) {
switch (event.getID()) {
case PlayerEvent.STOP:
Expand Down

0 comments on commit 4bb34f2

Please sign in to comment.