📄 httpscoretransmitter.java
字号:
/*
* @(#)HTTPScoreTransmitter.java, 1.7
*
* Copyright (c) 2001-2004 Sony Ericsson Mobile Communication AB (SEMC).
*
* German Branch
* Heisenbergbogen 1, Aschheim 85609, Munich, Germany.
* All Rights Reserved.
*
* This software is the confidential and proprietary information of
* SEMC. ("Confidential Information"). You shall not disclose such
* Confidential Information and shall use it only in accordance with
* the terms of the license agreement you entered into with SEMC.
*
* @author Sudip, Rohit, Keshav.
* @version 1.7, 10/10/2003
*/
package com.sonyericsson.erix;
import javax.microedition.io.HttpConnection;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import javax.microedition.io.Connector;
import javax.microedition.lcdui.*;
/**
* This class implements the scoreTransmitter interface.
*/
public class HTTPScoreTransmitter implements Runnable, CommandListener {
/**
* Status code which indicates that
* the highscores upload operation was successful.
*/
public static final int UPLOAD_SUCCESSFUL = 0;
/**
* Status code which indicates that
* the highscores upload operation failed
* due to a reason other than authentication error.
*/
public static final int UPLOAD_FAILED_OTHER_ERROR = 1;
/**
* Status code which indicates that
* the highscores upload operation failed
* due to authentication error.
*/
public static final int UPLOAD_FAILED_AUTHENTICATION_ERROR = 2;
/**
* Status code which indicates that
* the highscores upload was cancelled by the user.
*/
public static final int UPLOAD_CANCELLED = 3;
/**
* Indicates whether the transmission of the Highscores
* was sucessful or not.
* Initialized to failure due to an error other than authentication error.
*/
public int m_nHighScoreStatus = UPLOAD_FAILED_OTHER_ERROR;
/**
* Remembers the result of the previous highscores upload operation.
* This is used to determine whenther or not the Form that displays
* the status to the user is to be updated or not.
*/
public int m_nPreviousHighScoreStatus = UPLOAD_FAILED_OTHER_ERROR;
/**
* Version number of the authentication algorithm used.
*/
public final String m_strAuthVersion = "11";
/**
* The HttpConnection object used to upload the highscores to the server.
*/
public HttpConnection m_objConnection;
/**
* Indicates whether server response is received or not.
*/
private boolean m_bServerResonseReceived;
private final int m_nResponseWaitCounter = 10;
/**
* The string which represents the entire data to be sent.
* This string is assembled by concatenating individual datum.
*/
public String m_strFullString;
/**
* The delimiter to be used after each datum
* during highscores upload to a server.
*/
private String m_strDelimiter = "\f"; // The formfeed charcter.
/**
* The data received regarding the highest rank (in any difficulty level)
* at which a score uploaded by user was entered in the highscores server.
*/
public String m_strReceivedDataHighestRank;
/**
* The data received regarding the difficulty level associated
* with the highest rank at which a score uploaded by user
* was entered in the highscores server.
*/
public String m_strReceivedDataDifficultySetting;
/**
* The authentication prompt text for attempted highscores upload.
*/
public String m_strReceivedDataAuthenticationPrompt;
/**
* The authentication help text.
*/
public String m_strReceivedDataAuthenticationHelp;
// Temp vars for getting a data dump
String strDataSent;
StringBuffer strbufPrintableData;
/**
* The URL of the high score server.
*/
private final String m_strHighScoreServerUrl;
//private final String m_strHighScoreServerUrl = "http://10.143.10.175/servlets/HS";
//private final String m_strHighScoreServerUrl = "http://localhost/hi.jsp";
//private final String m_strHighScoreServerUrl = "http://aurws702:8080/cip11/highscore.jsp";
//private final String m_strHighScoreServerUrl = "http://cip-ext.aus.teleca.se/cip/highscore/index.jsp";
//private final String m_strHighScoreServerUrl = "http://devtest5.akamai.com/mc_ejtest/highscore/index.jsp";
//private final String m_strHighScoreServerUrl = "http://213.61.181.187/servlets/HS";
//private final String m_strHighScoreServerUrl = "http://10.143.10.60/servlets/HS";
//private final String m_strHighScoreServerUrl = "http://demob.sonyericsson.com/fun/highscore/index.jsp";
//private final String m_strHighScoreServerUrl = "http://www.sonyericsson.com/fun/highscore/index.jsp";
//private final String m_strHighScoreServerUrl = "http://demoa.sonyericsson.com/fun/highscore/index.jsp";
/**
* Keeps the reference to the MIDlet object.
*/
private ErixMIDlet m_objErixMIDlet;
/**
* Constructor
*/
HTTPScoreTransmitter(ErixMIDlet objErixMIDLet) {
m_objErixMIDlet = objErixMIDLet;
m_strHighScoreServerUrl = m_objErixMIDlet.getAppProperty("URL");
}
/**
* This method attempts to send the high scores to the High Scores server.
* This method checks for 10 seconds for server response.If the server response
* comes within ten seconds of sending high scores, then no further checks are made for
* server response.
*/
public void run() {
// Remember the result of the previous upload operation.
m_nPreviousHighScoreStatus = m_nHighScoreStatus;
// Reset the result of the previous upload operation
m_nHighScoreStatus = UPLOAD_FAILED_OTHER_ERROR;
// Attempt to send acores to the high score server
m_nHighScoreStatus = sendScores();
// Inform the upload progres form that the operation is completed.
m_objErixMIDlet.m_objGameCanvas.m_objHighscoresUploadProgressForm
.uploadOperationCompleted();
} // end of method run()
/**
* The command handler for the "Cancel" command present on the
* Highscores Upload Progress Form. All other commands in the application
* are handled by the ErixMIDlet object.
*/
public void commandAction(Command c, Displayable d) {
// Handle the case when the user has chosen the "Cancel" command on
// the Highscores Upload Progress Form.
// The HTTPScoreTransmitter object is the command listener for only
// the Highscores Upload Progress Form, and the only command contained
// on that form is the Cancel command. Therefore, in this command
// handler method, we do not perform any checking for the current
// displayable or the command.
// Set the status indicator to indicate that the highscores upload
// was cancelled.
m_nHighScoreStatus = UPLOAD_CANCELLED;
try {
// Cancel the highscores upload:
// Close the HTTPConnection
m_objConnection.close();
} catch (Exception e) {
}
}
/**
* This method attempts to get the high score server response.
* If the data is available, it is decoded
* and the relevant information is extracted from it.
* @param HTTP connection object.
* @param InputStream object.
*/
public void getServerresponse(HttpConnection objConnection, InputStream objInputStream, int nResponseCode) {
try {
// Get the length and process the data
int iDataLength = (int)objConnection.getLength();
byte[] arrDataRead = null;
// Has any content been returned in the server's response?
if (iDataLength > 0) {
// System.out.println("iDataLength = " + iDataLength);
// To indicate the while loop in the run method to end
m_bServerResonseReceived = true;
arrDataRead = new byte[iDataLength];
objInputStream.read(arrDataRead);
m_strFullString = new String(arrDataRead, "UTF-8");
//System.out.println("m_strFullString: " + m_strFullString);
// Extract the various individual data:
int nDataStartIndex;
int nDataEndIndex;
// The server returns the "pos", "d" and "srl"
// parameters only if the upload operation was successful.
// Therefore, retrieve these parameters only if the
// response code returned from the server is
// UPLOAD_SUCCESSFUL
if (nResponseCode == 200) {
// Retrieving the following three parameters will
// return meaningful values:
// Highest rank achived:
nDataStartIndex = m_strFullString.indexOf("pos=");
nDataEndIndex = m_strFullString.indexOf(m_strDelimiter, nDataStartIndex);
m_strReceivedDataHighestRank =
m_strFullString.substring(nDataStartIndex+4,
nDataEndIndex);
// Difficulty level associated with
// the highest rank achieved:
nDataStartIndex = m_strFullString.indexOf("d=");
nDataEndIndex = m_strFullString.indexOf(m_strDelimiter, nDataStartIndex);
m_strReceivedDataDifficultySetting =
m_strFullString.substring(nDataStartIndex+2,
nDataEndIndex);
// Serial number
nDataStartIndex = m_strFullString.indexOf("srl=");
nDataEndIndex = m_strFullString.indexOf(m_strDelimiter, nDataStartIndex);
m_objErixMIDlet.m_strSerialNumber =
m_strFullString.substring(nDataStartIndex+4,
nDataEndIndex);
//save serial number - keshav 17 Jun 2003
m_objErixMIDlet.saveSerialNumber();
//end of if block which retrieves the value of the
// "pos" , "d" and "srl" parameters.
} else {
// Set the values of the placeholders for the above three parameters
// to indicate that these parameters could not be retrieved.
m_strReceivedDataHighestRank = null;
m_strReceivedDataDifficultySetting = null;
m_objErixMIDlet.m_strSerialNumber = null;
}
// The following parameters are returned everytime by
// the server, no matter if the upload operation
// succeeded or failed
// Authentication prompt text
nDataStartIndex = m_strFullString.indexOf("infLogin=");
nDataEndIndex = m_strFullString.indexOf(m_strDelimiter, nDataStartIndex);
m_strReceivedDataAuthenticationPrompt =
m_strFullString.substring(nDataStartIndex+9,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -