diff -a -x CVS -x Root -x Repository -x Tag -x Entries -x Entries.Log -x .svn -x '*.rej' -x '*.orig' -x .cvsignore -N -r -u ./src/daemon/common/commonSwarming.ml ./src/daemon/common/commonSwarming.ml
--- ./src/daemon/common/commonSwarming.ml	2006-04-10 13:34:12.000000000 +0200
+++ ./src/daemon/common/commonSwarming.ml	2006-04-11 15:56:21.000000000 +0200
@@ -129,6 +129,7 @@
     mutable s_strategy : strategy;
 
     mutable s_verified_bitmap : string;
+    mutable s_priorities_bitmap : string;
     mutable s_availability : int array;
     mutable s_nuploading : int array;
 (*    mutable s_last_seen : int array; *)
@@ -235,6 +236,7 @@
     s_range_size = zero;
     s_strategy = AdvancedStrategy;
     s_verified_bitmap = "";
+    s_priorities_bitmap = "";
     s_blocks = [||];
     s_block_pos = [||];
     s_availability = [||];
@@ -298,6 +300,7 @@
           s_strategy = AdvancedStrategy;
 
           s_verified_bitmap = String.make nchunks '0';
+          s_priorities_bitmap = String.make nchunks '1'; (*JAVE init all prios to 1, thus all chunks will be downloaded as usual*)
           s_blocks = Array.create nchunks EmptyBlock ;
           s_block_pos = Array.create nchunks zero;
           s_availability = Array.create nchunks 0;
@@ -551,6 +554,7 @@
 
   s.s_blocks <- Array.create nblocks EmptyBlock;
   s.s_verified_bitmap <- String.make nblocks '0';
+  s.s_priorities_bitmap <- String.make nblocks '1';
   s.s_block_pos <- Array.create nblocks zero;
   s.s_availability <- Array.create nblocks 0;
   s.s_nuploading <- Array.create nblocks 0;
@@ -1878,6 +1882,21 @@
 (*                                                                       *)
 (*************************************************************************)
 
+(*this is used for checking if there are other non-downloaded chunks with higher prio
+  its lame because a) its written imperatively, b) should be more efficient
+*)
+let largestprioandnotdownloaded d p n = 
+  let curprio=p.[n] in  
+  let rv = ref true in 
+  if d.[n] >'1' then rv:=false else
+  for i = 0 to (String.length p - 1)  do     
+(*    print_char  d.[i]; print_char  p.[i]; print_char  curprio; *)
+    if d.[i] < '3' && p.[i] > curprio then  
+      rv:=false    
+  done; 
+  !rv
+
+
 let should_download_block s n =
 (*  lprintf "should_download_block %d\n" n; *)
   let result =
@@ -1897,7 +1916,7 @@
           | _ -> ()
         end;
         s.s_verified_bitmap.[n] < '2'
-    | '0' | '1' -> true
+    | '0' | '1' -> largestprioandnotdownloaded   s.s_verified_bitmap   s.s_priorities_bitmap n (*JAVE priorities*)
     | _ -> false
   in
 (*  if result then
@@ -1951,7 +1970,7 @@
 
 (*************   Try to download the movie index and the first minute to
    allow preview of the file as soon as possible *)
-
+(* JAVE disable downloadfirst code in the context of prio ptch
                 if debug_all then lprintf_nl () "{First}";
 
                 let download_first n b =
@@ -1982,6 +2001,7 @@
                 download_first 0 1;
                 download_first (up.up_ncomplete-1) 1;
                 download_first (up.up_ncomplete-2) 1;
+*)
 
 (************* If the file can be verified, and we don't have a lot of blocks
     yet, try to download the partial ones as soon as possible *)
@@ -3205,7 +3225,7 @@
     | _ ->
         let bitmap = verified_bitmap t in
         for i = 0 to String.length bitmap - 1 do
-          if bitmap.[i] <> '3' then raise Not_found;
+          if bitmap.[i] <> '3' && t.t_s.s_priorities_bitmap.[i] <> '0'  then raise Not_found; (*if priority is 0 for a chunk, ignore it when checking if file is finished*)
         done;
         if file_size file <> downloaded t then
           lprintf_nl () "Downloaded size differs after complete verification";
@@ -3213,3 +3233,25 @@
   with _ -> false
 
 
+(*functions for priority bitmask*)
+
+let file_swarmer f =
+  HS.find swarmers_by_name { dummy_swarmer with s_filename = file_disk_name f }
+
+(** set the priority bitmask for each chunk in a file*)
+let set_swarmer_chunk_priorities f priobitmap = 
+  let s = file_swarmer f in
+    if String.length priobitmap ==  String.length s.s_verified_bitmap then 
+      s.s_priorities_bitmap <- priobitmap
+
+
+(** get the priority bitmask for a file*)
+let get_swarmer_chunk_priorities f = 
+  let s = file_swarmer f in
+    s.s_priorities_bitmap
+
+let get_swarmer_chunk_verified f = 
+  let s = file_swarmer f in
+    s.s_verified_bitmap
+
+(* JAVE maybe export compute_block_num ?*)
diff -a -x CVS -x Root -x Repository -x Tag -x Entries -x Entries.Log -x .svn -x '*.rej' -x '*.orig' -x .cvsignore -N -r -u ./src/daemon/driver/driverCommands.ml ./src/daemon/driver/driverCommands.ml
--- ./src/daemon/driver/driverCommands.ml	2006-04-11 13:57:12.000000000 +0200
+++ ./src/daemon/driver/driverCommands.ml	2006-04-11 20:40:41.000000000 +0200
@@ -2981,6 +2981,35 @@
   register_commands "Driver/Xpert"
     [
 
+    "debug_set_download_priorities", Arg_two (fun arg priostring  o ->
+            let num = int_of_string arg in
+            let file = file_find num in
+            CommonSwarming.set_swarmer_chunk_priorities file priostring;
+              "set prio"
+                                                
+    ), ":\t\t\t\t\tset chunk download priorities for a file. 0=never download, >0=download largest prio first(only 1 works for now)";
+
+    "debug_get_download_priorities", Arg_one (fun arg o ->
+            let num = int_of_string arg in
+            let file = file_find num in
+            let swarmer = CommonSwarming.file_swarmer file in
+            let prio=CommonSwarming.get_swarmer_chunk_priorities file in
+            let downloaded = CommonSwarming.get_swarmer_chunk_verified file in
+              Printf.bprintf o.conn_buf
+                "prio      :%s\ndownloaded:%s\n" prio downloaded;
+              Unix32.myprinttree (file_fd file) 
+                (fun fname start len clen->
+                   Printf.bprintf o.conn_buf "startchunk:%d endchunk:%d\n" (CommonSwarming.compute_block_num swarmer start) (CommonSwarming.compute_block_num swarmer (len++start));
+                   ()
+                )
+                
+                o.conn_buf;(*prints to log!*)
+              "";              
+    ), ":\t\t\t\t\tget file chunk priorities for a file";
+
+
+
+
     "reload_messages", Arg_none (fun o ->
         CommonMessages.load_message_file ();
         "\\<script type=\\\"text/javascript\\\"\\>top.window.location.reload();\\</script\\>"
diff -a -x CVS -x Root -x Repository -x Tag -x Entries -x Entries.Log -x .svn -x '*.rej' -x '*.orig' -x .cvsignore -N -r -u ./src/utils/lib/unix32.ml ./src/utils/lib/unix32.ml
--- ./src/utils/lib/unix32.ml	2006-04-10 13:34:12.000000000 +0200
+++ ./src/utils/lib/unix32.ml	2006-04-11 18:50:49.000000000 +0200
@@ -429,6 +429,17 @@
           find_file
             (if file_pos < pos then tree1 else tree2) file_pos
 
+    let rec myprint_tree indent tree f buf=
+      match tree with
+          Leaf file -> Printf.bprintf buf  "%s: %s (%Ld,%Ld,%Ld) \n"
+            (if file.len = file.current_len then "X" else "-")
+            file.filename file.pos file.len file.current_len;
+            f file.filename file.pos file.len file.current_len
+        | Node (pos, tree1, tree2) ->
+            (*          lprintf "%scut at %Ld\n" indent pos;*)
+            myprint_tree (indent ^ "  ") tree1 f buf;
+            myprint_tree (indent ^ "  ") tree2 f buf
+
     let rec print_tree indent tree =
       match tree with
         Leaf file -> lprintf_nl "%s  - %s (%Ld,%Ld)"
@@ -1635,7 +1646,16 @@
       let shared_end = shared_begin ++ shared_len in
       ()
 *)      
-  end
+end
+
+(*JAVE printtree hack*)
+let myprinttree t f buf=
+  match t.file_kind with
+    | MultiFile t ->  MultiFile.myprint_tree "" t.MultiFile.tree f  buf; ()
+    | _ -> ()
+
+
+
 type t = file
 
 (*
diff -a -x CVS -x Root -x Repository -x Tag -x Entries -x Entries.Log -x .svn -x '*.rej' -x '*.orig' -x .cvsignore -N -r -u ./src/utils/lib/unix32.mli ./src/utils/lib/unix32.mli
--- ./src/utils/lib/unix32.mli	2006-04-10 13:34:12.000000000 +0200
+++ ./src/utils/lib/unix32.mli	2006-04-11 19:12:37.000000000 +0200
@@ -101,3 +101,4 @@
 val percentused : string -> int option
 val percentfree : string -> int option
 val filesystem : string -> string
+val myprinttree : t -> (string -> int64 -> int64 -> int64-> unit)  ->   Buffer.t  -> unit


