<commit_before>import 'package:webrtc_utils/client.dart';
import 'dart:html';
import 'dart:typed_data';
import 'dart:math';
import 'dart:async';

const Map rtcConfiguration = const {"iceServers": const [ const {"url": "stun:stun.l.google.com:19302"}]};
final String url = 'ws://${window.location.hostname}:28080';
P2PClient client;
Map<String, File> files = {};
void _onPeerJoined(Peer peer) {
  peer.onChannelCreated.listen((RtcDataChannel channel) {
    print('Channel created with label ${channel.label}');
    channel.binaryType = 'arraybuffer';
    
    if(files.containsKey(channel.label)) {
      print('Sender');
      
      channel.onOpen.listen((Event ev) {
        print('Channel is opened');
        final File file = files[channel.label];
        
        var chunkSize = 16384;
        sliceFile(int offset) {
          FileReader reader = new FileReader();
          reader.onLoad.listen((Event ev) {
            channel.send(reader.result);
            if (file.size > offset + (reader.result as TypedData).lengthInBytes) {
              new Future(() => sliceFile(offset + chunkSize));
            }
          });
          Blob slice = file.slice(offset, offset + chunkSize);
          reader.readAsArrayBuffer(slice);
        };
        sliceFile(0);
      });
      
      channel.onMessage.listen((MessageEvent ev) {
        print(ev.data);
      });
    } else {
      print('Receiver');
      List receiveBuffer = [];
      channel.onMessage.listen((MessageEvent ev) {
        if(ev.data is ByteBuffer) {
          if((ev.data as ByteBuffer).lengthInBytes < 16384) {
            AnchorElement save = new AnchorElement();
            save.target = '_blank';
            save.download = channel.label;
            save.href = Url.createObjectUrlFromBlob(new Blob(receiveBuffer, 'application/pdf'));
            document.body.append(save);
            save.click();
            save.remove();
          }
          receiveBuffer.add(ev.data);
        } else {
          print(ev.data);
        }
      });
    }
  });
}

void main() {
  client = new WebSocketP2PClient(url, rtcConfiguration);
  client.onConnected.listen((final int id) {
    client.join('filetransfer');
  });
  
  client.onRoomJoined.listen((final Room room) {
    room.peers.forEach(_onPeerJoined);
    room.onJoin.listen(_onPeerJoined);
    
    document.onDragEnter.listen((MouseEvent ev) {
    });
    
    document.onDragLeave.listen((MouseEvent ev) {
    });
    
    document.onDragOver.listen((MouseEvent ev) {
      ev.preventDefault();
    });
    <commit_msg>Added data transfer time and fixed an ordering bug in the receiver.<commit_after>import 'package:webrtc_utils/client.dart';
import 'dart:html';
import 'dart:typed_data';
// Used for delaying the sending one tick
import 'dart:async';

const Map rtcConfiguration = const {"iceServers": const [ const {"url": "stun:stun.l.google.com:19302"}]};
final String url = 'ws://${window.location.hostname}:28080';
P2PClient client;
Map<String, File> files = {};
void _onPeerJoined(Peer peer) {
  peer.onChannelCreated.listen((final RtcDataChannel channel) {
    print('Channel created with label ${channel.label}');
    channel.binaryType = 'arraybuffer';
    
    if(files.containsKey(channel.label)) {
      print('Sender');
      
      channel.onOpen.listen((Event ev) {
        print('Channel is opened');
        final File file = files[channel.label];
        final int startTime = new DateTime.now().millisecondsSinceEpoch;
        
        print('File to send: ${file.name} ${file.size}');
        
        var chunkSize = 16384;
        sliceFile(final int offset) {
          final FileReader reader = new FileReader();
          reader.onLoadEnd.listen((Event ev) {
            print('bytes in buffer (before): ${channel.bufferedAmount}');
            channel.send(reader.result);
            print('bytes in buffer (after): ${channel.bufferedAmount}');
            if (file.size > offset + (reader.result as TypedData).lengthInBytes) {
              // new Future(() => sliceFile(offset + chunkSize));
              // 0 Delay sending
              sliceFile(offset + chunkSize);
            } else {
              final int time = new DateTime.now().millisecondsSinceEpoch - startTime;
              print('Sending done after ${time}ms');
            }
          });
          Blob slice = file.slice(offset, offset + chunkSize);
          //print('Start reading: $slice');
          reader.readAsArrayBuffer(slice);
        };
        //print('Start slicing');
        sliceFile(0);
      });
      
      channel.onMessage.listen((MessageEvent ev) {
        print(ev.data);
      });
    } else {
      final int startTime = new DateTime.now().millisecondsSinceEpoch;
      
      
      print('Receiver');
      List receiveBuffer = [];
      channel.onMessage.listen((MessageEvent ev) {
        if(ev.data is ByteBuffer) {
          ByteBuffer buffer = ev.data;
          receiveBuffer.add(ev.data);
          
          if(buffer.lengthInBytes < 16384) {
            final int time = new DateTime.now().millisecondsSinceEpoch - startTime;
            print('Receiving done after ${time}ms');
            AnchorElement save = new AnchorElement();
            save.target = '_blank';
            save.download = channel.label;
            save.href = Url.createObjectUrlFromBlob(new Blob(receiveBuffer, 'application/pdf'));
            document.body.append(save);
            save.click();
            save.remove();
          }
        } else {
          print(ev.data);
        }
      });
    }
  });
}

void main() {
  client = new WebSocketP2PClient(url, rtcConfiguration);
  client.onConnected.listen((final int id) {
    client.join('filetransfer');
  });
  
  client.onRoomJoined.listen((final Room room) {
    room.peers.forEach(_onPeerJoined);
    room.onJoin.listen(_onPeerJoined);
    
    document.onDragEnter.listen((MouseEvent ev) {
    });
    
    document.onDragLeave.listen((MouseEvent ev) {
    });
    
    document.onDragOver.listen((MouseEvent ev) {
      ev.preventDefault();
    });
    
{'fraction_non_alphanumeric': 0.13314680710994076, 'fraction_numerical': 0.007735352205398289, 'mean_word_length': 2.37423653525819, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6479156', 'n_tokens_mistral': 1775, 'n_tokens_neox': 1739, 'n_words': 348}