if (password == NULL) {
return;
}
-
try {
key = initKey();
password = NULL;// drop password
* Builds the URL for the given request.
*/
URL *CloudComm::buildRequest(bool isput, int64_t sequencenumber, int64_t maxentries) {
- IoTString *reqstring = isput ? "req=putslot" : "req=getslot";
- IoTString *urlstr = baseurl + "?" + reqstring + "&seq=" + sequencenumber;
+ const char *reqstring = isput ? "req=putslot" : "req=getslot";
+ char * buffer = (char *) malloc(baseurl->length() + 200);
+ memcpy(buffer, baseurl->internalBytes(), baseurl->length());
+ int offset = baseurl->length();
+ offset+=sprintf(&buffer[offset], "?%s&seq=%" PRId64, reqstring, sequencenumber);
if (maxentries != 0)
- urlstr += "&max=" + maxentries;
+ sprintf(&buffer[offset], "&max=%" PRId64, maxentries);
+ IoTString *urlstr = new IoTString(buffer);
+ free(buffer);
return new URL(urlstr);
}
void CloudComm::setSalt() {
-
if (salt != NULL) {
- // Salt already sent to server so dont set it again
+ // Salt already sent to server so don't set it again
return;
}
-
+
try {
Array<char> *saltTmp = new Array<char>(CloudComm_SALT_SIZE);
random->nextBytes(saltTmp);
- for (int i = 0; i < CloudComm_SALT_SIZE; i++) {
- printf("%d\n", (int)saltTmp->get(i) & 255);
- }
-
- URL *url = new URL(baseurl + "?req=setsalt");
+ char * buffer = (char *) malloc(baseurl->length() + 100);
+ memcpy(buffer, baseurl->internalBytes(), baseurl->length());
+ int offset = baseurl->length();
+ offset+=sprintf(&buffer[offset], "?req=setsalt");
+ IoTString *urlstr = new IoTString(buffer);
+ free(buffer);
+
+ URL *url = new URL(urlstr);
timer->startTime();
URLConnection *con = url->openConnection();
HttpURLConnection *http = (HttpURLConnection *) con;
-
+
http->setRequestMethod("POST");
http->setFixedLengthStreamingMode(saltTmp->length());
http->setDoOutput(true);
int responsecode = http->getResponseCode();
if (responsecode != HttpURLConnection_HTTP_OK) {
- // TODO: Remove this print
- printf("%d\n", responsecode);
throw new Error("Invalid response");
}
HttpURLConnection *http = NULL;
try {
- url = new URL(baseurl + "?req=getsalt");
+ char * buffer = (char *) malloc(baseurl->length() + 100);
+ memcpy(buffer, baseurl->internalBytes(), baseurl->length());
+ int offset = baseurl->length();
+ offset+=sprintf(&buffer[offset], "?req=getsalt");
+ IoTString *urlstr = new IoTString(buffer);
+ free(buffer);
+
+ url = new URL(urlstr);
} catch (Exception *e) {
throw new Error("getSlot failed");
}
IvParameterSpec *ivSpec = new IvParameterSpec(ivBytes);
Cipher *cipher = Cipher_getInstance("AES/CTR/NoPadding");
cipher->init(Cipher_ENCRYPT_MODE, key, ivSpec);
-
Array<char> *encryptedBytes = cipher->doFinal(rawData);
-
Array<char> *chars = new Array<char>(encryptedBytes->length() + CloudComm_IV_SIZE);
System_arraycopy(ivBytes, 0, chars, 0, ivBytes->length());
System_arraycopy(encryptedBytes, 0, chars, CloudComm_IV_SIZE, encryptedBytes->length());
Array<char> *encryptedBytes = new Array<char>(rawData->length() - CloudComm_IV_SIZE);
System_arraycopy(rawData, 0, ivBytes, 0, CloudComm_IV_SIZE);
System_arraycopy(rawData, CloudComm_IV_SIZE, encryptedBytes, 0, encryptedBytes->length);
-
IvParameterSpec *ivSpec = new IvParameterSpec(ivBytes);
Cipher *cipher = Cipher_getInstance("AES/CTR/NoPadding");
cipher->init(Cipher_DECRYPT_MODE, key, ivSpec);
liveKeys(new Hashset<IoTString *>) {
}
-
Commit::Commit(int64_t _sequenceNumber, int64_t _machineId, int64_t _transactionSequenceNumber) :
parts(new Hashtable<int32_t, CommitPart *>()),
missingParts(NULL),
}
void Commit::setDead() {
- if (isDead) {
- // Already dead
- return;
- }
-
- // Set dead
- isDead = true;
-
- // Make all the parts of this transaction dead
- for (int32_t partNumber : parts->keySet()) {
- CommitPart *part = parts->get(partNumber);
- part->setDead();
+ if (!isDead) {
+ isDead = true;
+ // Make all the parts of this transaction dead
+ for (int32_t partNumber : parts->keySet()) {
+ CommitPart *part = parts->get(partNumber);
+ part->setDead();
+ }
}
}
void Commit::createCommitParts() {
parts->clear();
-
// Convert to chars
Array<char> *charData = convertDataToBytes();
-
int commitPartCount = 0;
int currentPosition = 0;
int remaining = charData->length();
while (remaining > 0) {
-
bool isLastPart = false;
// determine how much to copy
int copySize = CommitPart_MAX_NON_HEADER_SIZE;
}
void Commit::decodeCommitData() {
-
// Calculate the size of the data section
int dataSize = 0;
for (int i = 0; i < parts->keySet()->size(); i++) {
void Commit::setKVsMap(Hashtable<IoTString *, KeyValue *> *newKVs) {
keyValueUpdateSet->clear();
- liveKeys->clear();
-
keyValueUpdateSet->addAll(newKVs->values());
+ liveKeys->clear();
liveKeys->addAll(newKVs->keySet());
}
Commit *Commit_merge(Commit *newer, Commit *older, int64_t newSequenceNumber) {
-
if (older == NULL) {
return newer;
} else if (newer == NULL) {
return older;
}
-
Hashtable<IoTString *, KeyValue *> *kvSet = new Hashtable<IoTString *, KeyValue *>();
for (KeyValue *kv : older->getKeyValueUpdateSet()) {
kvSet->put(kv->getKey(), kv);
}
-
for (KeyValue *kv : newer->getKeyValueUpdateSet()) {
kvSet->put(kv->getKey(), kv);
}
int64_t transactionSequenceNumber = newer->getTransactionSequenceNumber();
-
if (transactionSequenceNumber == -1) {
transactionSequenceNumber = older->getTransactionSequenceNumber();
}
Commit *newCommit = new Commit(newSequenceNumber, newer->getMachineId(), transactionSequenceNumber);
-
newCommit->setKVsMap(kvSet);
return newCommit;
return (3 * sizeof(int64_t)) + (2 * sizeof(int32_t)) + (2 * sizeof(char)) + data->length();
}
-void CommitPart::setSlot(Slot *s) {
- parentslot = s;
-}
-
int CommitPart::getPartNumber() {
return partNumber;
}
CommitPart(Slot *s, int64_t _machineId, int64_t _sequenceNumber, int64_t _transactionSequenceNumber, int _partNumber, Array<char> *_data, bool _isLastPart);
~CommitPart();
int getSize();
- void setSlot(Slot *s);
int getPartNumber();
int getDataSize();
Array<char> *getData();
superceded by a newer update. */
private:
bool islive;
-
-protected:
+ protected:
Slot *parentslot;
public:
* Returns a copy of the Entry that can be added to a different slot.
*/
virtual Entry *getCopy(Slot *s) = 0;
+ friend Entry *Entry_decode(Slot *slot, ByteBuffer *bb);
};
/**
Array<Slot *> *newSlots = cloud->getSlots(sequenceNumber + 1);
validateAndUpdate(newSlots, false);
sendToServer(NULL);
-
-
updateLiveTransactionsAndStatus();
-
return true;
} catch (Exception *e) {
for (int64_t m : localCommunicationTable->keySet()) {
// There is already an arbitrator
return false;
}
-
NewKey *newKey = new NewKey(NULL, keyName, machineId);
if (sendToServer(newKey)) {
}
TransactionStatus *Table::commitTransaction() {
-
if (pendingTransactionBuilder->getKVUpdates()->size() == 0) {
// transaction with no updates will have no effect on the system
return new TransactionStatus(TransactionStatus_StatusNoEffect, -1);
return localSequenceNumber;
}
-
-bool lastInsertedNewKey = false;
-
bool Table::sendToServer(NewKey *newKey) {
-
bool fromRetry = false;
-
try {
if (hadPartialSendToServer) {
Array<Slot *> *newSlots = cloud->getSlots(sequenceNumber + 1);
for (Transaction *transaction : lastTransactionPartsSent->keySet()) {
transaction->resetServerFailure();
-
// Update which transactions parts still need to be sent
transaction->removeSentParts(lastTransactionPartsSent->get(transaction));
-
// Add the transaction status to the outstanding list
outstandingTransactionStatus->put(transaction->getSequenceNumber(), transaction->getTransactionStatus());
// Update the transaction status
transaction->getTransactionStatus()->setStatus(TransactionStatus_StatusSentPartial);
- // Check if all the transaction parts were successfully sent and if so then remove it from pending
+ // Check if all the transaction parts were successfully
+ // sent and if so then remove it from pending
if (transaction->didSendAllParts()) {
transaction->getTransactionStatus()->setStatus(TransactionStatus_StatusSentFully);
pendingTransactionQueue->remove(transaction);
}
}
} else {
-
newSlots = sendSlotsReturn->getThird();
-
bool isInserted = false;
for (uint si = 0; si < newSlots->length(); si++) {
Slot *s = newSlots->get(si);
if (newKeyEntry != NULL) {
newKeyEntry->setSlot(slot);
if (slot->hasSpace(newKeyEntry)) {
-
slot->addEntry(newKeyEntry);
inserted = true;
}
return (4 * sizeof(int64_t)) + (2 * sizeof(int32_t)) + (2 * sizeof(char)) + data->length();
}
-void TransactionPart::setSlot(Slot *s) {
- parentslot = s;
-}
-
Pair<int64_t, int64_t> TransactionPart::getTransactionId() {
return transactionId;
}
}
int getSize();
- void setSlot(Slot *s);
Pair<int64_t, int64_t> getTransactionId();
int64_t getArbitratorId();
Pair<int64_t, int32_t> getPartId();
#ifndef URL_H
#define URL_H
+#include "common.h"
class URL {
+ public:
+ URL(IoTString *string);
};
#endif