Project

General

Profile

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

magiccube / src / main / java / org / distorted / network / RubikNetwork.java @ 3f7a4363

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.objectlib.main.ObjectList;
38

    
39
import static org.distorted.objectlib.main.ObjectList.MAX_LEVEL;
40

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

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

    
52
  public static final int MAX_PLACES = 10;
53

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

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

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

    
99
  private static int mTotal = 0;
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

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

    
114
  private static void initializeStatics()
115
    {
116
    if( mTotal==0      ) mTotal   = ObjectList.getTotal();
117
    if( mCountry==null ) mCountry = new String[mTotal][MAX_LEVEL][MAX_PLACES];
118
    if( mName==null    ) mName    = new String[mTotal][MAX_LEVEL][MAX_PLACES];
119
    if( mTime==null    ) mTime    = new  float[mTotal][MAX_LEVEL][MAX_PLACES];
120
    if( mPlaces==null  ) mPlaces  = new int[mTotal][MAX_LEVEL];
121
    }
122

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124

    
125
  private static String computeHash(String stringToHash, byte[] salt)
126
    {
127
    String generatedPassword;
128

    
129
    try
130
      {
131
      MessageDigest md = MessageDigest.getInstance("MD5");
132
      md.update(salt);
133
      byte[] bytes = md.digest(stringToHash.getBytes());
134
      StringBuilder sb = new StringBuilder();
135

    
136
      for (byte aByte : bytes)
137
        {
138
        sb.append(Integer.toString((aByte & 0xff) + 0x100, 16).substring(1));
139
        }
140

    
141
      generatedPassword = sb.toString();
142
      }
143
    catch (NoSuchAlgorithmException e)
144
      {
145
      return "NoSuchAlgorithm";
146
      }
147

    
148
    return generatedPassword;
149
    }
150

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

    
153
  private boolean fillValues()
154
    {
155
    int begin=-1 ,end, len = mScores.length();
156
    String row;
157

    
158
    if( len==0 )
159
      {
160
      mReceiver.error("1");
161
      return false;
162
      }
163
    else if( len<=2 )
164
      {
165
      mReceiver.error(mScores);
166
      return false;
167
      }
168

    
169
    for(int i=0; i<mTotal; i++)
170
      for(int j=0; j<MAX_LEVEL; j++)
171
        {
172
        mPlaces[i][j] = 0;
173
        }
174

    
175
    while( begin<len )
176
      {
177
      end = mScores.indexOf('\n', begin+1);
178
      if( end<0 ) end = len;
179

    
180
      try
181
        {
182
        row = mScores.substring(begin+1,end);
183
        fillRow(row);
184
        }
185
      catch(Exception ex)
186
        {
187
        // faulty row - ignore
188
        }
189

    
190
      begin = end;
191
      }
192

    
193
    return true;
194
    }
195

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197

    
198
  private void fillRow(String row)
199
    {
200
    int s1 = row.indexOf(' ');
201
    int s2 = row.indexOf(' ',s1+1);
202
    int s3 = row.indexOf(' ',s2+1);
203
    int s4 = row.indexOf(' ',s3+1);
204
    int s5 = row.length();
205

    
206
    if( s5>s4 && s4>s3 && s3>s2 && s2>s1 && s1>0 )
207
      {
208
      int object = ObjectList.unpackObjectFromString( row.substring(0,s1) );
209

    
210
      if( object>=0 && object<mTotal )
211
        {
212
        int level      = Integer.parseInt( row.substring(s1+1,s2) );
213
        String name    = row.substring(s2+1, s3);
214
        int time       = Integer.parseInt( row.substring(s3+1,s4) );
215
        String country = row.substring(s4+1, s5);
216

    
217
        if( country.equals("do") ) country = "dm"; // see RubikScores.setCountry()
218

    
219
        if(level>=0 && level<MAX_LEVEL)
220
          {
221
          int p = mPlaces[object][level];
222
          mPlaces[object][level]++;
223

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

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
  private void tryDoCommand(String row)
239
    {
240
    if( row.startsWith("comm") )
241
      {
242
      int colon = row.indexOf(':');
243

    
244
      if( colon>0 )
245
        {
246
        String commandNumber = row.substring(4,colon);
247
        int number;
248

    
249
        try
250
          {
251
          number = Integer.parseInt(commandNumber);
252
          }
253
        catch(NumberFormatException ex)
254
          {
255
          number=0;
256
          }
257

    
258
        if(number==1)
259
          {
260
          String country = row.substring(colon+1);
261
          RubikScores scores = RubikScores.getInstance();
262
          scores.setCountry(country);
263
          }
264
        }
265
      }
266
    }
267

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269

    
270
  private int getRendererType(String renderer)
271
    {
272
    if( renderer.contains("Adreno")  ) return REND_ADRENO;
273
    if( renderer.contains("Mali")    ) return REND_MALI;
274
    if( renderer.contains("PowerVR") ) return REND_POWER;
275

    
276
    return REND_OTHER;
277
    }
278

    
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280

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

    
290
    if( type==REND_MALI )
291
      {
292
      int firstHyphen = renderer.indexOf('-');
293
      String ret = renderer.substring(firstHyphen+1);
294
      return URLencode(ret);
295
      }
296

    
297
    return "other";
298
    }
299

    
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301

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

    
322
///////////////////////////////////////////////////////////////////////////////////////////////////
323

    
324
  private String URLencode(String s)
325
    {
326
    StringBuilder sbuf = new StringBuilder();
327
    int len = s.length();
328

    
329
    for (int i = 0; i < len; i++)
330
      {
331
      int ch = s.charAt(i);
332

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

    
356
    return sbuf.toString();
357
    }
358

    
359
///////////////////////////////////////////////////////////////////////////////////////////////////
360

    
361
  private void sendDebug()
362
    {
363
    String url = constructDebugURL();
364

    
365
    try
366
      {
367
      java.net.URL connectURL = new URL(url);
368
      HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
369

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

    
384
///////////////////////////////////////////////////////////////////////////////////////////////////
385

    
386
  private boolean network(String url)
387
    {
388
    try
389
      {
390
      java.net.URL connectURL = new URL(url);
391
      HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
392

    
393
      conn.setDoInput(true);
394
      conn.setDoOutput(true);
395
      conn.setUseCaches(false);
396
      conn.setRequestMethod("GET");
397
      conn.connect();
398
      conn.getOutputStream().flush();
399

    
400
      try( InputStream is = conn.getInputStream() )
401
        {
402
        int ch;
403
        StringBuilder sb = new StringBuilder();
404
        while( ( ch = is.read() ) != -1 )
405
          {
406
          sb.append( (char)ch );
407
          }
408
        mScores = sb.toString();
409
        }
410
      catch( final Exception e)
411
        {
412
        mReceiver.message("Failed to get an answer from the High Scores server");
413
        return false;
414
        }
415
      }
416
    catch( final UnknownHostException e )
417
      {
418
      mReceiver.message("No access to Internet");
419
      return false;
420
      }
421
    catch( final SecurityException e )
422
      {
423
      mReceiver.message("Application not authorized to connect to the Internet");
424
      return false;
425
      }
426
    catch( final Exception e )
427
      {
428
      mReceiver.message(e.getMessage());
429
      return false;
430
      }
431

    
432
    if( mScores.length()==0 )
433
      {
434
      mReceiver.message("Failed to download scores");
435
      return false;
436
      }
437

    
438
    return true;
439
    }
440

    
441
///////////////////////////////////////////////////////////////////////////////////////////////////
442

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

    
453
    renderer = URLencode(renderer);
454
    version  = URLencode(version);
455

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

    
459
    return url;
460
    }
461

    
462
///////////////////////////////////////////////////////////////////////////////////////////////////
463

    
464
  private String constructDownloadURL()
465
    {
466
    RubikScores scores = RubikScores.getInstance();
467
    String name = URLencode(scores.getName());
468
    String veri = scores.isVerified() ? name : "";
469
    int numRuns = scores.getNumRuns();
470
    int numPlay = scores.getNumPlays();
471
    String country = scores.getCountry();
472

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

    
477
    return url;
478
    }
479

    
480
///////////////////////////////////////////////////////////////////////////////////////////////////
481

    
482
  private String constructSubmitURL()
483
    {
484
    RubikScores scores = RubikScores.getInstance();
485
    String name = URLencode(scores.getName());
486
    String veri = scores.isVerified() ? name : "";
487
    int numRuns = scores.getNumRuns();
488
    int numPlay = scores.getNumPlays();
489
    int deviceID= scores.getDeviceID();
490
    String reclist = scores.getRecordList("&o=","&l=","&t=");
491
    String country = scores.getCountry();
492
    long epoch = System.currentTimeMillis();
493
    String salt = "cuboid";
494

    
495
    String renderer = DistortedLibrary.getDriverRenderer();
496
    String version  = DistortedLibrary.getDriverVersion();
497

    
498
    int type = getRendererType(renderer);
499
    renderer = parseRenderer(type,renderer);
500
    version  = parseVersion(type,version);
501

    
502
    String url1="https://distorted.org/magic/cgi-bin/submit.cgi";
503
    String url2 = "n="+name+"&v="+veri+"&r="+numRuns+"&p="+numPlay+"&i="+deviceID+"&e="+mVersion+"d";
504
    url2 += "&d="+renderer+"&s="+version;
505
    url2 += reclist+"&c="+country+"&f="+epoch+"&oo="+ ObjectList.getObjectList();
506
    url2 += "&min=0&max="+MAX_LEVEL+"&lo="+MAX_PLACES;
507
    String hash = computeHash( url2, salt.getBytes() );
508

    
509
    return url1 + "?" + url2 + "&h=" + hash;
510
    }
511

    
512
///////////////////////////////////////////////////////////////////////////////////////////////////
513

    
514
  private boolean gottaDownload()
515
    {
516
    return ((mScores.length()==0) && !mRunning);
517
    }
518

    
519
///////////////////////////////////////////////////////////////////////////////////////////////////
520

    
521
  @Override
522
  public void run()
523
    {
524
    boolean receiveValues=true;
525

    
526
    initializeStatics();
527

    
528
    try
529
      {
530
      if( mMode==DOWNLOAD && gottaDownload() )
531
        {
532
        mRunning = true;
533
        receiveValues = network(constructDownloadURL());
534
        }
535
      if( mMode==SUBMIT )
536
        {
537
        mRunning = true;
538

    
539
        if( RubikScores.getInstance().thereAreUnsubmittedRecords() )
540
          {
541
          receiveValues = network(constructSubmitURL());
542
          }
543
        }
544
      if( mMode==DEBUG )
545
        {
546
        sendDebug();
547
        receiveValues = false;
548
        mRunning = false;
549
        }
550
      }
551
    catch( Exception e )
552
      {
553
      if( mReceiver!=null ) mReceiver.message("Exception downloading records: "+e.getMessage() );
554
      }
555

    
556
    if( mRunning )
557
      {
558
      receiveValues = fillValues();
559
      mRunning = false;
560
      }
561

    
562
    if( receiveValues )
563
      {
564
      if( mReceiver!=null ) mReceiver.receive(mCountry, mName, mTime);
565

    
566
      if( mMode==SUBMIT )
567
        {
568
        RubikScores.getInstance().successfulSubmit();
569
        }
570
      }
571
    }
572

    
573
///////////////////////////////////////////////////////////////////////////////////////////////////
574

    
575
  private RubikNetwork()
576
    {
577

    
578
    }
579

    
580
///////////////////////////////////////////////////////////////////////////////////////////////////
581
// PUBLIC API
582
///////////////////////////////////////////////////////////////////////////////////////////////////
583

    
584
  public static void onPause()
585
    {
586
    mRunning = false;
587
    }
588

    
589
///////////////////////////////////////////////////////////////////////////////////////////////////
590

    
591
  public static RubikNetwork getInstance()
592
    {
593
    if( mThis==null )
594
      {
595
      mThis = new RubikNetwork();
596
      }
597

    
598
    return mThis;
599
    }
600

    
601
///////////////////////////////////////////////////////////////////////////////////////////////////
602

    
603
  private void start(Receiver receiver, Activity act, int mode)
604
    {
605
    mReceiver = receiver;
606
    mMode     = mode;
607

    
608
    try
609
      {
610
      PackageInfo pInfo = act.getPackageManager().getPackageInfo( act.getPackageName(), 0);
611
      mVersion = pInfo.versionName;
612
      }
613
    catch (PackageManager.NameNotFoundException e)
614
      {
615
      mVersion = "0.9.2";
616
      }
617

    
618
    Thread networkThrd = new Thread(this);
619
    networkThrd.start();
620
    }
621

    
622
///////////////////////////////////////////////////////////////////////////////////////////////////
623

    
624
  public void download(Receiver receiver, FragmentActivity act)
625
    {
626
    start(receiver, act, DOWNLOAD);
627
    }
628

    
629
///////////////////////////////////////////////////////////////////////////////////////////////////
630

    
631
  public void submit(Receiver receiver, FragmentActivity act)
632
    {
633
    start(receiver, act, SUBMIT);
634
    }
635

    
636
///////////////////////////////////////////////////////////////////////////////////////////////////
637

    
638
  public void debug(AppCompatActivity act)
639
    {
640
    start(null, act, DEBUG);
641
    }
642
}
(1-1/2)