Skip to content

Commit

Permalink
Merge branch 'release/1_07_2_final'
Browse files Browse the repository at this point in the history
  • Loading branch information
abarisain committed Dec 9, 2014
2 parents f320932 + ba27d2a commit b3b4182
Show file tree
Hide file tree
Showing 41 changed files with 720 additions and 822 deletions.
33 changes: 22 additions & 11 deletions JMPDComm/src/main/java/org/a0z/mpd/CommandQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/** A class to generate and send a command queue. */
final class CommandQueue {
public class CommandQueue implements Iterable<MPDCommand> {

private static final boolean DEBUG = false;

Expand All @@ -54,14 +55,14 @@ final class CommandQueue {

private int mCommandQueueStringLength;

CommandQueue() {
public CommandQueue() {
super();

mCommandQueue = new ArrayList<>();
mCommandQueueStringLength = getStartLength();
}

CommandQueue(final int size) {
public CommandQueue(final int size) {
super();

mCommandQueue = new ArrayList<>(size);
Expand Down Expand Up @@ -103,7 +104,7 @@ private static List<String[]> separatedQueueResults(final Iterable<String> lines
*
* @param commandQueue The command queue to add to this one.
*/
void add(final CommandQueue commandQueue) {
public void add(final CommandQueue commandQueue) {
mCommandQueue.addAll(commandQueue.mCommandQueue);
mCommandQueueStringLength += commandQueue.mCommandQueueStringLength;
}
Expand All @@ -114,7 +115,7 @@ void add(final CommandQueue commandQueue) {
* @param position The position of this command queue to add the new command queue.
* @param commandQueue The command queue to add to this one.
*/
void add(final int position, final CommandQueue commandQueue) {
public void add(final int position, final CommandQueue commandQueue) {
mCommandQueue.addAll(position, commandQueue.mCommandQueue);
mCommandQueueStringLength += commandQueue.mCommandQueueStringLength;
}
Expand All @@ -125,7 +126,7 @@ void add(final int position, final CommandQueue commandQueue) {
* @param position The position of this command queue to add the new command.
* @param command The command to add to this command queue.
*/
void add(final int position, final MPDCommand command) {
public void add(final int position, final MPDCommand command) {
mCommandQueue.add(position, command);
mCommandQueueStringLength += command.toString().length();
}
Expand All @@ -135,7 +136,7 @@ void add(final int position, final MPDCommand command) {
*
* @param command Command to add to the queue.
*/
void add(final MPDCommand command) {
public void add(final MPDCommand command) {
mCommandQueue.add(command);
mCommandQueueStringLength += command.toString().length();
}
Expand All @@ -145,12 +146,12 @@ void add(final MPDCommand command) {
*
* @param command Command to add to the queue.
*/
void add(final String command, final String... args) {
public void add(final String command, final String... args) {
add(new MPDCommand(command, args));
}

/** Clear the command queue. */
void clear() {
public void clear() {
mCommandQueueStringLength = getStartLength();
mCommandQueue.clear();
}
Expand All @@ -159,8 +160,18 @@ public boolean isEmpty() {
return mCommandQueue.isEmpty();
}

/**
* Returns an {@link java.util.Iterator} for the elements in this object.
*
* @return An {@code Iterator} instance.
*/
@Override
public Iterator<MPDCommand> iterator() {
return mCommandQueue.iterator();
}

/** Reverse the command queue order, useful for removing playlist entries. */
void reverse() {
public void reverse() {
Collections.reverse(mCommandQueue);
}

Expand All @@ -172,7 +183,7 @@ void reverse() {
* @throws IOException Thrown upon a communication error with the server.
* @throws MPDException Thrown if an error occurs as a result of command execution.
*/
List<String> send(final MPDConnection mpdConnection) throws IOException, MPDException {
public List<String> send(final MPDConnection mpdConnection) throws IOException, MPDException {
return send(mpdConnection, false);
}

Expand Down
62 changes: 12 additions & 50 deletions JMPDComm/src/main/java/org/a0z/mpd/MPD.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.a0z.mpd.item.Music;
import org.a0z.mpd.item.PlaylistFile;
import org.a0z.mpd.item.Stream;
import org.a0z.mpd.subsystem.Sticker;

import java.io.IOException;
import java.net.InetAddress;
Expand Down Expand Up @@ -1216,71 +1217,32 @@ public List<String[]> listAlbumArtists(final List<Album> albums)
* @throws MPDException Thrown if an error occurs as a result of command execution.
*/
public List<String> listAlbums() throws IOException, MPDException {
return listAlbums(null, false, true);
return listAlbums(null, false);
}

/**
* List all albums from database.
*
* @param useAlbumArtist use AlbumArtist instead of Artist
* @return {@code Collection} with all album names from database.
* @throws IOException Thrown upon a communication error with the server.
* @throws MPDException Thrown if an error occurs as a result of command execution.
*/
public List<String> listAlbums(final boolean useAlbumArtist) throws IOException, MPDException {
return listAlbums(null, useAlbumArtist, true);
}

/**
* List all albums from a given artist, including an entry for songs with no
* album tag.
* List all albums from a given artist.
*
* @param artist artist to list albums
* @param useAlbumArtist use AlbumArtist instead of Artist
* @return {@code Collection} with all album names from database.
* @throws IOException Thrown upon a communication error with the server.
* @throws MPDException Thrown if an error occurs as a result of command execution.
*/
public List<String> listAlbums(final String artist, final boolean useAlbumArtist)
throws IOException, MPDException {
return listAlbums(artist, useAlbumArtist, true);
}

/**
* List all albums from a given artist.
*
* @param artist artist to list albums
* @param useAlbumArtist use AlbumArtist instead of Artist
* @param includeUnknownAlbum include an entry for songs with no album tag
* @return {@code Collection} with all album names from the given
* artist present in database.
* @throws IOException Thrown upon a communication error with the server.
* @throws MPDException Thrown if an error occurs as a result of command execution.
*/
public List<String> listAlbums(final String artist, final boolean useAlbumArtist,
final boolean includeUnknownAlbum) throws IOException, MPDException {
boolean foundSongWithoutAlbum = false;

public List<String> listAlbums(final String artist, final boolean useAlbumArtist)
throws IOException, MPDException {
final List<String> response =
mConnection.sendCommand
(listAlbumsCommand(artist, useAlbumArtist));
mConnection.sendCommand(listAlbumsCommand(artist, useAlbumArtist));
final List<String> result;

final List<String> result = new ArrayList<>(response.size());
for (final String line : response) {
final String name = line.substring("Album: ".length());
if (name.isEmpty()) {
foundSongWithoutAlbum = true;
} else {
result.add(name);
}
}

// add a single blank entry to host all songs without an album set
if (includeUnknownAlbum && foundSongWithoutAlbum) {
result.add("");
if (response.isEmpty()) {
result = Collections.emptyList();
} else {
result = Tools.parseResponse(response, "Album");
Collections.sort(result);
}

Collections.sort(result);
return result;
}

Expand Down
4 changes: 0 additions & 4 deletions JMPDComm/src/main/java/org/a0z/mpd/MPDCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ public class MPDCommand {

public static final String MPD_CMD_CLOSE = "close";

public static final String MPD_CMD_COMMANDS = "commands";

public static final String MPD_CMD_CONSUME = "consume";

public static final String MPD_CMD_COUNT = "count";
Expand Down Expand Up @@ -89,8 +87,6 @@ public class MPDCommand {

public static final String MPD_CMD_PAUSE = "pause";

public static final String MPD_CMD_PERMISSION = "permission";

public static final String MPD_CMD_PING = "ping";

public static final String MPD_CMD_PLAY = "play";
Expand Down
6 changes: 6 additions & 0 deletions JMPDComm/src/main/java/org/a0z/mpd/MusicList.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ private void add(final Music music) {
mSongID.add(null);
}

if (songPos == -1) {
throw new IllegalStateException("Media server protocol error: songPos not " +
"included with the playlist changes included with the following " +
"music. Path:" + music.getFullPath() + " Name: " + music.getName());
}

mList.set(songPos, music);
mSongID.set(songPos, music.getSongId());
}
Expand Down
7 changes: 7 additions & 0 deletions JMPDComm/src/main/java/org/a0z/mpd/Tools.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

package org.a0z.mpd;

import org.a0z.mpd.exception.InvalidResponseException;

import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -253,6 +255,11 @@ public static String[] splitResponse(final String line) {
final int delimiterIndex = line.indexOf(':');
final String[] result = new String[2];

if (delimiterIndex == -1) {
throw new InvalidResponseException("Failed to parse server response key for line: " +
line);
}

result[0] = line.substring(0, delimiterIndex);

/** Skip ': ' */
Expand Down
26 changes: 5 additions & 21 deletions JMPDComm/src/main/java/org/a0z/mpd/connection/MPDConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.a0z.mpd.MPDStatusMonitor;
import org.a0z.mpd.Tools;
import org.a0z.mpd.exception.MPDException;
import org.a0z.mpd.subsystem.Reflection;

import java.io.BufferedReader;
import java.io.EOFException;
Expand All @@ -52,8 +53,6 @@
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import static org.a0z.mpd.Tools.VALUE;

/**
* Class representing a connection to MPD Server.
*/
Expand Down Expand Up @@ -127,23 +126,6 @@ public abstract class MPDConnection {
}
}

/**
* Puts all available commands into a set backed collection.
*
* @param response The media server response to the {@link MPDCommand#MPD_CMD_COMMANDS}
* command.
* @return A collection of available commands from the response.
*/
private static Collection<String> getCommands(final Collection<String> response) {
final Collection<String> commands = new HashSet<>(response.size());

for (final String[] pair : Tools.splitResponse(response)) {
commands.add(pair[VALUE]);
}

return commands;
}

/**
* Sets up connection to host/port pair with MPD password.
*
Expand All @@ -162,12 +144,14 @@ public final void connect(final InetAddress host, final int port, final String p
mPassword = password;
mSocketAddress = new InetSocketAddress(host, port);

final MPDCommand mpdCommand = new MPDCommand(MPDCommand.MPD_CMD_COMMANDS);
final MPDCommand mpdCommand = new MPDCommand(Reflection.CMD_ACTION_COMMANDS);
final CommandResult commandResult = processCommand(mpdCommand);

synchronized (mAvailableCommands) {
final Collection<String> response = Tools.
parseResponse(commandResult.getResult(), Reflection.CMD_RESPONSE_COMMANDS);
mAvailableCommands.clear();
mAvailableCommands.addAll(getCommands(commandResult.getResult()));
mAvailableCommands.addAll(response);
}

if (!commandResult.isHeaderValid()) {
Expand Down
Loading

0 comments on commit b3b4182

Please sign in to comment.