Changeset 50

Show
Ignore:
Timestamp:
04/22/06 14:48:01 (2 years ago)
Author:
tueda2
Message:

Basic function : overhaul handler

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/overhaul_handler.js

    r45 r50  
    77//*     process and displaying the web page to the user. 
    88//* 
    9 function overhaul_handler()     { 
     9 
     10function overhaul_handler(clientHostArray, clientPortArray, url , filename, chunks_held, md5_list){ 
    1011        var self = this; 
     12         
     13        // counter for the raw datas 
     14        var i; 
     15         
     16        //raw data(unmodified) 
     17        var receivedDataArray=new Array(); 
     18         
     19        //modified data 
     20        //please WAIT!! 
     21         
     22        var availableList=new Array(); 
     23         
     24        //get double array which client have which chunks 
     25        var info= new overhaul_info(clientHostArray,clientPortArray, url, this); 
     26        info.sendInfoRequests(); 
     27         
     28        //loop until all of the data get 
     29        for(i=0;i<numChunk;i++){ 
     30                if(chunks_held[i]!=null){ 
     31                        receivedDataArray[i]=chunks_held[i]; 
     32                        break; 
     33                } 
     34                else{ 
     35                         
     36                        var host=null; 
     37                        var port=null; 
     38                        var k=0 
     39                         
     40                        while((host==null)||k<2){       //while loop begins 
     41                                //get available host from the bottom 
     42                                var j=availableList.size()-1; 
     43                                for(;j>=0;j--){ 
     44                                        if(availableList[j][i]==1){ 
     45                                                host=clientHostArray[j]; 
     46                                                port=clientPortArray[j]; 
     47                                                break; 
     48                                        } 
     49                                } 
     50                                if(host==null){ 
     51                                        availableList=overhaul_info(clientHostArray,clientPortArray); 
     52                                } 
     53                                k++; 
     54                        }//while loop ends 
     55 
     56                        if((host!=null)||(port!=null)){ 
     57                         
     58                                //Opensockets 
     59                                var transportService =Components.classes["@mozilla.org/network/socket-transport-service;1"] 
     60                                                        .getService(Components.interfaces.nsISocketTransportService); 
     61                                var cliantSocket = transportService.createTransport(null,0,host,port,null); 
     62                                //Open output stream 
     63                                var outstream = cliantSocket.openOutputStream(0,0,0); 
     64                                 
     65                                //filename: the file want to get 
     66                                //numChunk: the number of the chunk data which is wanted 
     67                                var sendingMessage="GET "+filename+" CHUNK "+numChunk+" HTTP\1.0\n\n"; 
     68                                                outstream.write(sendingMessage,sendingMessage.length); 
     69         
     70                                //myHost: my IP address 
     71                                var sendingSecondMessage="HOST "+myHost+"\n\n"; 
     72                                                outstream.write(sendingSecondMessage,sendingSecondMessage.length); 
     73         
     74                                //myPort: my port address 
     75                                var sendingPortMessage="Overhaul-port: "+myPort+"\n\n"; 
     76                                                outstream.write(sendingPortMessage,sendingPortMessage.length); 
     77         
     78                                //theHostList: the host and port list which this client has 
     79                                var sendingHostsPortMessage="Overhaul-hosts: "+theHostList+"\n\n"; 
     80                                                outstream.write(sendingHostsPortMessage,sendingHostsPortMessage.length); 
     81         
     82                                //thisChunkData: the chunk data which want to send 
     83                                var sendingChunkMessage="Overhaul-hosts: "+thisChunkData+"\n\n"; 
     84                                                outstream.write(sendingChunkMessage,sendingChunkMessage.length); 
     85         
     86                                //Open inputStream 
     87                                var stream = cliantSocket.openInputStream(0,0,0); 
     88                                var instream = Components.classes["@mozilla.org/scriptableinputstream;1"] 
     89                                                .createInstance(Components.interfaces.nsIScriptableInputStream); 
     90                                instream.init(stream); 
     91 
     92                                var pump = Components.classes["@mozilla.org/network/input-stream-pump;1"]. 
     93                                                createInstance(Components.interfaces.nsIInputStreamPump); 
     94                                pump.init(stream, -1, -1, 0, 0, true); 
     95                                pump.asyncRead(dataListener,null); 
     96                         
     97                        else{ 
     98                                throw "Error"; 
     99                        } 
     100                } 
     101        } 
     102        //loop ends (here this function already holds entire data in the Array "") 
     103 
     104        var final=""; 
     105        for(int i=0;receivedDataArray.size();i++){ 
     106                final+=receivedDataArray[i]; 
     107        } 
     108        window._content.document.write(final); 
     109 
     110 
     111        //* 
     112        //* This is the listener for accepted connections, and it will handle 
     113        //* the passage of data 
     114        //*  
     115 
     116        var dataListener = {     
     117                onStartRequest: function(request, context)      { 
     118                                }, 
     119                onStopRequest: function(request, context, status)       { 
     120                                }, 
     121                onDataAvailable: function(request, context, inputStream, offset, count) { 
     122                        var sinput = Components.classes["@mozilla.org/scriptableinputstream;1"] 
     123                                        .createInstance(Components.interfaces.nsIScriptableInputStream); 
     124                sinput.init(inputStream); 
     125                receivedDataArray[i]+= sinput.read(count); 
     126                }, 
     127        }; 
     128 
    11129} 
     130 
     131overhaul_handler.prototype={ 
     132                updateRows : function(row, number){ 
     133                        this.availableList[number]=row; 
     134                } 
     135} 
     136