Methods Summary |
---|
private void | addPendingTransaction(java.lang.String transId)
synchronized (mPendingTransactions) {
mPendingTransactions.add(new PendingTransaction(transId));
}
|
void | checkTimeout()
synchronized (mPendingTransactions) {
Iterator<PendingTransaction> iter = mPendingTransactions.iterator();
while (iter.hasNext()) {
PendingTransaction tx = iter.next();
if (tx.isExpired(mReplyTimeout)) {
notifyTimeout(tx);
} else {
break;
}
}
}
|
public void | connect()
mSmsService.addSmsListener(mSmsAddr, mSmsPort, mAssembler);
mReceiveQueue = new LinkedBlockingQueue<Primitive>();
mPendingTransactions = new LinkedList<PendingTransaction>();
mTimer = new Timer(mReplyTimeout);
new Thread(mTimer, "SmsDataChannel timer").start();
mConnected = true;
|
private java.lang.String | extractTrId(byte[] data)
int transIdStart = 4;
int index = transIdStart;
while(Character.isDigit(data[index])) {
index++;
}
int transIdLen = index - transIdStart;
try {
return new String(data, transIdStart, transIdLen, "UTF-8");
} catch (UnsupportedEncodingException e) {
return null;
}
|
public long | getLastActiveTime()
return mLastActive;
|
private void | handleError(byte[] data, int errCode, java.lang.String info)
String trId = extractTrId(data);
if (trId != null) {
mTxManager.notifyErrorResponse(trId, errCode, info);
removePendingTransaction(trId);
}
|
public boolean | isSendingQueueEmpty()
// Always true since we don't have a sending queue.
return true;
|
private boolean | needSendKeepAlive(long inactiveTime)
return mKeepAliveMillis - inactiveTime <= 500;
|
private void | notifyTimeout(com.android.im.imps.SmsDataChannel$PendingTransaction tx)
String transId = tx.mTransId;
mTxManager.notifyErrorResponse(transId, ImpsErrorInfo.TIMEOUT,
"Timeout");
removePendingTransaction(transId);
|
public void | onIncomingSms(byte[] data)
try {
Primitive p = mParser.parse(new ByteArrayInputStream(data));
mReceiveQueue.put(p);
removePendingTransaction(p.getTransactionID());
} catch (ParserException e) {
handleError(data, ImpsErrorInfo.PARSER_ERROR, e.getLocalizedMessage());
} catch (IOException e) {
handleError(data, ImpsErrorInfo.PARSER_ERROR, e.getLocalizedMessage());
} catch (InterruptedException e) {
handleError(data, ImpsErrorInfo.UNKNOWN_ERROR, e.getLocalizedMessage());
}
|
public Primitive | receivePrimitive()
return mReceiveQueue.take();
|
private void | removePendingTransaction(java.lang.String transId)
synchronized (mPendingTransactions) {
Iterator<PendingTransaction> iter = mPendingTransactions.iterator();
while (iter.hasNext()) {
PendingTransaction tx = iter.next();
if (tx.mTransId.equals(transId)) {
iter.remove();
break;
}
}
}
|
public boolean | resume()
return true;
|
public long | sendHeartbeat()
if (!mConnected) {
return 0;
}
long inactiveTime = SystemClock.elapsedRealtime() - mLastActive;
if (needSendKeepAlive(inactiveTime)) {
sendKeepAlive();
return mKeepAliveMillis;
} else {
return mKeepAliveMillis - inactiveTime;
}
|
private void | sendKeepAlive()
ImpsTransactionManager tm = mConnection.getTransactionManager();
AsyncTransaction tx = new AsyncTransaction(tm) {
@Override
public void onResponseError(ImpsErrorInfo error) {
}
@Override
public void onResponseOk(Primitive response) {
// Since we never request a new timeout value, the response
// can be ignored
}
};
tx.sendRequest(mKeepAlivePrimitive);
|
public void | sendPrimitive(Primitive p)
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
mSerializer.serialize(p, out);
mSplitter.split(out.toByteArray());
SmsService smsService = SystemService.getDefault().getSmsService();
SendFailureCallback sendFailureCallback
= new SendFailureCallback(p.getTransactionID());
while (mSplitter.hasNext()) {
smsService.sendSms(mSmsAddr, mSmsPort, mSplitter.getNext(),
sendFailureCallback);
}
mLastActive = SystemClock.elapsedRealtime();
addPendingTransaction(p.getTransactionID());
} catch (IOException e) {
mTxManager.notifyErrorResponse(p.getTransactionID(),
ImpsErrorInfo.SERIALIZER_ERROR, e.getLocalizedMessage());
} catch (SerializerException e) {
mTxManager.notifyErrorResponse(p.getTransactionID(),
ImpsErrorInfo.SERIALIZER_ERROR, e.getLocalizedMessage());
}
|
public void | shutdown()
mSmsService.removeSmsListener(this);
mTimer.stop();
mConnected = false;
|
public void | startKeepAlive(long interval)
if (!mConnected) {
throw new IllegalStateException();
}
if (interval <= 0) {
interval = mConnection.getConfig().getDefaultKeepAliveInterval();
}
mKeepAliveMillis = interval * 1000;
if (mKeepAliveMillis < 0) {
ImpsLog.log("Negative keep alive time. Won't send keep-alive");
}
mKeepAlivePrimitive = new Primitive(ImpsTags.KeepAlive_Request);
HeartbeatService heartbeatService
= SystemService.getDefault().getHeartbeatService();
if (heartbeatService != null) {
heartbeatService.startHeartbeat(this, mKeepAliveMillis);
}
|
public void | suspend()
// do nothing.
|