-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample_client.js
More file actions
157 lines (121 loc) · 5.32 KB
/
Copy pathsample_client.js
File metadata and controls
157 lines (121 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const fs = require('fs');
const path = require('path');
let file_path = "C:/Users/anmol/Downloads/WhatsApp Image 2024-05-06 at 9.29.52 PM.jpeg";
// Function to read the file in chunks of 1MB
function read_file(file_path) {
// Read the file in chunks of 0.4 KB
let chunk_size = 900;
// Read the file in binary chunks
let file = fs.createReadStream(file_path, {highWaterMark: chunk_size});
let indexOfChunk = 0;
let fileName = file_path.split("/").pop();
// Read the file in chunks
file.on('data', (chunk) => {
// Convert the chunk to binary
chunk = Buffer.from(chunk).toString('binary');
sendChunk(chunk, fileName, indexOfChunk);
indexOfChunk++;
});
// Handle the end of the file
file.on('end', () => {
console.log("File read successfully");
});
// Handle the error
file.on('error', (err) => {
console.log("Error reading file: ", err);
});
}
function sendChunk(chunk, file_name, indexOfChunk) {
// Send the chunk to the server
console.log("Sending chunk to the server");
reciveChunk(chunk, file_name, indexOfChunk);
}
async function reciveChunk(chunk, file_name, indexOfChunk) {
// Sleep Randomly for 1-5 seconds
let sleepTime = Math.floor(Math.random() * 5) + 1;
await new Promise(resolve => setTimeout(resolve, sleepTime * 1000));
// Receive the chunk on the server
console.log("Receiving chunk with index: ", indexOfChunk);
// Save in file
saveChunk(chunk, file_name, indexOfChunk);
}
function saveChunk(chunk, file_name, indexOfChunk) {
// Save the chunk in the file
console.log("Saving chunk in the file");
let downloadDirectory = "C:/Users/anmol/Desktop/Coding/DogeShare/recievedData";
let file_path = downloadDirectory + "/" + file_name;
if (!fs.existsSync(downloadDirectory)) {
fs.mkdirSync(downloadDirectory);
}
// If the file does not exist, create it
if (!fs.existsSync(file_path)) {
fs.writeFileSync(file_path, '');
}
// Open the file in read-write mode
let fd = fs.openSync(file_path, 'r+');
// Convert the chunk back to a Buffer
let buffer = Buffer.from(chunk, 'binary');
// Calculate the position at which to write the chunk
let chunk_size = 900; // This should be the same as the chunk size in the read_file function
let position = indexOfChunk * chunk_size;
// while file size <= position keep writing
let stats = fs.statSync(file_path);
let fileSize = stats.size;
let extraSize = position - fileSize;
if (extraSize > 0) {
console.log("Extra size: ", extraSize);
let extraBuffer = Buffer.alloc(extraSize);
// Append Extra Buffer to the file
fs.writeSync(fd, extraBuffer, 0, extraBuffer.length, fileSize);
}
// Write the chunk at the correct position in the file
fs.writeSync(fd, buffer, 0, buffer.length, position);
fs.closeSync(fd);
// Truncate the file to remove any extra bytes at the end
truncateFile(file_path);
console.log("Chunk saved successfully");
console.log("Chunk index: ", indexOfChunk);
}
function truncateFile(file_path) {
// Open the file in read-write mode
let fd = fs.openSync(file_path, 'r+');
// Get the current file size
let stats = fs.fstatSync(fd);
let size = stats.size;
// Create a buffer to read one byte at a time
let buffer = Buffer.alloc(1);
// Read the file backwards from the end
for (let i = size - 1; i >= 0; i--) {
fs.readSync(fd, buffer, 0, 1, i);
if (buffer[0] !== 0) {
// This is the position of the last non-null byte
fs.ftruncateSync(fd, i + 1);
break;
}
}
// Close the file
fs.closeSync(fd);
}
read_file(file_path);
//// ISSUE : WHEN ONE THREAD IS PROCESSING FILE, ANOTHER ALSO COMING IN. SO PROCESS FILE EDIT REQUESTS ATOMICALLY IN ORDER THEY COME USING A QUEUE
/// WHEN QUEUE IS FULL [200 MB] THEN SIGNAL SERVER TO STOP SENDING MORE PACKETS OF THE FILE
/// WHEN QUEUE IS FREE THEN SIGNAL SERVER TO START SENDING MORE PACKETS OF THE FILE
// APPROACH - 1 FOR FILE SHARING [PEOPLE ARE ASKING A SPECIFIC SENDER FOR THE FILE] [SIMILAR TO SEND ANYWHERE BUT WITH MULTIPLE USERS]
// CREATE A ROOM
// USERS JOIN THAT ROOM
// NOW THE SENDER [WHO CREATED THE ROOM STARTS SENDING FILE TO ALL USERS IN THE ROOM]
// HE SENDS ALL CHUNKS TO ALL USERS IN THE ROOM
// IF HE RECIEVES STOP SIGNAL FROM ANY USER THEN HE STOPS SENDING FILE TO ALL USERS
// IF HE RECIEVES START SIGNAL FROM ALL USERS THEN HE STARTS SENDING FILE TO ALL USERS AGAIN
// APPROACH 1-B:
// SERVER ONLY SENDS PACKET WHEN USER REQUESTS IT
// USER REQUESTS PACKET FROM SERVER WHEN HE IS READY TO RECIEVE IT [HE HAS SPACE IN HIS QUEUE]
// APPROACH - 2 FOR FILE SHARING - SIMILAR TO TORRENT CLIENT [ASK FILE FROM WHOLE NETWORK]
// SINGLE FILE - FIXED CHUNKS -> MENTIONED IN A TORRENT LIKE FILE [METADATA FILE]
// USER SAYS DOWNLOAD THIS FILE FOR ME TO OUR CLIENT
// CLIENT SEES WHAT CHUNKS ARE ALREADY DOWNLOADED
// REMAINING CHUNKS ARE REQUESTED FROM THE SERVER
// SERVER CHECKS WHAT USERS ARE UPLOADING THIS FILE [PEERS]
// TAKES THOSE CHUNKS FROM THOSE USERS AND SENDS TO THE REQUESTING USER
// SERVER REQUESTS CHUNKS FROM PEERS IN PARALLEL [REQUEST DIFFERENCE CHUNKS FROM DIFFERENT PEERS]
// CLIENT AT A TIME ASKES FOR ONLY SOME CHUNKS SUCH THAT THE DATA CAN EASILY FIT IN QUEUE