| 44 | | |
|---|
| 45 | | while((host == null) || thrdDimChunk < 2) { |
|---|
| 46 | | //get available host from the bottom |
|---|
| 47 | | var sndDimChunk=availableList.size()-1; |
|---|
| 48 | | for(;sndDimChunk>=0;sndDimChunk--){ |
|---|
| 49 | | if(availableList[sndDimChunk][countChunk]==1){ |
|---|
| 50 | | host=clientHostArray[sndDimChunk]; |
|---|
| 51 | | port=clientPortArray[sndDimChunk]; |
|---|
| 52 | | break; |
|---|
| 53 | | } |
|---|
| 54 | | } |
|---|
| 55 | | if(host==null){ |
|---|
| 56 | | availableList = overhaul_info(clientHostArray, clientPortArray); |
|---|
| 57 | | } |
|---|
| 58 | | thrdDimChunk++; |
|---|
| 59 | | } |
|---|
| | 43 | var resultReceiver = overhaul_availablehost(); |
|---|
| | 44 | host = resultReceiver[0]; |
|---|
| | 45 | port = resultReceiver[1]; |
|---|
| 61 | | if((host!=null)||(port!=null)){ |
|---|
| 62 | | |
|---|
| 63 | | //Opensockets |
|---|
| 64 | | var transportService =Components.classes["@mozilla.org/network/socket-transport-service;1"] |
|---|
| 65 | | .getService(Components.interfaces.nsISocketTransportService); |
|---|
| 66 | | var cliantSocket = transportService.createTransport(null,0,host,port,null); |
|---|
| 67 | | //Open output stream |
|---|
| 68 | | var outstream = cliantSocket.openOutputStream(0,0,0); |
|---|
| 69 | | |
|---|
| 70 | | //filename: the file want to get |
|---|
| 71 | | //numChunk: the number of the chunk data which is wanted |
|---|
| 72 | | var sendingMessage="GET "+filename+" CHUNK "+numChunk+" HTTP\1.0\n\n"; |
|---|
| 73 | | outstream.write(sendingMessage,sendingMessage.length); |
|---|
| 74 | | |
|---|
| 75 | | //myHost: my IP address |
|---|
| 76 | | var sendingSecondMessage="HOST "+myHost+"\n\n"; |
|---|
| 77 | | outstream.write(sendingSecondMessage,sendingSecondMessage.length); |
|---|
| 78 | | |
|---|
| 79 | | //myPort: my port address |
|---|
| 80 | | var sendingPortMessage="Overhaul-port: "+myPort+"\n\n"; |
|---|
| 81 | | outstream.write(sendingPortMessage,sendingPortMessage.length); |
|---|
| 82 | | |
|---|
| 83 | | //theHostList: the host and port list which this client has |
|---|
| 84 | | var sendingHostsPortMessage="Overhaul-hosts: "+theHostList+"\n\n"; |
|---|
| 85 | | outstream.write(sendingHostsPortMessage,sendingHostsPortMessage.length); |
|---|
| 86 | | |
|---|
| 87 | | //thisChunkData: the chunk data which want to send |
|---|
| 88 | | var sendingChunkMessage="Overhaul-hosts: "+thisChunkData+"\n\n"; |
|---|
| 89 | | outstream.write(sendingChunkMessage,sendingChunkMessage.length); |
|---|
| 90 | | |
|---|
| 91 | | outstream.close(); |
|---|
| 92 | | |
|---|
| 93 | | //Open inputStream |
|---|
| 94 | | var stream = cliantSocket.openInputStream(0,0,0); |
|---|
| 95 | | create_scriptable_input_stream(stream); |
|---|
| 96 | | bind_stream_to_listener(dataListener, stream); |
|---|
| | 47 | if((host!=null)||(port!=null)) |
|---|
| | 48 | { |
|---|
| | 49 | overhaul_givemefile(host, port,filename,myHost, myPort, clientHostArray, receivedDataArray); |
|---|
| 112 | | |
|---|
| 113 | | |
|---|
| 114 | | //* |
|---|
| 115 | | //* This is the listener for accepted connections, and it will handle |
|---|
| 116 | | //* the passage of data |
|---|
| 117 | | //* |
|---|
| 118 | | |
|---|
| 119 | | var dataListener = { |
|---|
| 120 | | onStartRequest: function(request, context) { |
|---|
| 121 | | }, |
|---|
| 122 | | onStopRequest: function(request, context, status) { |
|---|
| 123 | | }, |
|---|
| 124 | | onDataAvailable: function(request, context, inputStream, offset, count) { |
|---|
| 125 | | var sinput = Components.classes["@mozilla.org/scriptableinputstream;1"] |
|---|
| 126 | | .createInstance(Components.interfaces.nsIScriptableInputStream); |
|---|
| 127 | | sinput.init(inputStream); |
|---|
| 128 | | receivedDataArray[countChunk]+= sinput.read(count); |
|---|
| 129 | | } |
|---|
| 130 | | }; |
|---|