Project

General

Profile

Download (20.9 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / network / RubikNetwork.java @ 100764d0

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.network;
21

    
22
import java.io.InputStream;
23
import java.net.HttpURLConnection;
24
import java.net.URL;
25
import java.net.UnknownHostException;
26
import java.security.MessageDigest;
27
import java.security.NoSuchAlgorithmException;
28

    
29
import android.app.Activity;
30
import android.content.pm.PackageInfo;
31
import android.content.pm.PackageManager;
32

    
33
import androidx.appcompat.app.AppCompatActivity;
34
import androidx.fragment.app.FragmentActivity;
35

    
36
import org.distorted.library.main.DistortedLibrary;
37
import org.distorted.objects.RubikObject;
38
import org.distorted.objects.RubikObjectList;
39

    
40
import static org.distorted.objects.RubikObjectList.MAX_LEVEL;
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

    
44
public class RubikNetwork implements Runnable
45
  {
46
  public interface Receiver
47
    {
48
    void receive(String[][][] country, String[][][] name, float[][][] time);
49
    void message(String mess);
50
    void error(String error);
51
    }
52

    
53
  public static final int MAX_PLACES = 10;
54

    
55
  private static final int DOWNLOAD   = 0;
56
  private static final int SUBMIT     = 1;
57
  private static final int DEBUG      = 2;
58
  private static final int IDLE       = 3;
59

    
60
  private static final int REND_ADRENO= 0;
61
  private static final int REND_MALI  = 1;
62
  private static final int REND_POWER = 2;
63
  private static final int REND_OTHER = 3;
64

    
65
  private final String[] hex = {
66
    "%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07",
67
    "%08", "%09", "%0a", "%0b", "%0c", "%0d", "%0e", "%0f",
68
    "%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17",
69
    "%18", "%19", "%1a", "%1b", "%1c", "%1d", "%1e", "%1f",
70
    "%20", "%21", "%22", "%23", "%24", "%25", "%26", "%27",
71
    "%28", "%29", "%2a", "%2b", "%2c", "%2d", "%2e", "%2f",
72
    "%30", "%31", "%32", "%33", "%34", "%35", "%36", "%37",
73
    "%38", "%39", "%3a", "%3b", "%3c", "%3d", "%3e", "%3f",
74
    "%40", "%41", "%42", "%43", "%44", "%45", "%46", "%47",
75
    "%48", "%49", "%4a", "%4b", "%4c", "%4d", "%4e", "%4f",
76
    "%50", "%51", "%52", "%53", "%54", "%55", "%56", "%57",
77
    "%58", "%59", "%5a", "%5b", "%5c", "%5d", "%5e", "%5f",
78
    "%60", "%61", "%62", "%63", "%64", "%65", "%66", "%67",
79
    "%68", "%69", "%6a", "%6b", "%6c", "%6d", "%6e", "%6f",
80
    "%70", "%71", "%72", "%73", "%74", "%75", "%76", "%77",
81
    "%78", "%79", "%7a", "%7b", "%7c", "%7d", "%7e", "%7f",
82
    "%80", "%81", "%82", "%83", "%84", "%85", "%86", "%87",
83
    "%88", "%89", "%8a", "%8b", "%8c", "%8d", "%8e", "%8f",
84
    "%90", "%91", "%92", "%93", "%94", "%95", "%96", "%97",
85
    "%98", "%99", "%9a", "%9b", "%9c", "%9d", "%9e", "%9f",
86
    "%a0", "%a1", "%a2", "%a3", "%a4", "%a5", "%a6", "%a7",
87
    "%a8", "%a9", "%aa", "%ab", "%ac", "%ad", "%ae", "%af",
88
    "%b0", "%b1", "%b2", "%b3", "%b4", "%b5", "%b6", "%b7",
89
    "%b8", "%b9", "%ba", "%bb", "%bc", "%bd", "%be", "%bf",
90
    "%c0", "%c1", "%c2", "%c3", "%c4", "%c5", "%c6", "%c7",
91
    "%c8", "%c9", "%ca", "%cb", "%cc", "%cd", "%ce", "%cf",
92
    "%d0", "%d1", "%d2", "%d3", "%d4", "%d5", "%d6", "%d7",
93
    "%d8", "%d9", "%da", "%db", "%dc", "%dd", "%de", "%df",
94
    "%e0", "%e1", "%e2", "%e3", "%e4", "%e5", "%e6", "%e7",
95
    "%e8", "%e9", "%ea", "%eb", "%ec", "%ed", "%ee", "%ef",
96
    "%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7",
97
    "%f8", "%f9", "%fa", "%fb", "%fc", "%fd", "%fe", "%ff"
98
    };
99

    
100
  private static String[][][] mCountry;
101
  private static String[][][] mName;
102
  private static float[][][] mTime;
103
  private static int[][] mPlaces;
104

    
105
  private static RubikNetwork mThis;
106
  private static String mScores = "";
107
  private static boolean mRunning = false;
108
  private static int mMode = IDLE;
109
  private static Receiver mReceiver;
110
  private static String mVersion;
111
  private static int mNumObjects;
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114

    
115
  private static void initializeStatics()
116
    {
117
    int newNum = RubikObjectList.getNumObjects();
118

    
119
    if( mCountry==null || newNum!=mNumObjects ) mCountry = new String[newNum][MAX_LEVEL][MAX_PLACES];
120
    if( mName==null    || newNum!=mNumObjects ) mName    = new String[newNum][MAX_LEVEL][MAX_PLACES];
121
    if( mTime==null    || newNum!=mNumObjects ) mTime    = new  float[newNum][MAX_LEVEL][MAX_PLACES];
122
    if( mPlaces==null  || newNum!=mNumObjects ) mPlaces  = new int[newNum][MAX_LEVEL];
123

    
124
    mNumObjects = newNum;
125
    }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

    
129
  private static String computeHash(String stringToHash, byte[] salt)
130
    {
131
    String generatedPassword;
132

    
133
    try
134
      {
135
      MessageDigest md = MessageDigest.getInstance("MD5");
136
      md.update(salt);
137
      byte[] bytes = md.digest(stringToHash.getBytes());
138
      StringBuilder sb = new StringBuilder();
139

    
140
      for (byte aByte : bytes)
141
        {
142
        sb.append(Integer.toString((aByte & 0xff) + 0x100, 16).substring(1));
143
        }
144

    
145
      generatedPassword = sb.toString();
146
      }
147
    catch (NoSuchAlgorithmException e)
148
      {
149
      return "NoSuchAlgorithm";
150
      }
151

    
152
    return generatedPassword;
153
    }
154

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156

    
157
  private boolean fillValues()
158
    {
159
    int begin=-1 ,end, len = mScores.length();
160
    String row;
161

    
162
    if( len==0 )
163
      {
164
      if( mReceiver!=null ) mReceiver.error("1");
165
      return false;
166
      }
167
    else if( len<=2 )
168
      {
169
      if( mReceiver!=null ) mReceiver.error(mScores);
170
      return false;
171
      }
172

    
173
    for(int i=0; i<mNumObjects; i++)
174
      for(int j=0; j<MAX_LEVEL; j++)
175
        {
176
        mPlaces[i][j] = 0;
177
        }
178

    
179
    while( begin<len )
180
      {
181
      end = mScores.indexOf('\n', begin+1);
182
      if( end<0 ) end = len;
183

    
184
      try
185
        {
186
        row = mScores.substring(begin+1,end);
187
        fillRow(row);
188
        }
189
      catch(Exception ex)
190
        {
191
        // faulty row - ignore
192
        }
193

    
194
      begin = end;
195
      }
196

    
197
    return true;
198
    }
199

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

    
202
  private void fillRow(String row)
203
    {
204
    int s1 = row.indexOf(' ');
205
    int s2 = row.indexOf(' ',s1+1);
206
    int s3 = row.indexOf(' ',s2+1);
207
    int s4 = row.indexOf(' ',s3+1);
208
    int s5 = row.length();
209

    
210
    if( s5>s4 && s4>s3 && s3>s2 && s2>s1 && s1>0 )
211
      {
212
      int object = RubikObjectList.getOrdinal( row.substring(0,s1) );
213

    
214
      if( object>=0 && object<mNumObjects )
215
        {
216
        int level      = Integer.parseInt( row.substring(s1+1,s2) );
217
        String name    = row.substring(s2+1, s3);
218
        int time       = Integer.parseInt( row.substring(s3+1,s4) );
219
        String country = row.substring(s4+1, s5);
220

    
221
        if( country.equals("do") ) country = "dm"; // see RubikScores.setCountry()
222

    
223
        if(level>=0 && level<MAX_LEVEL)
224
          {
225
          int p = mPlaces[object][level];
226
          mPlaces[object][level]++;
227

    
228
          mCountry[object][level][p] = country;
229
          mName   [object][level][p] = name;
230
          mTime   [object][level][p] = ((float)(time/10))/100.0f;
231
          }
232
        }
233
      }
234
    else
235
      {
236
      tryDoCommand(row);
237
      }
238
    }
239

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241

    
242
  private void tryDoCommand(String row)
243
    {
244
    if( row.startsWith("comm") )
245
      {
246
      int colon = row.indexOf(':');
247

    
248
      if( colon>0 )
249
        {
250
        String commandNumber = row.substring(4,colon);
251
        int number;
252

    
253
        try
254
          {
255
          number = Integer.parseInt(commandNumber);
256
          }
257
        catch(NumberFormatException ex)
258
          {
259
          number=0;
260
          }
261

    
262
        if(number==1)
263
          {
264
          String country = row.substring(colon+1);
265
          RubikScores scores = RubikScores.getInstance();
266
          scores.setCountry(country);
267
          }
268
        }
269
      }
270
    }
271

    
272
///////////////////////////////////////////////////////////////////////////////////////////////////
273

    
274
  private int getRendererType(String renderer)
275
    {
276
    if( renderer.contains("Adreno")  ) return REND_ADRENO;
277
    if( renderer.contains("Mali")    ) return REND_MALI;
278
    if( renderer.contains("PowerVR") ) return REND_POWER;
279

    
280
    return REND_OTHER;
281
    }
282

    
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284

    
285
  private String parseRenderer(final int type, String renderer)
286
    {
287
    if( type==REND_ADRENO || type==REND_POWER )
288
      {
289
      int lastSpace = renderer.lastIndexOf(' ');
290
      String ret = renderer.substring(lastSpace+1);
291
      return URLencode(ret);
292
      }
293

    
294
    if( type==REND_MALI )
295
      {
296
      int firstHyphen = renderer.indexOf('-');
297
      String ret = renderer.substring(firstHyphen+1);
298
      return URLencode(ret);
299
      }
300

    
301
    return "other";
302
    }
303

    
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305

    
306
  private String parseVersion(final int type, String version)
307
    {
308
    switch(type)
309
      {
310
      case REND_ADRENO: int aMonkey = version.indexOf('@');
311
                        int aDot = version.indexOf('.', aMonkey);
312
                        String ret1 = aDot>=3 ? version.substring(aDot-3,aDot) : "";
313
                        return URLencode(ret1);
314
      case REND_MALI  : int mV1 = version.indexOf("v1");
315
                        int mHyphen = version.indexOf('-', mV1);
316
                        String ret2 = mHyphen>mV1+3 && mV1>=0 ? version.substring(mV1+3,mHyphen) : "";
317
                        return URLencode(ret2);
318
      case REND_POWER : int pMonkey = version.indexOf('@');
319
                        int pSpace  = version.lastIndexOf(' ');
320
                        String ret3 = pSpace>=0 && pMonkey>pSpace+1 ? version.substring(pSpace+1,pMonkey) : "";
321
                        return URLencode(ret3);
322
      default         : return "";
323
      }
324
    }
325

    
326
///////////////////////////////////////////////////////////////////////////////////////////////////
327

    
328
  private String URLencode(String s)
329
    {
330
    StringBuilder sbuf = new StringBuilder();
331
    int len = s.length();
332

    
333
    for (int i = 0; i < len; i++)
334
      {
335
      int ch = s.charAt(i);
336

    
337
           if ('A' <= ch && ch <= 'Z') sbuf.append((char)ch);
338
      else if ('a' <= ch && ch <= 'z') sbuf.append((char)ch);
339
      else if ('0' <= ch && ch <= '9') sbuf.append((char)ch);
340
      else if (ch == ' '             ) sbuf.append('+');
341
      else if (ch == '-' || ch == '_'
342
            || ch == '.' || ch == '!'
343
            || ch == '~' || ch == '*'
344
            || ch == '\'' || ch == '('
345
            || ch == ')'             ) sbuf.append((char)ch);
346
      else if (ch <= 0x007f)           sbuf.append(hex[ch]);
347
      else if (ch <= 0x07FF)
348
        {
349
        sbuf.append(hex[0xc0 | (ch >> 6)]);
350
        sbuf.append(hex[0x80 | (ch & 0x3F)]);
351
        }
352
      else
353
        {
354
        sbuf.append(hex[0xe0 | (ch >> 12)]);
355
        sbuf.append(hex[0x80 | ((ch >> 6) & 0x3F)]);
356
        sbuf.append(hex[0x80 | (ch & 0x3F)]);
357
        }
358
      }
359

    
360
    return sbuf.toString();
361
    }
362

    
363
///////////////////////////////////////////////////////////////////////////////////////////////////
364

    
365
  private void sendDebug()
366
    {
367
    String url = constructDebugURL();
368

    
369
    try
370
      {
371
      java.net.URL connectURL = new URL(url);
372
      HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
373

    
374
      conn.setDoInput(true);
375
      conn.setDoOutput(true);
376
      conn.setUseCaches(false);
377
      conn.setRequestMethod("GET");
378
      conn.connect();
379
      conn.getOutputStream().flush();
380
      conn.getInputStream();
381
      }
382
    catch( final Exception e )
383
      {
384
      // ignore
385
      }
386
    }
387

    
388
///////////////////////////////////////////////////////////////////////////////////////////////////
389

    
390
  private boolean network(String url)
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
      try( InputStream is = conn.getInputStream() )
405
        {
406
        int ch;
407
        StringBuilder sb = new StringBuilder();
408
        while( ( ch = is.read() ) != -1 )
409
          {
410
          sb.append( (char)ch );
411
          }
412
        mScores = sb.toString();
413
        }
414
      catch( final Exception e)
415
        {
416
        if( mReceiver!=null ) mReceiver.message("Failed to get an answer from the High Scores server");
417
        return false;
418
        }
419
      }
420
    catch( final UnknownHostException e )
421
      {
422
      if( mReceiver!=null ) mReceiver.message("No access to Internet");
423
      return false;
424
      }
425
    catch( final SecurityException e )
426
      {
427
      if( mReceiver!=null ) mReceiver.message("Application not authorized to connect to the Internet");
428
      return false;
429
      }
430
    catch( final Exception e )
431
      {
432
      if( mReceiver!=null ) mReceiver.message(e.getMessage());
433
      return false;
434
      }
435

    
436
    if( mScores.length()==0 )
437
      {
438
      if( mReceiver!=null ) mReceiver.message("Failed to download scores");
439
      return false;
440
      }
441

    
442
    return true;
443
    }
444

    
445
///////////////////////////////////////////////////////////////////////////////////////////////////
446

    
447
  private String constructDebugURL()
448
    {
449
    RubikScores scores = RubikScores.getInstance();
450
    String name = URLencode(scores.getName());
451
    int numRuns = scores.getNumRuns();
452
    int numPlay = scores.getNumPlays();
453
    String country = scores.getCountry();
454
    String renderer = DistortedLibrary.getDriverRenderer();
455
    String version  = DistortedLibrary.getDriverVersion();
456

    
457
    renderer = URLencode(renderer);
458
    version  = URLencode(version);
459

    
460
    String url="https://distorted.org/magic/cgi-bin/debugs.cgi";
461
    url += "?n="+name+"&r="+numRuns+"&p="+numPlay+"&c="+country+"&e="+mVersion+"d"+"&d="+renderer+"&v="+version;
462

    
463
    return url;
464
    }
465

    
466
///////////////////////////////////////////////////////////////////////////////////////////////////
467

    
468
  private static String getObjectList()
469
    {
470
    StringBuilder list = new StringBuilder();
471

    
472
    for(int i=0; i<mNumObjects; i++)
473
      {
474
      RubikObject object = RubikObjectList.getObject(i);
475

    
476
      if( object!=null )
477
        {
478
        if( i>0 ) list.append(',');
479
        list.append(object.getName());
480
        }
481
      }
482

    
483
    return list.toString();
484
    }
485

    
486
///////////////////////////////////////////////////////////////////////////////////////////////////
487

    
488
  private String constructDownloadURL()
489
    {
490
    RubikScores scores = RubikScores.getInstance();
491
    String name = URLencode(scores.getName());
492
    String veri = scores.isVerified() ? name : "";
493
    int numRuns = scores.getNumRuns();
494
    int numPlay = scores.getNumPlays();
495
    String country = scores.getCountry();
496

    
497
    String url="https://distorted.org/magic/cgi-bin/download.cgi";
498
    url += "?n="+name+"&v="+veri+"&r="+numRuns+"&p="+numPlay+"&c="+country+"&e="+mVersion+"d";
499
    url += "&o="+getObjectList()+"&min=0&max="+MAX_LEVEL+"&l="+MAX_PLACES;
500

    
501
    return url;
502
    }
503

    
504
///////////////////////////////////////////////////////////////////////////////////////////////////
505

    
506
  private String constructSubmitURL()
507
    {
508
    RubikScores scores = RubikScores.getInstance();
509
    String name = URLencode(scores.getName());
510
    String veri = scores.isVerified() ? name : "";
511
    int numRuns = scores.getNumRuns();
512
    int numPlay = scores.getNumPlays();
513
    int deviceID= scores.getDeviceID();
514
    String reclist = scores.getRecordList("&o=","&l=","&t=");
515
    String country = scores.getCountry();
516
    long epoch = System.currentTimeMillis();
517
    String salt = "cubold";
518

    
519
    String renderer = DistortedLibrary.getDriverRenderer();
520
    String version  = DistortedLibrary.getDriverVersion();
521

    
522
    int type = getRendererType(renderer);
523
    renderer = parseRenderer(type,renderer);
524
    version  = parseVersion(type,version);
525

    
526
    String url1="https://distorted.org/magic/cgi-bin/submit.cgi";
527
    String url2 = "n="+name+"&v="+veri+"&r="+numRuns+"&p="+numPlay+"&i="+deviceID+"&e="+mVersion+"d";
528
    url2 += "&d="+renderer+"&s="+version;
529
    url2 += reclist+"&c="+country+"&f="+epoch+"&oo="+getObjectList();
530
    url2 += "&min=0&max="+MAX_LEVEL+"&lo="+MAX_PLACES;
531
    String hash = computeHash( url2+"d", salt.getBytes() );
532

    
533
    return url1 + "?" + url2 + "&h=" + hash;
534
    }
535

    
536
///////////////////////////////////////////////////////////////////////////////////////////////////
537

    
538
  private boolean gottaDownload()
539
    {
540
    return ((mScores.length()==0) && !mRunning);
541
    }
542

    
543
///////////////////////////////////////////////////////////////////////////////////////////////////
544

    
545
  @Override
546
  public void run()
547
    {
548
    boolean receiveValues=true;
549

    
550
    initializeStatics();
551

    
552
    try
553
      {
554
      if( mMode==DOWNLOAD && gottaDownload() )
555
        {
556
        mRunning = true;
557
        receiveValues = network(constructDownloadURL());
558
        }
559
      if( mMode==SUBMIT )
560
        {
561
        mRunning = true;
562

    
563
        if( RubikScores.getInstance().thereAreUnsubmittedRecords() )
564
          {
565
          receiveValues = network(constructSubmitURL());
566
          }
567
        }
568
      if( mMode==DEBUG )
569
        {
570
        sendDebug();
571
        receiveValues = false;
572
        mRunning = false;
573
        }
574
      }
575
    catch( Exception e )
576
      {
577
      if( mReceiver!=null ) mReceiver.message("Exception downloading records: "+e.getMessage() );
578
      }
579

    
580
    if( mRunning )
581
      {
582
      receiveValues = fillValues();
583
      mRunning = false;
584
      }
585

    
586
    if( receiveValues )
587
      {
588
      if( mReceiver!=null ) mReceiver.receive(mCountry, mName, mTime);
589

    
590
      if( mMode==SUBMIT )
591
        {
592
        RubikScores.getInstance().successfulSubmit();
593
        }
594
      }
595
    }
596

    
597
///////////////////////////////////////////////////////////////////////////////////////////////////
598

    
599
  private RubikNetwork()
600
    {
601

    
602
    }
603

    
604
///////////////////////////////////////////////////////////////////////////////////////////////////
605
// PUBLIC API
606
///////////////////////////////////////////////////////////////////////////////////////////////////
607

    
608
  public static void onPause()
609
    {
610
    mRunning = false;
611
    }
612

    
613
///////////////////////////////////////////////////////////////////////////////////////////////////
614

    
615
  public static RubikNetwork getInstance()
616
    {
617
    if( mThis==null )
618
      {
619
      mThis = new RubikNetwork();
620
      }
621

    
622
    return mThis;
623
    }
624

    
625
///////////////////////////////////////////////////////////////////////////////////////////////////
626

    
627
  private void start(Receiver receiver, Activity act, int mode)
628
    {
629
    mReceiver = receiver;
630
    mMode     = mode;
631

    
632
    try
633
      {
634
      PackageInfo pInfo = act.getPackageManager().getPackageInfo( act.getPackageName(), 0);
635
      mVersion = pInfo.versionName;
636
      }
637
    catch (PackageManager.NameNotFoundException e)
638
      {
639
      mVersion = "0.9.2";
640
      }
641

    
642
    Thread networkThrd = new Thread(this);
643
    networkThrd.start();
644
    }
645

    
646
///////////////////////////////////////////////////////////////////////////////////////////////////
647

    
648
  public void download(Receiver receiver, FragmentActivity act)
649
    {
650
    start(receiver, act, DOWNLOAD);
651
    }
652

    
653
///////////////////////////////////////////////////////////////////////////////////////////////////
654

    
655
  public void submit(Receiver receiver, FragmentActivity act)
656
    {
657
    start(receiver, act, SUBMIT);
658
    }
659

    
660
///////////////////////////////////////////////////////////////////////////////////////////////////
661

    
662
  public void debug(AppCompatActivity act)
663
    {
664
    start(null, act, DEBUG);
665
    }
666
}
(1-1/2)