<commit_before>(*
 * Copyright (C) 2006-2009 Citrix Systems Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; version 2.1 only. with the special
 * exception on linking described in file LICENSE.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *)
open Rfb

let w = 640
let h = 480

module Server = Rfb.Make(Rfb_unix)
open Server

let server (s: Unix.file_descr) = 
  Server.handshake w h s;

  let started = ref false in
  let bpp = ref 32 in

  while true do
    let req = Request.unmarshal s in
    print_endline (Request.prettyprint req);
    match req with
    | Request.SetPixelFormat pf ->
	bpp := pf.PixelFormat.bpp;
    | Request.FrameBufferUpdateRequest _ ->
	if not(!started) then begin
	    (* Update the whole thing *)
	    let buffer = String.create (w * h * !bpp / 8) in
	    for i = 0 to String.length buffer - 1 do
	      buffer.[i] <- char_of_int (Random.int 255)
	    done;
	    let raw = { FramebufferUpdate.Raw.buffer = buffer } in
	    let update = { FramebufferUpdate.x = 0; y = 0; w = w; h = h;
			   encoding = FramebufferUpdate.Encoding.Raw raw } in
	    Rfb_unix.really_write s (FramebufferUpdate.marshal [ update ]);
	    started := true;
	end else begin
	    (* send a copyrect *)
	    let w' = Random.int w and h' = Random.int h in
	    let x' = Random.int (w - w') and y' = Random.int (h - h') in
	    let x'' = Random.int (w - w') and y'' = Random.int (h - h') in
	    let cr = { FramebufferUpdate.CopyRect.x = x''; y = y'' } in
	    let update = { FramebufferUpdate.x = x'; y = y'; w = w'; h = h';
			   encoding = FramebufferUpdate.Encoding.CopyRect cr } in
	    Rfb_unix.really_write s (FramebufferUpdate.marshal [ update ])
	  end
    | _ -> ()
  done
  

<commit_msg>Incremental commit: fix random example<commit_after>(*
 * Copyright (C) 2006-2009 Citrix Systems Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; version 2.1 only. with the special
 * exception on linking described in file LICENSE.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *)
open Rfb

let w = 640
let h = 480

module Server = Rfb.Make(Rfb_unix)
open Server

let send_random_updates bpp s =
  (* Update the whole thing *)
  let buffer = String.create (w * h * bpp / 8) in
  for i = 0 to String.length buffer - 1 do
    buffer.[i] <- char_of_int (Random.int 255)
  done;
  let raw = { FramebufferUpdate.Raw.buffer = buffer } in
  let update = { FramebufferUpdate.x = 0; y = 0; w = w; h = h;
                 encoding = FramebufferUpdate.Encoding.Raw raw } in
  print_endline ("-> " ^ (FramebufferUpdate.prettyprint update));
  Rfb_unix.really_write s (FramebufferUpdate.marshal [ update ]);
  while true do
(* Thread.delay 0.1; *)
    (* send a copyrect *)
    let w' = Random.int (w - 1) + 1 and h' = Random.int (h - 1) + 1 in
    let x' = Random.int (w - w') and y' = Random.int (h - h') in
    let x'' = Random.int (w - w') and y'' = Random.int (h - h') in
    let cr = { FramebufferUpdate.CopyRect.x = x'' ; y = y'' } in
    let update = { FramebufferUpdate.x = x'; y = y'; w = w'; h = h';
                   encoding = FramebufferUpdate.Encoding.CopyRect cr } in
    print_endline ("-> " ^ (FramebufferUpdate.prettyprint update));
    Rfb_unix.really_write s (FramebufferUpdate.marshal [ update ])
  done

let server (s: Unix.file_descr) = 
  Server.handshake w h s;

  let bpp = ref 32 in
  let update_thread = ref None in

  while true do
    let req = Request.unmarshal s in
    print_endline ("<- " ^ (Request.prettyprint req));
    match req with
    | Request.SetPixelFormat pf ->
	bpp := pf.PixelFormat.bpp;
    | Request.FrameBufferUpdateRequest _ ->
        if !update_thread = None
        then update_thread := Some (Thread.create (send_random_updates !bpp) s)
    | _ ->
	print_endline "<- ^^ ignoring";
  done
  


{'fraction_non_alphanumeric': 0.10486048604860486, 'fraction_numerical': 0.013051305130513051, 'mean_word_length': 2.8451557093425603, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3365248', 'n_tokens_mistral': 1485, 'n_tokens_neox': 1377, 'n_words': 538}