comparison src/myVncProxy/MyRfbProto.java @ 86:b7225991184b

refactor for tests
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 03 Aug 2011 08:05:51 +0900
parents b384db76c28a
children a8c33757ac99
comparison
equal deleted inserted replaced
85:b384db76c28a 86:b7225991184b
369 is.mark(dataLen); 369 is.mark(dataLen);
370 } 370 }
371 return dataLen; 371 return dataLen;
372 } 372 }
373 373
374
375 void sendDataToClient() throws Exception {
376 regiFramebufferUpdate();
377 int dataLen = checkAndMark();
378 readSendData(dataLen);
379 }
380
381 BufferedImage createBufferedImage(Image img) {
382 BufferedImage bimg = new BufferedImage(img.getWidth(null),
383 img.getHeight(null), BufferedImage.TYPE_INT_RGB);
384
385 Graphics g = bimg.getGraphics();
386 g.drawImage(img, 0, 0, null);
387 g.dispose();
388 return bimg;
389 }
390
391 void createPngBytes(BufferedImage bimg) throws IOException {
392 pngBytes = getImageBytes(bimg, "png");
393 }
394
395 byte[] getBytes(BufferedImage img) throws IOException {
396 byte[] b = getImageBytes(img, "png");
397 return b;
398 }
399
400 byte[] getImageBytes(BufferedImage image, String imageFormat)
401 throws IOException {
402 ByteArrayOutputStream bos = new ByteArrayOutputStream();
403 BufferedOutputStream os = new BufferedOutputStream(bos);
404 image.flush();
405 ImageIO.write(image, imageFormat, os);
406 os.flush();
407 os.close();
408 return bos.toByteArray();
409 }
410
411 void sendPngData(Socket sock) throws IOException {
412 byte[] dataLength = castIntByte(pngBytes.length);
413 sock.getOutputStream().write(dataLength);
414 sock.getOutputStream().write(pngBytes);
415 }
416
417 byte[] castIntByte(int len) {
418 byte[] b = new byte[4];
419 b[0] = (byte) ((len >>> 24) & 0xFF);
420 b[1] = (byte) ((len >>> 16) & 0xFF);
421 b[2] = (byte) ((len >>> 8) & 0xFF);
422 b[3] = (byte) ((len >>> 0) & 0xFF);
423 return b;
424 }
425
426 BufferedImage createBimg() throws IOException {
427 BufferedImage bimg = ImageIO.read(new ByteArrayInputStream(pngBytes));
428 return bimg;
429 }
430 /*
431 void readPngData() throws IOException {
432 pngBytes = new byte[is.available()];
433 readFully(pngBytes);
434 }
435 */
436 void printFramebufferUpdate() {
437
438 System.out.println("messageType=" + messageType);
439 System.out.println("rectangles=" + rectangles);
440 System.out.println("encoding=" + encoding);
441 System.out.println("rectX = "+rectX+": rectY = "+rectY);
442 System.out.println("rectW = "+rectW+": rectH = "+rectH);
443 switch (encoding) {
444 case RfbProto.EncodingRaw:
445 System.out.println("rectW * rectH * 4 + 16 =" + rectW * rectH * 4
446 + 16);
447 break;
448 default:
449 }
450 }
451
452 void readSpeedCheck() throws IOException {
453 byte[] b = new byte[1];
454 readFully(b);
455 }
456
457 void startSpeedCheck() {
458 byte[] b = new byte[2];
459 b[0] = (byte) SpeedCheckMillis;
460 b[1] = (byte) 0;
461 startCheckTime = System.currentTimeMillis();
462 System.out.println("startChckTime = "+ startCheckTime);
463 LinkedList<byte[]>bufs = new LinkedList<byte[]>();
464 bufs.add(b);
465 multicastqueue.put(bufs);
466 }
467
468 void endSpeedCheck() {
469 long accTime = System.currentTimeMillis();
470 long time = accTime - startCheckTime;
471 System.out.println("checkMillis: " + time);
472 }
473
474 void printStatus() {
475 System.out.println();
476 }
477
478 synchronized void changeStatusFlag() {
479 printStatusFlag = true;
480 }
481
482 void printMills() {
483 if(printStatusFlag) {
484
485 changeStatusFlag();
486 } else {
487 changeStatusFlag();
488 }
489 }
490
491 public int zip(Deflater deflater,LinkedList<byte[]> inputs, byte[] header, LinkedList<byte[]> outputs) throws IOException {
492 int clen = u32(inputs.poll(),0);
493
494 int len = 0, count = 0;
495 int len2=0;
496 //int bufSize = bufs.size();
497 //int bufCount = 0;
498 deflater.reset();
499 do {
500 byte[] b1 = inputs.poll();
501 if (inputs.size()==0) {
502 deflater.setInput(b1,0,clen);
503 deflater.finish();
504 } else
505 deflater.setInput(b1);
506 int len1=0;
507 do {
508 byte[] c1 = new byte[INFLATE_BUFSIZE];
509 len2 = len1;
510 len1 = deflater.deflate(c1);
511 if (len1>0) {
512 outputs.addLast(c1);
513 count ++;
514 len += len1;
515 }
516 } while (len1 > 0);
517 } while(inputs.size()>0);
518 byte[] blen = castIntByte(len);
519 outputs.addFirst(blen);
520 outputs.addFirst(header);
521 return len2;
522
523 }
524
525 public void unzip(Inflater inflater, byte[] input, LinkedList<byte[]> outputs)
526 throws DataFormatException {
527 int len=0,len0;
528 inflater.setInput(input, 20, input.length-20);
529 do {
530 byte buf[] = new byte[INFLATE_BUFSIZE];
531 len0 = inflater.inflate(buf);
532 len += len0;
533 outputs.addLast(buf);
534 } while (len0 ==INFLATE_BUFSIZE);
535 byte [] blen = castIntByte(len0);
536 outputs.addFirst(blen);
537 byte inf[] = new byte[16];
538 for(int i = 0;i<16;i++) inf[i] = input[i];
539 outputs.addFirst(inf);
540 }
541
374 void readSendData(int dataLen) throws IOException, DataFormatException { 542 void readSendData(int dataLen) throws IOException, DataFormatException {
375 byte b[] = new byte[dataLen]; 543 byte b[] = new byte[dataLen];
376 readFully(b); 544 readFully(b);
377 LinkedList<byte[]>bufs = new LinkedList<byte[]>(); 545 LinkedList<byte[]>bufs = new LinkedList<byte[]>();
378 if (b[0]==RfbProto.FramebufferUpdate) { 546 if (b[0]==RfbProto.FramebufferUpdate) {
379 int encoding = u32(b,12); 547 int encoding = u32(b,12);
380 if (encoding==RfbProto.EncodingZlib||encoding==RfbProto.EncodingZRLE) { 548 if (encoding==RfbProto.EncodingZlib||encoding==RfbProto.EncodingZRLE) {
381 int len=0,len0; 549 unzip(inflater, b, bufs);
382 inflater.setInput(b, 20, b.length-20);
383 do {
384 byte buf[] = new byte[INFLATE_BUFSIZE];
385 len0 = inflater.inflate(buf);
386 len += len0;
387 bufs.addLast(buf);
388 } while (len0 ==INFLATE_BUFSIZE);
389 byte [] blen = castIntByte(len0);
390 bufs.addFirst(blen);
391 byte inf[] = new byte[16];
392 for(int i = 0;i<16;i++) inf[i] = b[i];
393 bufs.addFirst(inf);
394 multicastqueue.put(bufs); 550 multicastqueue.put(bufs);
395 is.reset(); 551 is.reset();
396 return ; 552 return ;
397 } 553 }
398 } 554 }
416 } 572 }
417 573
418 } 574 }
419 */ 575 */
420 } 576 }
421 void sendDataToClient() throws Exception {
422 regiFramebufferUpdate();
423 int dataLen = checkAndMark();
424 readSendData(dataLen);
425 }
426
427 BufferedImage createBufferedImage(Image img) {
428 BufferedImage bimg = new BufferedImage(img.getWidth(null),
429 img.getHeight(null), BufferedImage.TYPE_INT_RGB);
430
431 Graphics g = bimg.getGraphics();
432 g.drawImage(img, 0, 0, null);
433 g.dispose();
434 return bimg;
435 }
436
437 void createPngBytes(BufferedImage bimg) throws IOException {
438 pngBytes = getImageBytes(bimg, "png");
439 }
440
441 byte[] getBytes(BufferedImage img) throws IOException {
442 byte[] b = getImageBytes(img, "png");
443 return b;
444 }
445
446 byte[] getImageBytes(BufferedImage image, String imageFormat)
447 throws IOException {
448 ByteArrayOutputStream bos = new ByteArrayOutputStream();
449 BufferedOutputStream os = new BufferedOutputStream(bos);
450 image.flush();
451 ImageIO.write(image, imageFormat, os);
452 os.flush();
453 os.close();
454 return bos.toByteArray();
455 }
456
457 void sendPngData(Socket sock) throws IOException {
458 byte[] dataLength = castIntByte(pngBytes.length);
459 sock.getOutputStream().write(dataLength);
460 sock.getOutputStream().write(pngBytes);
461 }
462
463 byte[] castIntByte(int len) {
464 byte[] b = new byte[4];
465 b[0] = (byte) ((len >>> 24) & 0xFF);
466 b[1] = (byte) ((len >>> 16) & 0xFF);
467 b[2] = (byte) ((len >>> 8) & 0xFF);
468 b[3] = (byte) ((len >>> 0) & 0xFF);
469 return b;
470 }
471
472 BufferedImage createBimg() throws IOException {
473 BufferedImage bimg = ImageIO.read(new ByteArrayInputStream(pngBytes));
474 return bimg;
475 }
476 /*
477 void readPngData() throws IOException {
478 pngBytes = new byte[is.available()];
479 readFully(pngBytes);
480 }
481 */
482 void printFramebufferUpdate() {
483
484 System.out.println("messageType=" + messageType);
485 System.out.println("rectangles=" + rectangles);
486 System.out.println("encoding=" + encoding);
487 System.out.println("rectX = "+rectX+": rectY = "+rectY);
488 System.out.println("rectW = "+rectW+": rectH = "+rectH);
489 switch (encoding) {
490 case RfbProto.EncodingRaw:
491 System.out.println("rectW * rectH * 4 + 16 =" + rectW * rectH * 4
492 + 16);
493 break;
494 default:
495 }
496 }
497
498 void readSpeedCheck() throws IOException {
499 byte[] b = new byte[1];
500 readFully(b);
501 }
502
503 void startSpeedCheck() {
504 byte[] b = new byte[2];
505 b[0] = (byte) SpeedCheckMillis;
506 b[1] = (byte) 0;
507 startCheckTime = System.currentTimeMillis();
508 System.out.println("startChckTime = "+ startCheckTime);
509 LinkedList<byte[]>bufs = new LinkedList<byte[]>();
510 bufs.add(b);
511 multicastqueue.put(bufs);
512 }
513
514 void endSpeedCheck() {
515 long accTime = System.currentTimeMillis();
516 long time = accTime - startCheckTime;
517 System.out.println("checkMillis: " + time);
518 }
519
520 void printStatus() {
521 System.out.println();
522 }
523
524 synchronized void changeStatusFlag() {
525 printStatusFlag = true;
526 }
527
528 void printMills() {
529 if(printStatusFlag) {
530
531 changeStatusFlag();
532 } else {
533 changeStatusFlag();
534 }
535 }
536
537 577
538 void newClient(AcceptThread acceptThread, final Socket newCli, 578 void newClient(AcceptThread acceptThread, final Socket newCli,
539 final OutputStream os, final InputStream is) throws IOException { 579 final OutputStream os, final InputStream is) throws IOException {
540 // createBimgFlag = true; 580 // createBimgFlag = true;
541 // rfb.addSockTmp(newCli); 581 // rfb.addSockTmp(newCli);
561 LinkedList<byte[]> bufs = c.poll(); 601 LinkedList<byte[]> bufs = c.poll();
562 byte[] b = bufs.poll(); 602 byte[] b = bufs.poll();
563 if (b[0]==RfbProto.FramebufferUpdate) { 603 if (b[0]==RfbProto.FramebufferUpdate) {
564 int encoding = u32(b,12); 604 int encoding = u32(b,12);
565 if (encoding==RfbProto.EncodingZlib||encoding==RfbProto.EncodingZRLE) { 605 if (encoding==RfbProto.EncodingZlib||encoding==RfbProto.EncodingZRLE) {
566 int clen = u32(bufs.poll(),0);
567 LinkedList<byte[]> outs = new LinkedList<byte[]>(); 606 LinkedList<byte[]> outs = new LinkedList<byte[]>();
568 int len = 0, count = 0; 607 int len2 = zip(deflater, bufs, b, outs);
569 int len2=0; 608 while(!outs.isEmpty()) {
570 //int bufSize = bufs.size(); 609 byte [] out= outs.poll();
571 //int bufCount = 0; 610 if (outs.isEmpty())
572 deflater.reset(); 611 os.write(out,0,len2);
573 do { 612 else
574 byte[] b1 = bufs.poll();
575 if (bufs.size()==0) {
576 deflater.setInput(b1,0,clen);
577 deflater.finish();
578 } else
579 deflater.setInput(b1);
580 int len1=0;
581 do {
582 byte[] c1 = new byte[INFLATE_BUFSIZE];
583 len2 = len1;
584 len1 = deflater.deflate(c1);
585 if (len1>0) {
586 outs.addLast(c1);
587 count ++;
588 len += len1;
589 }
590 } while (len1 > 0);
591 } while(bufs.size()>0);
592 byte[] blen = castIntByte(len);
593 outs.addFirst(blen);
594 outs.addFirst(b);
595 int i = 0;
596 for(byte [] out: outs) {
597 if (i++ <count+2-1)
598 os.write(out); 613 os.write(out);
599 else
600 os.write(out,0,len2);
601 } 614 }
602 } 615 }
603 } else { 616 } else {
604 os.write(b, 0, b.length); 617 os.write(b, 0, b.length);
605 } 618 }
611 */ 624 */
612 // cliList.remove(newCli); 625 // cliList.remove(newCli);
613 } 626 }
614 627
615 } 628 }
629
616 630
617 631
618 }; 632 };
619 clients++; 633 clients++;
620 new Thread(sender).start(); 634 new Thread(sender).start();