<commit_before>package backupbuddies.network;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.HashMap;

import backupbuddies.network.packet.BackupFile;
import backupbuddies.network.packet.Handshake;
import backupbuddies.network.packet.ListFiles;
import backupbuddies.network.packet.NotifyNewPeer;
import backupbuddies.network.packet.NotifyTransferFailed;
import backupbuddies.network.packet.ReplyRestoreFile;
import backupbuddies.network.packet.RequestListOfFiles;
import backupbuddies.network.packet.RequestRestoreFile;

final class PeerServicer implements Runnable {

	private final Peer peer;
	
	private final DataInputStream inbound;
	
	private HashMap<String, IPacketHandler> packets = new HashMap<>();

	PeerServicer(Peer peer, DataInputStream inbound) {
		this.peer = peer;
		this.inbound = inbound;

		registerPacket(new RequestListOfFiles());
		registerPacket(new ListFiles());
		
		registerPacket(new NotifyNewPeer());
		
		registerPacket(new BackupFile());
		
		registerPacket(new RequestRestoreFile());
		registerPacket(new ReplyRestoreFile());
		
		registerPacket(new NotifyTransferFailed());
	}

	@Override
	public void run() {
		try{
			// If they fail to handshake properly, they're either not Backup
			// Buddies or don't have the password. Don't take their commands.
			try{
				Handshake.checkHandshake(peer, inbound);
			}catch(IllegalArgumentException e){
				e.printStackTrace();
				peer.kill("Bad handshake");
				return;
			}
			
			//Now that they're auth'd, send them our file list
			peer.sendStoredFileList();


			while(!peer.isDead()){
				String command=inbound.readUTF();
				if(command==null){
					peer.kill("Null command");
					return;
				}
				
				IPacketHandler packetHandler = packets.get(command);
				if(packetHandler==null){
					peer.kill("Illegal command: "+command);
					return;
				} else {
					//Acquire a lock on the peer
					//This is often needed - packets may require replies.
					synchronized(peer){
						packetHandler.handlePacket(peer, peer.network, inbound);
					}
				}
				
			}
		}catch(IOException e){
			System.out.println("Connection lost to "+peer.url);
			peer.kill(e);
			return;
		}
	}
	
	private void registerPacket(IPacketHandler packet){
		packets.put(packet.getIdentifier(), packet);
	}

}<commit_msg>Log a message if a connection is lost<commit_after>package backupbuddies.network;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.HashMap;

import backupbuddies.network.packet.BackupFile;
import backupbuddies.network.packet.Handshake;
import backupbuddies.network.packet.ListFiles;
import backupbuddies.network.packet.NotifyNewPeer;
import backupbuddies.network.packet.NotifyTransferFailed;
import backupbuddies.network.packet.ReplyRestoreFile;
import backupbuddies.network.packet.RequestListOfFiles;
import backupbuddies.network.packet.RequestRestoreFile;

final class PeerServicer implements Runnable {

	private final Peer peer;
	
	private final DataInputStream inbound;
	
	private HashMap<String, IPacketHandler> packets = new HashMap<>();

	PeerServicer(Peer peer, DataInputStream inbound) {
		this.peer = peer;
		this.inbound = inbound;

		registerPacket(new RequestListOfFiles());
		registerPacket(new ListFiles());
		
		registerPacket(new NotifyNewPeer());
		
		registerPacket(new BackupFile());
		
		registerPacket(new RequestRestoreFile());
		registerPacket(new ReplyRestoreFile());
		
		registerPacket(new NotifyTransferFailed());
	}

	@Override
	public void run() {
		try{
			// If they fail to handshake properly, they're either not Backup
			// Buddies or don't have the password. Don't take their commands.
			try{
				Handshake.checkHandshake(peer, inbound);
			}catch(IllegalArgumentException e){
				e.printStackTrace();
				peer.kill("Bad handshake");
				return;
			}
			
			//Now that they're auth'd, send them our file list
			peer.sendStoredFileList();


			while(!peer.isDead()){
				String command=inbound.readUTF();
				if(command==null){
					peer.kill("Null command");
					return;
				}
				
				IPacketHandler packetHandler = packets.get(command);
				if(packetHandler==null){
					peer.kill("Illegal command: "+command);
					return;
				} else {
					//Acquire a lock on the peer
					//This is often needed - packets may require replies.
					synchronized(peer){
						packetHandler.handlePacket(peer, peer.network, inbound);
					}
				}
				
			}
		}catch(IOException e){
			peer.network.log("Connection lost to "+peer.url);
			peer.kill(e);
			return;
		}
	}
	
	private void registerPacket(IPacketHandler packet){
		packets.put(packet.getIdentifier(), packet);
	}

}
{'fraction_non_alphanumeric': 0.10294728102947281, 'fraction_numerical': 0.0, 'mean_word_length': 3.4786245353159853, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1674800', 'n_tokens_mistral': 1696, 'n_tokens_neox': 1376, 'n_words': 321}