org.lyra.LastDotFm
Class Sender

java.lang.Object
  extended by org.lyra.LastDotFm.Sender
All Implemented Interfaces:
java.lang.Runnable

public class Sender
extends java.lang.Object
implements java.lang.Runnable

The Sender class provides the ability to send requests to the Last.fm service and retrieve contents. All that is required to make a request is a Map element that contains all of the parameters and values necessary to make the request.

Requests can be made in one of two variants:

Interactive. Permits the user to make a request and then retrieve the contents returned from the Last.fm service. These will be the most common elements used in integration.

Map param = new HashMap(){ { put("method","album.getInfo"); put("album" ,"The Baroness"); put("artist" ,"Sarah Slean"); put("autocorrect" ,"1"); put("api_key" ,"b25b959554ed76058ac220b7b2e0a026"); } }; Sender requests = new Sender(logger, global, lang); String xmlresponse = requests.exsecuteParameters(param); ...

Queued. Permits a developer to queue requests and have them committed within a period of time. Since we limit the speed at which the requests are made this should prevent going beyond their terms of service while reducing code.

Map param = new HashMap(){ { put("method","album.getInfo"); put("album" ,"The Baroness"); put("artist" ,"Sarah Slean"); put("autocorrect" ,"1"); put("api_key" ,"b25b959554ed76058ac220b7b2e0a026"); } }; Sender requests = new Sender(logger, global, lang); requests.addParameters(param); ... ... requests.addParameters(param);

NOTE: The above examples never return an output. Useful for playlist elements such as playlist generation.

Since:
1.0.1
Version:
1.0.5, %Revision, 174%, %LastChangedDate, 27/10/12 3:33PM%
Author:
Martin Foster

Field Summary
private  HTTPClient Caller
           
private static java.lang.String CLASS
           
private static java.lang.String DIGEST
           
private  java.util.Map<java.lang.String,java.lang.String> Global
           
private  java.util.Map<java.lang.String,java.lang.String> Lang
           
private  Ledger Logger
           
private  java.lang.Thread myThread
           
private static long PERIOD
           
private  boolean processDone
           
private  java.util.LinkedList<java.util.Map<java.lang.String,java.lang.String>> Queue
           
private  java.util.LinkedList<java.lang.Long> Span
           
private static int THROTTLE
          Maximum amount of requests that can be made within a second according to the terms of service for Last.fm.
 
Constructor Summary
Sender(Ledger logger, java.util.Map<java.lang.String,java.lang.String> global, java.util.Map<java.lang.String,java.lang.String> lang)
          Base constructor for the class.
 
Method Summary
 void addParameters(java.util.Map<java.lang.String,java.lang.String> param)
          Permits us to add in parameters that will be processed in a Queue form and within the limitations as set by usage terms of Last.fm integration.
private  void addSpan()
          Inserts an element into the Span queue.
private  void addSpan(java.lang.Long process)
          Inserts an element into the Span queue.
 java.lang.String executeParameters(java.util.Map<java.lang.String,java.lang.String> current)
          Makes the request to the Last.fm service.
private  java.util.Map<java.lang.String,java.lang.String> getParameters()
          Extracts the oldest parameters from the queue in order to process them against the Last.fm site.
private  java.lang.Long getSpan()
          Performs the necessary calculations to make sure that our queued parameter handling is within the limits of the terms of service.
private  boolean moreParameters()
          Will return a boolean value on if there are other parameters to be process as necessary.
 void run()
           
private  void signParameters(java.util.Map<java.lang.String,java.lang.String> params)
          Every request made to the Last.fm requires a generated signature and included along with the parameters passed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

Logger

private Ledger Logger

Global

private java.util.Map<java.lang.String,java.lang.String> Global

Lang

private java.util.Map<java.lang.String,java.lang.String> Lang

Queue

private java.util.LinkedList<java.util.Map<java.lang.String,java.lang.String>> Queue

Span

private java.util.LinkedList<java.lang.Long> Span

myThread

private java.lang.Thread myThread

processDone

private boolean processDone

Caller

private HTTPClient Caller

CLASS

private static final java.lang.String CLASS
See Also:
Constant Field Values

DIGEST

private static final java.lang.String DIGEST
See Also:
Constant Field Values

THROTTLE

private static final int THROTTLE
Maximum amount of requests that can be made within a second according to the terms of service for Last.fm.

See Also:
Constant Field Values

PERIOD

private static final long PERIOD
See Also:
Constant Field Values
Constructor Detail

Sender

public Sender(Ledger logger,
              java.util.Map<java.lang.String,java.lang.String> global,
              java.util.Map<java.lang.String,java.lang.String> lang)
Base constructor for the class.

Parameters:
logger - Ledger instance for record processing.
global - Global settings map.
lang - Language map for multiple language support.
Method Detail

addSpan

private void addSpan()
Inserts an element into the Span queue. This will permit us to calculate the distance between the first and fist element in order to throttle the request queue as necessary.


addSpan

private void addSpan(java.lang.Long process)
Inserts an element into the Span queue. This will permit us to calculate the distance between the first and fist element in order to throttle the request queue as necessary.

Parameters:
process - Long unit-time element.

getSpan

private java.lang.Long getSpan()
Performs the necessary calculations to make sure that our queued parameter handling is within the limits of the terms of service. This will also consider interactive requests which are not queued nor are they paused.

Returns:
Long containing wait delay.

addParameters

public void addParameters(java.util.Map<java.lang.String,java.lang.String> param)
Permits us to add in parameters that will be processed in a Queue form and within the limitations as set by usage terms of Last.fm integration. Requests made here are not considered time critical or even necessarily guaranteed to be completed if the user closes the application.

Parameters:
param - Map containing parameters to send off to Last.fm.

getParameters

private java.util.Map<java.lang.String,java.lang.String> getParameters()
Extracts the oldest parameters from the queue in order to process them against the Last.fm site.

Returns:
Oldest parameter Map for request in queue.

moreParameters

private boolean moreParameters()
Will return a boolean value on if there are other parameters to be process as necessary.

Returns:
Boolean on requirement.

signParameters

private void signParameters(java.util.Map<java.lang.String,java.lang.String> params)
Every request made to the Last.fm requires a generated signature and included along with the parameters passed. This is used as a verification as to the generated source.

Parameters:
params - Map containing parameter list for passage.

executeParameters

public java.lang.String executeParameters(java.util.Map<java.lang.String,java.lang.String> current)
Makes the request to the Last.fm service. Making signatures, recording the time and returning the reply document.

Parameters:
current - Map containing parameters to pass on.
Returns:
String document containing the full reply.

run

public void run()
Specified by:
run in interface java.lang.Runnable