Android Open Source - android-checkers-app Game Service Impl






From Project

Back to project page android-checkers-app.

License

The source code is released under:

Apache License

If you think the Android project android-checkers-app listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package edu.unlv.sudo.checkers.service.impl;
/*from   www .  ja  v  a2  s  . c om*/
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;

import org.codehaus.jackson.map.ObjectMapper;
import org.json.JSONObject;

import java.io.IOException;
import java.util.logging.Logger;

import edu.unlv.sudo.checkers.CheckersApplication;
import edu.unlv.sudo.checkers.model.Game;
import edu.unlv.sudo.checkers.model.Team;
import edu.unlv.sudo.checkers.service.GameService;
import edu.unlv.sudo.checkers.util.VolleySingleton;

/**
 * This {@link GameService} talks to the checkers API.
 */
public class GameServiceImpl implements GameService {

    private static final Logger LOGGER = Logger.getLogger(GameServiceImpl.class.getName());

    private static final String BASE_URL = "http://23.253.42.115:2020/checkers/game/";
    private static final String CREATE_ENDPOINT = "create/";
    private static final String JOIN_ENDPOINT = "join/";

    private static final String URL_PARAM_DEVICE_ID = "deviceId";
    private static final String URL_PARAM_TEAM = "color";

    private static GameService instance;
    private final ObjectMapper mapper = new ObjectMapper();

    /**
     * @return the instance of this service.
     */
    public static GameService getInstance() {
        return instance == null ? instance = new GameServiceImpl() : instance;
    }

    /**
     * Private constructor to prevent outside instantiation.
     */
    private GameServiceImpl() { }

    @Override
    public void joinGame(final String gameId, final Team team, final Listener listener) {
        LOGGER.fine("Joining game " + gameId + " with team " + team);

        final String url = BASE_URL + gameId + "/"
                + JOIN_ENDPOINT + "/" + CheckersApplication.getDeviceUid();

        issuePost(url, null, listener);
    }

    @Override
    public void newGame(final Team team, final Listener listener) {
        LOGGER.fine("Creating a new game with team " + team);

        final String url = BASE_URL + CREATE_ENDPOINT
                + "?" + URL_PARAM_DEVICE_ID + "=" + CheckersApplication.getDeviceUid()
                + "&" + URL_PARAM_TEAM + "=" + team.name();

        issueGet(url, listener);
    }

    @Override
    public void move(Game game, Listener listener) {
        LOGGER.fine("Moving game " + game);
        //TODO: implement me!
    }

    @Override
    public void update(String gameId, Listener listener) {
        LOGGER.fine("Requesting update for game " + gameId);

        final String url = BASE_URL + gameId;

        issueGet(url, listener);
    }


    /**
     * Issue a GET request to the game server.
     * @param url the URL to make the request to
     * @param listener the callback listener
     */
    private void issueGet(final String url, final Listener listener) {
        issuePost(url, null, listener);
    }

    /**
     * Issue a POST request to the game server.
     * @param url the URL to make the request to
     * @param body the {@link org.json.JSONObject} body of the request
     * @param listener the callback listener
     */
    private void issuePost(final String url, final JSONObject body, final Listener listener) {
        final JsonObjectRequest request = new JsonObjectRequest(
                url, body,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            listener.onGame(parseGame(response));
                        } catch (IOException e) {
                            listener.onError(e);
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        listener.onError(error);
                    }
                }
        );

        VolleySingleton.getInstance().getRequestQueue().add(request);
    }

    /**
     * Parse a {@link JSONObject} into a {@link edu.unlv.sudo.checkers.model.Game} object.
     * @param jsonObject the {@link JSONObject} to parse
     * @return the {@link edu.unlv.sudo.checkers.model.Game} result
     * @throws IOException if unable to parse the JSON
     */
    private Game parseGame(final JSONObject jsonObject) throws IOException {
        return mapper.readValue(jsonObject.toString(), Game.class);
    }
}




Java Source Code List

com.android.volley.AuthFailureError.java
com.android.volley.CacheDispatcher.java
com.android.volley.Cache.java
com.android.volley.DefaultRetryPolicy.java
com.android.volley.ExecutorDelivery.java
com.android.volley.NetworkDispatcher.java
com.android.volley.NetworkError.java
com.android.volley.NetworkResponse.java
com.android.volley.Network.java
com.android.volley.NoConnectionError.java
com.android.volley.ParseError.java
com.android.volley.RequestQueue.java
com.android.volley.Request.java
com.android.volley.ResponseDelivery.java
com.android.volley.Response.java
com.android.volley.RetryPolicy.java
com.android.volley.ServerError.java
com.android.volley.TimeoutError.java
com.android.volley.VolleyError.java
com.android.volley.VolleyLog.java
com.android.volley.toolbox.AndroidAuthenticator.java
com.android.volley.toolbox.Authenticator.java
com.android.volley.toolbox.BasicNetwork.java
com.android.volley.toolbox.ByteArrayPool.java
com.android.volley.toolbox.ClearCacheRequest.java
com.android.volley.toolbox.DiskBasedCache.java
com.android.volley.toolbox.HttpClientStack.java
com.android.volley.toolbox.HttpHeaderParser.java
com.android.volley.toolbox.HttpStack.java
com.android.volley.toolbox.HurlStack.java
com.android.volley.toolbox.ImageLoader.java
com.android.volley.toolbox.ImageRequest.java
com.android.volley.toolbox.JsonArrayRequest.java
com.android.volley.toolbox.JsonObjectRequest.java
com.android.volley.toolbox.JsonRequest.java
com.android.volley.toolbox.NetworkImageView.java
com.android.volley.toolbox.NoCache.java
com.android.volley.toolbox.PoolingByteArrayOutputStream.java
com.android.volley.toolbox.RequestFuture.java
com.android.volley.toolbox.StringRequest.java
com.android.volley.toolbox.Volley.java
edu.unlv.sudo.checkers.CheckersApplication.java
edu.unlv.sudo.checkers.CheckersBoard.java
edu.unlv.sudo.checkers.model.Board.java
edu.unlv.sudo.checkers.model.Game.java
edu.unlv.sudo.checkers.model.Location.java
edu.unlv.sudo.checkers.model.Piece.java
edu.unlv.sudo.checkers.model.Rules.java
edu.unlv.sudo.checkers.model.Team.java
edu.unlv.sudo.checkers.model.exception.InvalidBoardException.java
edu.unlv.sudo.checkers.model.exception.InvalidMoveException.java
edu.unlv.sudo.checkers.model.exception.OutOfTurnException.java
edu.unlv.sudo.checkers.service.GameService.java
edu.unlv.sudo.checkers.service.impl.GameServiceImpl.java
edu.unlv.sudo.checkers.util.VolleySingleton.java
edu.unlv.sudo.checkers.views.BoardView.java