Search in sources :

Example 1 with AutoreleasePool

use of com.google.j2objc.annotations.AutoreleasePool in project actor-platform by actorapp.

the class ManagerActor method onInMessage.

@AutoreleasePool
private void onInMessage(byte[] data, int offset, int len) {
    // Log.d(TAG, "Received package");
    DataInput bis = new DataInput(data, offset, len);
    try {
        long authId = bis.readLong();
        long sessionId = bis.readLong();
        if (authId != this.authId || sessionId != this.sessionId) {
            throw new IOException("Incorrect header");
        }
        if (authKey != null) {
            EncryptedPackage encryptedPackage = new EncryptedPackage(bis);
            int seq = (int) encryptedPackage.getSeqNumber();
            if (seq != inSeq) {
                throw new IOException("Expected " + inSeq + ", got: " + seq);
            }
            inSeq++;
            // long start = Runtime.getActorTime();
            EncryptedCBCPackage usEncryptedPackage = new EncryptedCBCPackage(new DataInput(encryptedPackage.getEncryptedPackage()));
            byte[] ruPackage = serverUSDecryptor.decryptPackage(ByteStrings.longToBytes(seq), usEncryptedPackage.getIv(), usEncryptedPackage.getEncryptedContent());
            EncryptedCBCPackage ruEncryptedPackage = new EncryptedCBCPackage(new DataInput(ruPackage));
            byte[] plainText = serverRUDecryptor.decryptPackage(ByteStrings.longToBytes(seq), ruEncryptedPackage.getIv(), ruEncryptedPackage.getEncryptedContent());
            // Log.d(TAG, "Package decrypted in " + (Runtime.getActorTime() - start) + " ms, size: " + len);
            DataInput ptInput = new DataInput(plainText);
            long messageId = ptInput.readLong();
            byte[] ptPayload = ptInput.readProtoBytes();
            receiver.send(new ProtoMessage(messageId, ptPayload));
        } else {
            long messageId = bis.readLong();
            byte[] payload = bis.readProtoBytes();
            receiver.send(new ProtoMessage(messageId, payload));
        }
    } catch (IOException e) {
        Log.w(TAG, "Closing connection: incorrect package");
        Log.e(TAG, e);
        if (currentConnection != null) {
            try {
                currentConnection.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
            currentConnection = null;
            currentConnectionId = 0;
            outSeq = 0;
            inSeq = 0;
        // Log.d(TAG, "Set connection #" + 0);
        }
        checkConnection();
    }
}
Also used : DataInput(im.actor.runtime.bser.DataInput) EncryptedPackage(im.actor.core.network.mtp.entity.EncryptedPackage) ProtoMessage(im.actor.core.network.mtp.entity.ProtoMessage) EncryptedCBCPackage(im.actor.core.network.mtp.entity.EncryptedCBCPackage) IOException(java.io.IOException) IOException(java.io.IOException) AutoreleasePool(com.google.j2objc.annotations.AutoreleasePool)

Example 2 with AutoreleasePool

use of com.google.j2objc.annotations.AutoreleasePool in project actor-platform by actorapp.

the class AsyncStorageActor method loadHead.

@AutoreleasePool
public void loadHead(LoadItemCallback<T> callback) {
    List<ListEngineRecord> records = storage.loadForward(null, 1);
    if (records.size() != 1) {
        callback.onLoaded(null);
        return;
    }
    ListEngineRecord record = records.get(0);
    try {
        callback.onLoaded(Bser.parse(creator.createInstance(), record.getData()));
    } catch (IOException e) {
        e.printStackTrace();
        callback.onLoaded(null);
    }
}
Also used : ListEngineRecord(im.actor.runtime.storage.ListEngineRecord) IOException(java.io.IOException) AutoreleasePool(com.google.j2objc.annotations.AutoreleasePool)

Example 3 with AutoreleasePool

use of com.google.j2objc.annotations.AutoreleasePool in project actor-platform by actorapp.

the class AsyncStorageActor method loadItem.

@AutoreleasePool
public void loadItem(long key, LoadItemCallback<T> callback) {
    ListEngineRecord record = storage.loadItem(key);
    if (record != null) {
        try {
            T res = Bser.parse(creator.createInstance(), record.getData());
            callback.onLoaded(res);
        } catch (IOException e) {
            e.printStackTrace();
            callback.onLoaded(null);
        }
    } else {
        callback.onLoaded(null);
    }
}
Also used : ListEngineRecord(im.actor.runtime.storage.ListEngineRecord) IOException(java.io.IOException) AutoreleasePool(com.google.j2objc.annotations.AutoreleasePool)

Example 4 with AutoreleasePool

use of com.google.j2objc.annotations.AutoreleasePool in project actor-platform by actorapp.

the class AsyncStorageActor method addOrUpdate.

@AutoreleasePool
public void addOrUpdate(List<T> items) {
    if (items.size() == 1) {
        T item = items.get(0);
        storage.updateOrAdd(new ListEngineRecord(item.getEngineId(), item.getEngineSort(), item.getEngineSearch(), item.toByteArray()));
    } else if (items.size() > 0) {
        List<ListEngineRecord> updated = new ArrayList<>();
        for (T i : items) {
            updated.add(new ListEngineRecord(i.getEngineId(), i.getEngineSort(), i.getEngineSearch(), i.toByteArray()));
        }
        storage.updateOrAdd(updated);
    }
}
Also used : ListEngineRecord(im.actor.runtime.storage.ListEngineRecord) List(java.util.List) ArrayList(java.util.ArrayList) AutoreleasePool(com.google.j2objc.annotations.AutoreleasePool)

Example 5 with AutoreleasePool

use of com.google.j2objc.annotations.AutoreleasePool in project actor-platform by actorapp.

the class AsyncStorageActor method replace.

@AutoreleasePool
public void replace(List<T> items) {
    List<ListEngineRecord> updated = new ArrayList<>();
    for (T i : items) {
        updated.add(new ListEngineRecord(i.getEngineId(), i.getEngineSort(), i.getEngineSearch(), i.toByteArray()));
    }
    storage.clear();
    storage.updateOrAdd(updated);
}
Also used : ListEngineRecord(im.actor.runtime.storage.ListEngineRecord) ArrayList(java.util.ArrayList) AutoreleasePool(com.google.j2objc.annotations.AutoreleasePool)

Aggregations

AutoreleasePool (com.google.j2objc.annotations.AutoreleasePool)9 ListEngineRecord (im.actor.runtime.storage.ListEngineRecord)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)2 JUnitCore (org.junit.runner.JUnitCore)2 Result (org.junit.runner.Result)2 ExtensionRegistry (com.google.protobuf.ExtensionRegistry)1 EncryptedCBCPackage (im.actor.core.network.mtp.entity.EncryptedCBCPackage)1 EncryptedPackage (im.actor.core.network.mtp.entity.EncryptedPackage)1 ProtoMessage (im.actor.core.network.mtp.entity.ProtoMessage)1 DataInput (im.actor.runtime.bser.DataInput)1 EnumMap (java.util.EnumMap)1 HashMap (java.util.HashMap)1 IdentityHashMap (java.util.IdentityHashMap)1 List (java.util.List)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 WeakHashMap (java.util.WeakHashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 MapMsg (protos.MapMsg)1