incomingCapsule method

Future<Uint8List> incomingCapsule(
  1. Uint8List cap
)

Implementation

Future<Uint8List> incomingCapsule(Uint8List cap) async {
  if (_icpo) return Uint8List(0);

  //reply with xx I have. thats it.
  if (cap.lengthInBytes < 20) return outgoingCapsule;

  //inject entries and save, return list of new entries to you
  //also maps cid xxhash on diks
  //pass true so we strip moderate flag
  final _mismes = updateLatestCapsule(cap, true);

  //fullfill wants
  final passedCapsule = ModeratorCapsule.fromBinary(cap);
  if (passedCapsule.isEmpty) return Uint8List(0);
  _icpo = true;
  Uint8List satWanted = Uint8List(80000000);
  var b0 = new ByteData.view(satWanted.buffer);

  int sawa = 0;
  if (passedCapsule.xxWants.isNotEmpty) {
    List<int> wl = [];

    var mb = new ByteData.view(passedCapsule.xxWants.buffer);
    var wb = 0;
    while (wb < passedCapsule.xxWants.lengthInBytes) {
      wl.add(mb.getInt64(wb));
      wb += 8;
    }
/*
    for (var xxInt in wl) {
      final xs = await _xxCache.pullLocalXX(xxInt);
      if (xs != null) {
        if ((sawa + xs.lengthInBytes) < b0.lengthInBytes) {
          b0.setUint64(sawa, xs.lengthInBytes);
          sawa += 8;
          b0.setUint64(sawa, xxInt);
          sawa += 8;
          for (var xb in xs) {
            b0.setUint8(sawa++, xb);
          }
        }
      }
    } */
  }
  //inject new to me
  Set<int> _newt = {};
  Set<int> _receivedThreads = {};
  List<int> myxxwants = [];
/*
  if (passedCapsule.threadEntries.isNotEmpty) {
    final md =
        ModeratorEntrySet.fromModeratorCapsule(passedCapsule.threadEntries);

    for (var i in md.all) {
      _receivedThreads.add(i.keyHint);

      if (data.add(i)) {
        if (i.flags.attachements &&
            !_xxCache.isXXstored(i.attachmentLinkAsXXint)) {
          myxxwants.add(i.attachmentLinkAsXXint);
        }
        _newt.add(i.keyHint);
      }
    }
  }
*/
  //messages they might be missing
  //final List<ModeratorEntry> _mismes =
  //    data.all.where((element) => !_newt.contains(element.keyHint)).toList();

  List<ModeratorCapsuleItem> _mcl = [];
  var keepBel64kb = 0;
  List<int> satXXs = [];
  /*
  for (ModeratorEntry ee in _mismes) {
    if (keepBel64kb > 65000) break;
    if (ee.flags.attachements) {
      if (!_xxCache.isXXstored(ee.attachmentLinkAsXXint)) {
        myxxwants.add(ee.attachmentLinkAsXXint);
      }
      /*if (sawa == 0) {
        final xs =
            await _xxCache.pullIPFScid(ee.ipfsCid, ee.attachmentLinkAsXXint);
        if (xs != null) {
          b0.setUint64(sawa, xs.lengthInBytes);
          sawa += 8;
          b0.setUint64(sawa, ee.attachmentLinkAsXXint);
          sawa += 8;
          for (var xb in xs) {
            b0.setUint8(sawa++, xb);
          }
        }
      }*/
    }
    _mcl.add(ModeratorCapsuleItem(
        ee.flags.binary,
        0,
        ee.bl,
        ee.pp,
        ee.timeElapsedSincePostMs,
        ee.body.length,
        ee.bodyBytes,
        ee.shortLinkBytes.lengthInBytes,
        ee.shortLinkBytes,
        ee.keyHint,
        ee.avatar,
        ee.nick,
        ee.attachmentLink,
        0,
        ee.perceivedTimestamp,
        ee.likes,
        ee.shares,
        ee.views,
        ee.replycount));
    keepBel64kb += _mcl.last.binary.lengthInBytes;
  }
  */
  //only 64 kb a pop
  Uint8List enl = Uint8List(68000);
  var wic = 0;
  for (var element in _mcl) {
    //do not send stuff in recent circulation
    if (_latelySeenp4p.contains(element.hint)) continue;
    _latelySeenp4p.add(element.hint);

    if (wic > 64000) {
      break;
    }
    for (var ib in element.binary) {
      enl[wic++] = ib;
    }
  }
  //get the xxhashes I'm missing

  /*if (sawa == 0 && wic == 0 && myxxwants.isEmpty) {
    //no xxs to satisfy request, no new messages
    _icpo = false;
    return Uint8List(0);
  }*/

  Uint8List _xxWantBin = Uint8List(8089);
  var bo = new ByteData.view(_xxWantBin.buffer);
  myxxwants.shuffle();
  int xw = 0;
  for (var id in myxxwants) {
    if (_latelySeenp4p.contains(id)) continue;
    if (xw == _xxWantBin.lengthInBytes) break;
    bo.setUint64(xw, id);
    xw += 8;
  }

  //TODO something missing for sender?

  final ccap = ModeratorCapsule(Uint8List(0), enl.sublist(0, wic),
      Uint8List(0), _xxWantBin.sublist(0, xw), satWanted.sublist(0, sawa));
  final zut = ccap.binary;
  final md = ModeratorCapsule.fromBinary(zut);
  _icpo = false;
  return zut;
}