-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
120 lines (104 loc) · 3.66 KB
/
main.js
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
var fs = require('fs');
var Datastore = require('nedb');
global.parts_left_collection = new Datastore({filename: "parts_left", autoload: true});
process.on("uncaughtException", function(err){
fs.appendFileSync('err.txt', err);
});
function main(window){
var express = require("express");
var app = express();
var http = require('http');
var randomAccessFile = require("random-access-file");
var server = http.createServer(app);
var io = require('socket.io').listen(server);
server.listen(12345);
app.use(express.static(__dirname + '/static'));
app.set("views", __dirname+"/views/");
app.set("view engine", "jade");
app.set("view options", {layout:false});
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.logger());
app.get("/", function(req,res){
res.render("index");
});
global.window = window;
var config = JSON.parse(fs.readFileSync('config.json'));
var hash = config.hash;
var size = config.size;
var filepath = config.filepath;
global.hash = hash;
global.filepath = filepath;
var my_uid = JSON.parse(fs.readFileSync('user.json')).user;
io.sockets.on('connection', function(socket) {
global.socket = socket;
socket.emit("initpeer", my_uid); // create Peer for download/upload
var fileDownloadV4 = require('./fileDownloadV4/downloaderV4.js');
var fileUploadV4 = require('./fileDownloadV4/uploaderV4.js');
// init window object for debugging, remove this when moved into FBT
fileUploadV4.initWindow(window);
fileDownloadV4.initWindow(window);
require('./fileDownloadV4/peerDownloader.js').initWindow(window);
require('./res/res_api.js').initWindow(window);
if (my_uid === 'lizhihua') {
var uploader_uids = 'zuoyao';
var fileInfo = {hash: hash, size: size, file_to_save: filepath};
fileDownloadV4.downloadFile(fileInfo, my_uid, uploader_uids);
// test pause and resume
/*
setTimeout(function(){
fileDowloadV4.pauseFileDownload(hash);
setTimeout(function(){
fileDowloadV4.resumeFileDownload(hash);
}, 5000);
}, 10000);
*/
}
if (my_uid === 'zuoyao') {
//var downloader_uid = 'lizhihua';
//fileUploadV4.initV4Upload(my_uid, downloader_uid, hash, size);
var chatRoomTalker = require('./fileDownloadV4/chatRoom-talker.js');
var uploader = new chatRoomTalker('http://182.92.212.237:8099', my_uid);
uploader.onError = function(error) {
window.console.log(error);
};
uploader.onMessage = function(sUid, message) {
window.console.log(message);
var filehash = message.hash;
var filesize = message.filesize;
var pieceindex = message.pieceindex;
var piecesize = message.piecesize;
var file = randomAccessFile(filepath);
var isLastPiece = (filesize-pieceindex*piecesize < piecesize);
var readsize = isLastPiece ? filesize-pieceindex*piecesize : piecesize;
if(readsize <= 0) {
uploader.send(sUid, {
hash: filehash,
filesize: filesize,
pieceindex: pieceindex,
piecesize: readsize,
piecehash: null,
data: null //EOF
});
return;
}
file.read(pieceindex*piecesize, readsize, function(error, data) {
if(error) {
console.log(error);
return process.exit(1);
}
uploader.send(sUid, {
hash: filehash,
filesize: filesize,
pieceindex: pieceindex,
piecesize: readsize,
piecehash: 11111,
data: data
});
file.close();
});
}
}
});
}
exports.main = main;