📄 httpscoretransmitter.java
字号:
// (this one) has failed.
m_objErixMIDlet.saveHTTPStatus(m_objErixMIDlet.NOT_UPLOADED);
m_objErixMIDlet.m_bSendMenu = true;
}
finally {
// Close the InputStream
if (objInputStream != null) {
objInputStream.close();
}
// Close the DataOutputStream
if (objDataOutputStream != null) {
objDataOutputStream.close();
}
//Close the Connection
if (m_objConnection != null){
m_objConnection.close();
}
}
} // end of outer try block in which data is compiled and sent.
catch(Exception e)
{
System.out.println("Unable to send scores to highscores server " + e);
e.printStackTrace();
// If the exception occured due to the user cancelling the
// upload midway (resulting in the connection being closed),
// then return the error code to indicte this, otherwise
// return a general error code.
if (m_nHighScoreStatus == UPLOAD_CANCELLED) {
nHTTPResponseCode = UPLOAD_CANCELLED;
} else {
nHTTPResponseCode = UPLOAD_FAILED_OTHER_ERROR;
}
m_objErixMIDlet.bytHTTPStatus = m_objErixMIDlet.NOT_UPLOADED;
m_objErixMIDlet.saveHTTPStatus(m_objErixMIDlet.NOT_UPLOADED);
m_objErixMIDlet.m_bSendMenu = true;
}
return nHTTPResponseCode;
} // end of method sendScores()
/**
* This method builds the message header string with the delimiter and concatenates it
* with the all the high scores data.
* @return Returns a byte sequence of complete string to be sent over HTTP to the
* high score server.
*/
private byte[] buildString(byte[] arrbytNameNSscore, int nLastIndex, int nNumRecords)
throws UnsupportedEncodingException {
// Initialize the string used for compiling the data to be uploaded.
m_strFullString = new String("");
// The version of the authentication key generation algorithm used.
m_strFullString = m_strFullString.concat("v=" + m_strAuthVersion + m_strDelimiter);
// The unique serial number associated by the server with this instance of the game.
// Get the value of the serial number retrieved from the RMS:
//m_objErixMIDlet.retrieveSerialNumber();
//System.out.println("m_strSerialNumber retrieved from RMS = " + m_objErixMIDlet.m_strSerialNumber);
// If a serial number could be successfully retrieved from the RMS ...
if (m_objErixMIDlet.m_strSerialNumber != null) {
// Assign its value to the 'srl' parameter
m_strFullString = m_strFullString.concat("srl=" + m_objErixMIDlet.m_strSerialNumber + m_strDelimiter);
} else {
// Assign a vlaue of "\0" to the 'srl' parameter
m_strFullString = m_strFullString.concat("srl=\0" + m_strDelimiter);
// temp mod., for simulation:
// m_strFullString = m_strFullString.concat("srl=0BB32A525B7C470E9752B536C30F795A" + m_strDelimiter);
}
// Model of the device
//m_strFullString = m_strFullString.concat("m=" + m_objErixMIDlet.m_strMSID + m_strDelimiter);
m_strFullString = m_strFullString.concat("m=" + m_objErixMIDlet.getAppProperty("MSID") + m_strDelimiter);
// Game name
//m_strFullString = m_strFullString.concat("gn=" + m_objErixMIDlet.m_strMIDletName + m_strDelimiter);
m_strFullString = m_strFullString.concat("gn=" + m_objErixMIDlet.getAppProperty("MIDlet-Name") + m_strDelimiter);
// Game version
//m_strFullString = m_strFullString.concat("gv=" + m_objErixMIDlet.m_strMIDletVersion + m_strDelimiter);
m_strFullString = m_strFullString.concat("gv=" + m_objErixMIDlet.getAppProperty("MIDlet-Version") + m_strDelimiter);
// number of highscore entries being sent
m_strFullString = m_strFullString.concat("ng=" + Integer.toString(nNumRecords) + m_strDelimiter);
// The username entered by the user in the user authentication screen.
m_strFullString = m_strFullString.concat("usr=" + m_objErixMIDlet.m_strHighscoresServerUserid) + m_strDelimiter;
// The password entered by the user in the user authentication screen.
m_strFullString = m_strFullString.concat("pwd=" + m_objErixMIDlet.m_strHighscoresServerPassword) + m_strDelimiter;
// The ISO 639-1 name of the language currently set by the user.
m_strFullString = m_strFullString.concat("lang=" + m_objErixMIDlet.m_arrstrLanguageISOName[m_objErixMIDlet.m_nGameLanguage]) + m_strDelimiter;
// The various highscores records (consisting of signature, difficulty, level and score).
m_strFullString = m_strFullString.concat(new String(arrbytNameNSscore, 0, nLastIndex, "UTF-8"));
// Method for calculating the authentication key
char [] arrTemp = generateAuthenticationKey();
m_strFullString = m_strFullString.concat("key=" + new String(arrTemp) + m_strDelimiter);
//System.out.println("Full Uploaded String " + m_strFullString);
return m_strFullString.getBytes("UTF-8");
}
/**
* This method builds the message header string with the delimiter and concatenates it
* with the all the high scores data.
* @return Returns a byte sequence of complete string to be sent over HTTP to the
* high score server.
*/
private byte[] getNameScoreByteArray(String strPlayerName,
String strGameDiff,
String strGameLevel,
String strScore)
throws UnsupportedEncodingException {
// This array keeps the consecutive bytes for the name
byte[] arrPlayerName = strPlayerName.getBytes("UTF-8");
byte[] arrGameDiff = strGameDiff.getBytes("UTF-8");
byte[] arrGameLevel = strGameLevel.getBytes("UTF-8");
byte[] arrGameScore = strScore.getBytes("UTF-8");
// Calculate the total length of Score Data nIndex:
// No. of bytes for Name
// + No. of Bytes for Difficulty
// + No. of Bytes for Game level
// + No. of Bytes for the delimiter character after each datum.
int nRecordLength = strPlayerName.length() + 1
+ arrGameScore.length + 1
+ arrGameDiff.length + 1
+ arrGameLevel.length + 1;
// Creating an byte array to send through HTTP
byte[] arrNameAndScore = new byte[nRecordLength];
int nIndexForPlayerName = 0;
//int nScoreIndex = 0; unused local variable - keshav
byte bytDelimiter = m_strDelimiter.getBytes("UTF-8")[0];
// Getting the player name in the ouput array
for (int nIndex = 0 ;nIndex < strPlayerName.length() ;nIndex++ ) {
arrNameAndScore[nIndexForPlayerName++] = arrPlayerName[nIndex];
}
// Appending the delimiter (Line feed) character
arrNameAndScore[nIndexForPlayerName++] = bytDelimiter;
// Getting the Difficulty string in the output array
for (int nIndex = 0 ;nIndex < arrGameDiff.length ;nIndex++ ) {
arrNameAndScore[nIndexForPlayerName++] = arrGameDiff[nIndex];
}
// Appending the delimiter (Line feed) character
arrNameAndScore[nIndexForPlayerName++] = bytDelimiter;
// Getting the Level string in the ouput array
for (int nIndex = 0 ;nIndex < arrGameLevel.length ;nIndex++ ) {
arrNameAndScore[nIndexForPlayerName++] = arrGameLevel[nIndex];
}
// Appending the delimiter (Line feed) character
arrNameAndScore[nIndexForPlayerName++] = bytDelimiter;
// Getting the score string in the ouput array
for (int nIndex = 0 ;nIndex < arrGameScore.length ;nIndex++ ) {
arrNameAndScore[nIndexForPlayerName++] = arrGameScore[nIndex];
}
// Appending the delimiter (Line feed) character
arrNameAndScore[nIndexForPlayerName++] = bytDelimiter;
return arrNameAndScore;
} // end of method getNameScoreByteArray
/**
* Clears the references to any object referred-to by this object.
*/
public void cleanUp() {
m_objErixMIDlet = null;
}
/**
* Calculates the authentication key for all the data to be sent,
* excluding the authentication key itself.
*/
private char[] generateAuthenticationKey()
throws UnsupportedEncodingException {
byte[] bytArray;
int iLength;
int nCheckSum = 0;
char arrFullAuthKey[] = null;
// Code to assist in fuctional testing - Begin
/*
strbufPrintableData = new StringBuffer(2*m_strFullString.length());
for (int i=0; i<m_strFullString.length(); i++) {
// If current character of the String is a formfeed ...
if (m_strFullString.charAt(i) == '\f') {
// Copy it to the buffer as two separate characters
strbufPrintableData.append("\\");
strbufPrintableData.append("f");
}
// else if the current character of the String is a null (0x00) ...
else if (m_strFullString.charAt(i) == '\0') {
// Copy it to the buffer as two separate characters
strbufPrintableData.append('\\');
strbufPrintableData.append('0');
}
// Copy all other characters as-such
else {
strbufPrintableData.append(m_strFullString.charAt(i));
}
} // end for loop
*/
// Code to assist in fuctional testing - End
bytArray = m_strFullString.getBytes("UTF-8");
iLength = bytArray.length;
for (int i=0; i<iLength; i++) {
nCheckSum += bytArray[i];
}
// encoding the checksum
char[] temp = (Integer.toString(nCheckSum)).toCharArray();
int nSum = 0;
int nAuthKeyLength = 2*temp.length;
char arrAuthKey[] = new char[nAuthKeyLength];
for (int i = 0; i<temp.length; i++ ) {
int n = temp[i];
int y;
if (n < 50) {
y = (n + 8);
nSum += (2*(n-48)) + 8;
arrAuthKey[2*i] = (char)y;
arrAuthKey[2*i + 1] = temp[i];
} else {
y = (n - 2);
nSum += (2*(n-48)) - 2;
arrAuthKey[2*i] = (char)y;
arrAuthKey[2*i + 1] = temp[i];
}
}
/* Checking if the Check Sum is less than 100 */
bytArray = null;
nCheckSum = 0;
char[] arrCheckSum = (Integer.toString(nSum)).toCharArray();
int nTotalAuthKeyLen = arrCheckSum.length + arrAuthKey.length;
if (nSum < 100) {
nTotalAuthKeyLen += 1;
arrFullAuthKey = new char[nTotalAuthKeyLen];
int j = 1;
// Putting a zero at the start
arrFullAuthKey[0] = 48;
for (int i = 1 ;i<arrCheckSum.length+1;i++ ) {
arrFullAuthKey[i] = arrCheckSum[i-1];
j++;
}
for (int i = 0 ;i<arrAuthKey.length;i++,j++ ) {
arrFullAuthKey[j] = arrAuthKey[i];
}
} else { // if checksum is more then zero
arrFullAuthKey = new char[nTotalAuthKeyLen];
int j = 1;
for (int i = 0 ;i<arrCheckSum.length+1;i++ ) {
arrFullAuthKey[i] = arrCheckSum[i];
j++;
}
for (int i = 0 ;i<arrAuthKey.length;i++,j++ ) {
arrFullAuthKey[j] = arrAuthKey[i];
}
} // end of else of if nSum is less than 100
return arrFullAuthKey;
} // end of method generateAuthenticationKey()
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -