Project

General

Profile

« Previous | Next » 

Revision ffe7e55d

Added by Leszek Koltunski over 2 years ago

Reorganize the Network so that concurrent network operattions are possible.
Finally figure out and fix the bug which was causing users to multi-submit the same records over and over.

View differences:

src/main/java/org/distorted/dialogs/RubikDialogUpdates.java
177 177
          View pane = rubikView.createView(act,info,mFontSize,mPadding,pI,pV,pT,pB);
178 178
          mLayout.addView(pane);
179 179
          }
180

  
181
        RubikNetwork network = RubikNetwork.getInstance();
182
        network.downloadIcons(this);
180 183
        }
181 184
      else
182 185
        {
......
211 214
    mText.setText("Network error");
212 215
    }
213 216

  
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218

  
219
  public void iconDownloaded()
220
    {
221

  
222
    }
223

  
214 224
///////////////////////////////////////////////////////////////////////////////////////////////////
215 225

  
216 226
  public static String getDialogTag()
src/main/java/org/distorted/main/RubikObjectLibInterface.java
123 123
        long elapsed = timeNow - startTime;
124 124
        String suspicious ="start"+startTime+"end"+endTime+"elapsed"+elapsed+"obj"+objName+"level"+level+"record"+mNewRecord+"scrambles"+scrambleNum+debug;
125 125
        RubikNetwork network = RubikNetwork.getInstance();
126
        network.suspicious(act,suspicious);
126
        network.suspicious(suspicious);
127 127
        }
128 128

  
129 129
      FirebaseAnalytics analytics = act.getAnalytics();
src/main/java/org/distorted/main/RubikRenderer.java
140 140
        {
141 141
        mDebugSent= true;
142 142
        RubikNetwork network = RubikNetwork.getInstance();
143
        network.debug( (RubikActivity)mView.getContext());
143
        network.debug();
144 144
        }
145 145
      }
146 146

  
src/main/java/org/distorted/network/RubikNetwork.java
28 28
import java.security.MessageDigest;
29 29
import java.security.NoSuchAlgorithmException;
30 30

  
31
import android.app.Activity;
32 31
import android.content.pm.PackageInfo;
33 32
import android.content.pm.PackageManager;
34 33

  
35
import androidx.appcompat.app.AppCompatActivity;
36 34
import androidx.fragment.app.FragmentActivity;
37 35

  
38 36
import org.distorted.library.main.DistortedLibrary;
......
43 41

  
44 42
///////////////////////////////////////////////////////////////////////////////////////////////////
45 43

  
46
public class RubikNetwork implements Runnable
44
public class RubikNetwork
47 45
  {
48 46
  public interface Receiver
49 47
    {
......
54 52

  
55 53
  public interface Updatee
56 54
    {
55
    void iconDownloaded();
57 56
    void receiveUpdate(RubikUpdates update);
58 57
    void errorUpdate();
59 58
    }
60 59

  
61 60
  public static final int MAX_PLACES = 10;
62 61

  
63
  private static final int DOWNLOAD   = 0;
64
  private static final int SUBMIT     = 1;
65
  private static final int DEBUG      = 2;
66
  private static final int SUSPICIOUS = 3;
67
  private static final int IDLE       = 4;
68

  
69 62
  private static final int REND_ADRENO= 0;
70 63
  private static final int REND_MALI  = 1;
71 64
  private static final int REND_POWER = 2;
72 65
  private static final int REND_OTHER = 3;
73 66

  
74
  private static final int DEBUG_NOT_YET = 0;
75 67
  private static final int DEBUG_RUNNING = 1;
76 68
  private static final int DEBUG_SUCCESS = 2;
77 69
  private static final int DEBUG_FAILURE = 3;
......
119 111
  private static RubikNetwork mThis;
120 112
  private static String mScores = "";
121 113
  private static boolean mRunning = false;
122
  private static int mMode = IDLE;
123 114
  private static Receiver mReceiver;
124 115
  private static Updatee mUpdatee;
125 116
  private static String mVersion;
......
380 371
    return sbuf.toString();
381 372
    }
382 373

  
383
///////////////////////////////////////////////////////////////////////////////////////////////////
384

  
385
  private void sendDebug()
386
    {
387
    String url = constructDebugURL();
388
/*
389
    try { Thread.sleep(5000); }
390
    catch( InterruptedException ignored) {}
391
*/
392
    try
393
      {
394
      java.net.URL connectURL = new URL(url);
395
      HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
396

  
397
      conn.setDoInput(true);
398
      conn.setDoOutput(true);
399
      conn.setUseCaches(false);
400
      conn.setRequestMethod("GET");
401
      conn.connect();
402
      conn.getOutputStream().flush();
403

  
404
      InputStream is = conn.getInputStream();
405
      BufferedReader r = new BufferedReader(new InputStreamReader(is));
406
      StringBuilder answer = new StringBuilder();
407

  
408
      for (String line; (line = r.readLine()) != null; )
409
        {
410
        answer.append(line).append('\n');
411
        }
412

  
413
      String updates = answer.toString();
414
      mUpdates.parse(updates);
415
      if( mUpdatee!=null ) mUpdatee.receiveUpdate(mUpdates);
416
      mDebugState = DEBUG_SUCCESS;
417
      }
418
    catch( final Exception e )
419
      {
420
      if( mUpdatee!=null ) mUpdatee.errorUpdate();
421
      mDebugState = DEBUG_FAILURE;
422
      }
423
    }
424

  
425
///////////////////////////////////////////////////////////////////////////////////////////////////
426

  
427
  private void sendSuspicious()
428
    {
429
    String url = constructSuspiciousURL();
430

  
431
    try
432
      {
433
      java.net.URL connectURL = new URL(url);
434
      HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
435

  
436
      conn.setDoInput(true);
437
      conn.setDoOutput(true);
438
      conn.setUseCaches(false);
439
      conn.setRequestMethod("GET");
440
      conn.connect();
441
      conn.getOutputStream().flush();
442
      conn.getInputStream();
443
      }
444
    catch( final Exception e )
445
      {
446
      // ignore
447
      }
448
    }
449

  
450 374
///////////////////////////////////////////////////////////////////////////////////////////////////
451 375

  
452 376
  private boolean network(String url)
......
592 516

  
593 517
///////////////////////////////////////////////////////////////////////////////////////////////////
594 518

  
595
  @Override
596
  public void run()
519
  private void figureOutVersion(FragmentActivity act)
597 520
    {
598
    boolean receiveValues=true;
521
    try
522
      {
523
      PackageInfo pInfo = act.getPackageManager().getPackageInfo( act.getPackageName(), 0);
524
      mVersion = pInfo.versionName;
525
      }
526
    catch (PackageManager.NameNotFoundException e)
527
      {
528
      mVersion = "0.9.2";
529
      }
530
    }
599 531

  
600
    initializeStatics();
532
///////////////////////////////////////////////////////////////////////////////////////////////////
601 533

  
534
  private void downloadThread()
535
    {
602 536
    try
603 537
      {
604
      if( mMode==DOWNLOAD && gottaDownload() )
605
        {
606
        mRunning = true;
607
        receiveValues = network(constructDownloadURL());
608
        }
609
      if( mMode==SUBMIT )
538
      if( gottaDownload() )
610 539
        {
611 540
        mRunning = true;
612
        RubikScores scores = RubikScores.getInstance();
541
        boolean receiveValues = network(constructDownloadURL());
613 542

  
614
        if( scores.thereAreUnsubmittedRecords() )
543
        if( mRunning )
615 544
          {
616
          receiveValues = network(constructSubmitURL());
545
          receiveValues = fillValuesNormal();
546
          mRunning = false;
617 547
          }
618
        }
619
      if( mMode==DEBUG )
620
        {
621
        sendDebug();
622
        receiveValues = false;
623
        mRunning = false;
624
        }
625
      if( mMode==SUSPICIOUS )
626
        {
627
        sendSuspicious();
628
        receiveValues = false;
629
        mRunning = false;
548

  
549
        if( receiveValues && mReceiver!=null ) mReceiver.receive(mCountry, mName, mTime);
630 550
        }
631 551
      }
632 552
    catch( Exception e )
633 553
      {
634
      android.util.Log.e("D", "Exception in RUN");
635

  
636 554
      if( mReceiver!=null ) mReceiver.message("Exception downloading records: "+e.getMessage() );
637 555
      }
556
    }
557

  
558
///////////////////////////////////////////////////////////////////////////////////////////////////
638 559

  
639
    if( mRunning )
560
  private void submitThread()
561
    {
562
    try
563
      {
564
      mRunning = true;
565
      RubikScores scores = RubikScores.getInstance();
566

  
567
      if( scores.thereAreUnsubmittedRecords() )
568
        {
569
        boolean receiveValues = network(constructSubmitURL());
570

  
571
        if( mRunning )
572
          {
573
          receiveValues = fillValuesNormal();
574
          mRunning = false;
575
          }
576

  
577
        if( receiveValues )
578
          {
579
          RubikScores.getInstance().successfulSubmit();
580
          if( mReceiver!=null ) mReceiver.receive(mCountry, mName, mTime);
581
          }
582
        }
583
      }
584
    catch( Exception e )
640 585
      {
641
      receiveValues = fillValuesNormal();
642
      mRunning = false;
586
      if( mReceiver!=null ) mReceiver.message("Exception submitting records: "+e.getMessage() );
643 587
      }
588
    }
644 589

  
645
    if( receiveValues )
590
///////////////////////////////////////////////////////////////////////////////////////////////////
591

  
592
  private void debugThread()
593
    {
594
    String url = constructDebugURL();
595
/*
596
    try { Thread.sleep(5000); }
597
    catch( InterruptedException ignored) {}
598
*/
599
    try
646 600
      {
647
      if( mReceiver!=null ) mReceiver.receive(mCountry, mName, mTime);
601
      java.net.URL connectURL = new URL(url);
602
      HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
648 603

  
649
      if( mMode==SUBMIT )
604
      conn.setDoInput(true);
605
      conn.setDoOutput(true);
606
      conn.setUseCaches(false);
607
      conn.setRequestMethod("GET");
608
      conn.connect();
609
      conn.getOutputStream().flush();
610

  
611
      InputStream is = conn.getInputStream();
612
      BufferedReader r = new BufferedReader(new InputStreamReader(is));
613
      StringBuilder answer = new StringBuilder();
614

  
615
      for (String line; (line = r.readLine()) != null; )
650 616
        {
651
        RubikScores.getInstance().successfulSubmit();
617
        answer.append(line).append('\n');
652 618
        }
619

  
620
      String updates = answer.toString();
621
      mUpdates.parse(updates);
622

  
623
      if( mUpdatee!=null ) mUpdatee.receiveUpdate(mUpdates);
624
      mDebugState = DEBUG_SUCCESS;
625
      }
626
    catch( final Exception e )
627
      {
628
      if( mUpdatee!=null ) mUpdatee.errorUpdate();
629
      mDebugState = DEBUG_FAILURE;
630
      }
631
    }
632

  
633
///////////////////////////////////////////////////////////////////////////////////////////////////
634

  
635
  private void suspiciousThread()
636
    {
637
    String url = constructSuspiciousURL();
638

  
639
    try
640
      {
641
      java.net.URL connectURL = new URL(url);
642
      HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
643

  
644
      conn.setDoInput(true);
645
      conn.setDoOutput(true);
646
      conn.setUseCaches(false);
647
      conn.setRequestMethod("GET");
648
      conn.connect();
649
      conn.getOutputStream().flush();
650
      conn.getInputStream();
651
      }
652
    catch( final Exception e )
653
      {
654
      // ignore
653 655
      }
654 656
    }
655 657

  
......
683 685

  
684 686
///////////////////////////////////////////////////////////////////////////////////////////////////
685 687

  
686
  private void start(Receiver receiver, Activity act, int mode)
688
  public void download(Receiver receiver, FragmentActivity act)
687 689
    {
690
    initializeStatics();
688 691
    mReceiver = receiver;
689
    mMode     = mode;
692
    figureOutVersion(act);
690 693

  
691
    try
694
    Thread thread = new Thread()
692 695
      {
693
      PackageInfo pInfo = act.getPackageManager().getPackageInfo( act.getPackageName(), 0);
694
      mVersion = pInfo.versionName;
695
      }
696
    catch (PackageManager.NameNotFoundException e)
697
      {
698
      mVersion = "0.9.2";
699
      }
696
      public void run()
697
        {
698
        downloadThread();
699
        }
700
      };
700 701

  
701
    Thread networkThrd = new Thread(this);
702
    networkThrd.start();
702
    thread.start();
703 703
    }
704 704

  
705 705
///////////////////////////////////////////////////////////////////////////////////////////////////
706 706

  
707
  public void download(Receiver receiver, FragmentActivity act)
707
  public void submit(Receiver receiver, FragmentActivity act)
708 708
    {
709
    start(receiver, act, DOWNLOAD);
710
    }
709
    initializeStatics();
710
    mReceiver = receiver;
711
    figureOutVersion(act);
711 712

  
712
///////////////////////////////////////////////////////////////////////////////////////////////////
713
    Thread thread = new Thread()
714
      {
715
      public void run()
716
        {
717
        submitThread();
718
        }
719
      };
713 720

  
714
  public void submit(Receiver receiver, FragmentActivity act)
715
    {
716
    start(receiver, act, SUBMIT);
721
    thread.start();
717 722
    }
718 723

  
719 724
///////////////////////////////////////////////////////////////////////////////////////////////////
720 725

  
721
  public void debug(AppCompatActivity act)
726
  public void debug()
722 727
    {
728
    initializeStatics();
723 729
    mDebugState = DEBUG_RUNNING;
724
    start(null, act, DEBUG);
730

  
731
    Thread thread = new Thread()
732
      {
733
      public void run()
734
        {
735
        debugThread();
736
        }
737
      };
738

  
739
    thread.start();
725 740
    }
726 741

  
727 742
///////////////////////////////////////////////////////////////////////////////////////////////////
728 743

  
729
  public void suspicious(AppCompatActivity act, String suspicious)
744
  public void suspicious(String suspicious)
730 745
    {
746
    initializeStatics();
731 747
    mSuspicious = suspicious;
732
    start(null, act, SUSPICIOUS);
748

  
749
    Thread thread = new Thread()
750
      {
751
      public void run()
752
        {
753
        suspiciousThread();
754
        }
755
      };
756

  
757
    thread.start();
733 758
    }
734 759

  
735 760
///////////////////////////////////////////////////////////////////////////////////////////////////
......
749 774
    else mUpdatee = updatee;
750 775
    }
751 776

  
777
///////////////////////////////////////////////////////////////////////////////////////////////////
778

  
779
  public void downloadIcons(Updatee updatee)
780
    {
781

  
782
    }
783

  
752 784
///////////////////////////////////////////////////////////////////////////////////////////////////
753 785

  
754 786
  public void updateDone(String shortName)
src/main/java/org/distorted/screens/RubikScreenPlay.java
710 710

  
711 711
  public void receiveUpdate(RubikUpdates updates)
712 712
    {
713
    updates.showDebug();
714

  
715 713
    Activity act = mWeakAct.get();
716 714

  
717 715
    if( act!=null )
......
746 744
    {
747 745
    android.util.Log.e("D", "Screen: Error receiving update");
748 746
    }
747

  
748
///////////////////////////////////////////////////////////////////////////////////////////////////
749

  
750
  public void iconDownloaded()
751
    {
752
    // empty
753
    }
749 754
  }

Also available in: Unified diff