diff --git a/peer_temp/abcd.txt b/peer_temp/abcd.txt new file mode 100644 index 0000000..5eaf5f5 --- /dev/null +++ b/peer_temp/abcd.txt @@ -0,0 +1 @@ +Hello Hi Test \ No newline at end of file diff --git a/peer_temp/client_server.js b/peer_temp/client_server.js new file mode 100644 index 0000000..8248b2f --- /dev/null +++ b/peer_temp/client_server.js @@ -0,0 +1,45 @@ + +const SimplePeer = require('simple-peer'); + +// Function to act as the file host +function hostFile() { + const peer = new SimplePeer({ initiator: true }); + + peer.on('signal', data => { + console.log('Share this data with the peer who wants to download the file:'); + console.log(JSON.stringify(data)); + }); + + peer.on('connect', () => { + console.log('Peer connected. Sending file...'); + const fileContent = 'This is the content of the file.'; + peer.send(fileContent); + }); +} + +// Function to act as the file requester +function requestFile() { + const peer = new SimplePeer(); + + peer.on('signal', data => { + console.log('Share this data with the file host:'); + console.log(JSON.stringify(data)); + }); + + peer.on('connect', () => { + console.log('Peer connected. Waiting for file...'); + }); + + peer.on('data', data => { + console.log(`File received: ${data}`); + }); +} + +// Decide whether to host the file or request it +const isHost = process.argv[2] === 'host'; + +if (isHost) { + hostFile(); +} else { + requestFile(); +} diff --git a/peer_temp/temp_requester.js b/peer_temp/temp_requester.js new file mode 100644 index 0000000..f522ae1 --- /dev/null +++ b/peer_temp/temp_requester.js @@ -0,0 +1,84 @@ +// // Requester Peer + +// const net = require('net'); + +// // IP address and port of the other peer +// const otherPeerIP = 'other_peer_ip_address'; +// const otherPeerPort = 12345; // Assuming this is the port the other peer is listening on + +// // File ID to request +// const fileId = 'file123'; + +// // Create a TCP socket to connect to the other peer +// const client = new net.Socket(); + +// client.connect(otherPeerPort, otherPeerIP, () => { +// console.log('Connected to peer'); + +// // Send the file ID as a request +// client.write(fileId); +// }); + +// // Listen for data (the requested file) from the other peer +// client.on('data', (data) => { +// console.log('Received file:', data.toString()); + +// // Close the connection after receiving the file +// client.end(); +// }); + +// // Handle connection errors +// client.on('error', (err) => { +// console.error('Connection error:', err); +// }); + + +// Requester Peer + +// const net = require('net'); + +// // IP address and port of the other peer +// const otherPeerIP = '127.0.0.1'; // Assuming both peers are running on the same machine +// const otherPeerPort = 12345; + +// File ID to request +const fileId = 'abcd.txt'; + +// Create a TCP server to handle incoming requests from the other peer + + +// Requester Peer + +const net = require('net'); + +// IP address and port of the receiving peer +const receiverIP = '127.0.0.1'; // Assuming both peers are running on the same machine +const receiverPort = 12345; + +// File ID to request +// const fileId = 'file123'; + +// Create a TCP client to connect to the receiving peer server +const client = new net.Socket(); + +// Handle connection to the receiving peer +client.connect(receiverPort, receiverIP, () => { + console.log('Connected to receiving peer server'); + + // Send the file ID as a request to the receiving peer + client.write(fileId); +}); + +// Listen for data (the requested file) from the receiving peer +client.on('data', (data) => { + console.log('Received file:', data.toString()); + + // Close the connection after receiving the file + client.end(); +}); + +// Handle connection errors +client.on('error', (err) => { + console.error('Connection error:', err); +}); + diff --git a/peer_temp/temp_sender.js b/peer_temp/temp_sender.js new file mode 100644 index 0000000..7e732b1 --- /dev/null +++ b/peer_temp/temp_sender.js @@ -0,0 +1,121 @@ +// // Receiving Peer + +// const net = require('net'); +// const fs = require('fs'); + +// // Port on which this peer will listen for requests +// const listenPort = 12345; + +// // Mapping of file IDs to file paths +// const filesMap = { +// 'file123': '/path/to/file.txt', +// // Add more file IDs and corresponding file paths as needed +// }; + +// // Create a server to listen for incoming requests +// const server = net.createServer((socket) => { +// console.log('Peer connected'); + +// // Handle data (file ID) received from the requester peer +// socket.on('data', (data) => { +// const fileId = data.toString(); +// console.log('Received request for file ID:', fileId); + +// // Look up the file path based on the file ID +// const filePath = filesMap[fileId]; + +// if (filePath) { +// // Read the file and send its contents back to the requester +// fs.readFile(filePath, (err, fileData) => { +// if (err) { +// console.error('Error reading file:', err); +// socket.end(); +// return; +// } + +// // Send the file data to the requester +// socket.write(fileData); + +// // Close the connection after sending the file +// socket.end(); +// }); +// } else { +// console.log('File ID not found'); +// socket.end(); +// } +// }); +// }); + +// // Start listening for incoming connections +// server.listen(listenPort, () => { +// console.log('Peer server listening on port', listenPort); +// }); + +// // Handle server errors +// server.on('error', (err) => { +// console.error('Server error:', err); +// }); + + +// Receiving Peer + +const net = require('net'); +const fs = require('fs'); + +// Port on which this peer will listen for requests +const listenPort = 12345; + +// Mapping of file IDs to file paths +const filesMap = { + 'abcd.txt': './abcd.txt', + // Add more file IDs and corresponding file paths as needed +}; + +// Create a TCP server to listen for incoming requests from other peers +const server = net.createServer((socket) => { + console.log('Receiving peer server connected'); + + // Handle data (file ID) received from the requester peer + socket.on('data', (data) => { + const fileId = data.toString(); + console.log('Received request for file ID:', fileId); + + // Look up the file path based on the file ID + const filePath = filesMap[fileId]; + + if (filePath) { + // Read the file and send its contents back to the requester + fs.readFile(filePath, (err, fileData) => { + if (err) { + console.error('Error reading file:', err); + socket.end(); + return; + } + + // Send the file data to the requester + socket.write(fileData); + + // Close the connection after sending the file + socket.end(); + }); + } else { + console.log('File ID not found'); + socket.end(); + } + }); + + // Handle connection errors + socket.on('error', (err) => { + console.error('Connection error:', err); + }); +}); + +// Start the server +server.listen(listenPort, () => { + console.log('Receiving peer server listening for incoming connections'); +}); + +// Handle server errors +server.on('error', (err) => { + console.error('Server error:', err); +});