static void handleNotificationReceived(OSNotification notification) { if (OfflinePushMessages.fetchPushMessages(true)) { OneSignal.Current.ClearAndroidOneSignalNotifications(); } }
// Handle timer routines static public void onUpdate(object source, ElapsedEventArgs e) { // Update the friendlist FriendList.Update(); // Cleanup the presence list // TODO: optimize this by using a different thread perhaps PresenceList.performCleanup(); if (Node.walletStorage.getPrimaryAddress() == null) { return; } if (Config.enablePushNotifications) { OfflinePushMessages.fetchPushMessages(); } // Request initial wallet balance if (balance.blockHeight == 0 || balance.lastUpdate + 300 < Clock.getTimestamp()) { using (MemoryStream mw = new MemoryStream()) { using (BinaryWriter writer = new BinaryWriter(mw)) { writer.Write(Node.walletStorage.getPrimaryAddress().Length); writer.Write(Node.walletStorage.getPrimaryAddress()); NetworkClientManager.broadcastData(new char[] { 'M' }, ProtocolMessageCode.getBalance, mw.ToArray(), null); } } } }
// Handle timer routines static public void mainLoop() { byte[] primaryAddress = IxianHandler.getWalletStorage().getPrimaryAddress(); if (primaryAddress == null) { return; } byte[] getBalanceBytes; using (MemoryStream mw = new MemoryStream()) { using (BinaryWriter writer = new BinaryWriter(mw)) { writer.WriteIxiVarInt(primaryAddress.Length); writer.Write(primaryAddress); } getBalanceBytes = mw.ToArray(); } while (running) { try { // Update the friendlist FriendList.Update(); // Request initial wallet balance if (balance.blockHeight == 0 || balance.lastUpdate + 300 < Clock.getTimestamp()) { CoreProtocolMessage.broadcastProtocolMessage(new char[] { 'M', 'H' }, ProtocolMessageCode.getBalance2, getBalanceBytes, null); } if (Config.enablePushNotifications) { OfflinePushMessages.fetchPushMessages(); } // Cleanup the presence list // TODO: optimize this by using a different thread perhaps PresenceList.performCleanup(); } catch (Exception e) { Logging.error("Exception occured in mainLoop: " + e); } Thread.Sleep(2500); } }
private bool sendMessage(Friend friend, PendingMessage pending_message, bool add_to_pending_messages = true) { StreamMessage msg = pending_message.streamMessage; bool send_to_server = pending_message.sendToServer; bool send_push_notification = pending_message.sendPushNotification; // TODO this function has to be improved and node's wallet address has to be added if (friend.publicKey != null || (msg.encryptionType != StreamMessageEncryptionCode.rsa && friend.aesKey != null && friend.chachaKey != null)) { if (msg.encryptionType == StreamMessageEncryptionCode.none) { if (friend.aesKey != null && friend.chachaKey != null) { // upgrade encryption type msg.encryptionType = StreamMessageEncryptionCode.spixi1; } else if (!friend.bot) { // upgrade encryption type msg.encryptionType = StreamMessageEncryptionCode.rsa; } } if (msg.encryptionType != StreamMessageEncryptionCode.none) { if (msg.version == 0 && msg.encryptionType == StreamMessageEncryptionCode.rsa && !msg.encrypted) { msg.sign(IxianHandler.getWalletStorage().getPrimaryPrivateKey()); } if (!msg.encrypt(friend.publicKey, friend.aesKey, friend.chachaKey)) { Logging.warn("Could not encrypt message for {0}!", Base58Check.Base58CheckEncoding.EncodePlain(msg.recipient)); return(false); } if (msg.version > 0 && msg.encryptionType == StreamMessageEncryptionCode.rsa) { msg.sign(IxianHandler.getWalletStorage().getPrimaryPrivateKey()); } } } else if (msg.encryptionType != StreamMessageEncryptionCode.none) { if (friend.publicKey == null) { byte[] pub_k = FriendList.findContactPubkey(friend.walletAddress); friend.setPublicKey(pub_k); } if (!friend.bot) { Logging.warn("Could not send message to {0}, due to missing encryption keys!", Base58Check.Base58CheckEncoding.EncodePlain(msg.recipient)); // Return true in case it has other messages in the queue that need to be processed and aren't encrypted return(true); } else { // TODO TODO TODO perhaps it would be better to discard such message and notify the user Logging.warn("Tried sending encrypted message of type {0} without encryption keys to {1}, which is a bot, changing message encryption type to none!", msg.type, Base58Check.Base58CheckEncoding.EncodePlain(msg.recipient)); msg.encryptionType = StreamMessageEncryptionCode.none; } } bool sent = false; string hostname = friend.searchForRelay(); // TODO cuckoo filter should be used instead, need to check the performance when PL is big if (friend.online) { if (hostname != "" && hostname != null) { StreamClientManager.connectTo(hostname, null); // TODO replace null with node address sent = StreamClientManager.sendToClient(hostname, ProtocolMessageCode.s2data, msg.getBytes(), msg.id); if (sent && pending_message.removeAfterSending) { removeMessage(friend, pending_message.streamMessage.id); } } } if (friend.forcePush || !friend.online || !sent) { if (send_to_server) { send_to_server = Config.enablePushNotifications; if (friend.bot) { send_to_server = false; send_push_notification = false; } } if (send_to_server) { if (OfflinePushMessages.sendPushMessage(msg, send_push_notification)) { pending_message.sendToServer = false; if (add_to_pending_messages) { pending_message.save(storagePath); } PendingMessageHeader tmp_msg_header = getPendingMessageHeader(friend, pending_message.streamMessage.id); if (tmp_msg_header != null) { tmp_msg_header.sendToServer = false; } if (pending_message.removeAfterSending) { removeMessage(friend, pending_message.streamMessage.id); } return(true); } } return(false); } return(true); /* string pub_k = FriendList.findContactPubkey(msg.recipientAddress); * if (pub_k.Length < 1) * { * Console.WriteLine("Contact {0} not found, adding to offline queue!", msg.recipientAddress); * addOfflineMessage(msg); * return; * } * * * // Create a new IXIAN transaction * // byte[] checksum = Crypto.sha256(encrypted_message); * Transaction transaction = new Transaction(0, msg.recipientAddress, Node.walletStorage.address); * // transaction.data = Encoding.UTF8.GetString(checksum); * msg.transactionID = transaction.id; * //ProtocolMessage.broadcastProtocolMessage(ProtocolMessageCode.newTransaction, transaction.getBytes()); * * // Add message to the queue * messages.Add(msg); * * // Request a new keypair from the S2 Node * if(hostname == null) * ProtocolMessage.broadcastProtocolMessage(ProtocolMessageCode.s2generateKeys, Encoding.UTF8.GetBytes(msg.getID())); * else * { * NetworkClientManager.sendData(ProtocolMessageCode.s2generateKeys, Encoding.UTF8.GetBytes(msg.getID()), hostname); * }*/ }