Project

General

Profile

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

magiccube / src / main / java / org / distorted / network / RubikNetwork.java @ 11d68e9c

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.BufferedReader;
23
import java.io.InputStream;
24
import java.io.InputStreamReader;
25
import java.net.HttpURLConnection;
26
import java.net.URL;
27
import java.net.UnknownHostException;
28
import java.security.MessageDigest;
29
import java.security.NoSuchAlgorithmException;
30

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

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

    
38
import org.distorted.library.main.DistortedLibrary;
39
import org.distorted.objects.RubikObject;
40
import org.distorted.objects.RubikObjectList;
41

    
42
import static org.distorted.objects.RubikObjectList.MAX_LEVEL;
43

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

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

    
55
  public static final int MAX_PLACES = 10;
56

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

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

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

    
102
  private static String[][][] mCountry;
103
  private static String[][][] mName;
104
  private static float[][][] mTime;
105
  private static int[][] mPlaces;
106

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

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116

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

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

    
126
    mNumObjects = newNum;
127
    }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

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

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

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

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

    
154
    return generatedPassword;
155
    }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

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

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

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

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

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

    
196
      begin = end;
197
      }
198

    
199
    return true;
200
    }
201

    
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203

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

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

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

    
223
        if( country.equals("do") ) country = "dm"; // see RubikScores.setCountry()
224

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

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

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

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

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

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

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

    
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275

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

    
282
    return REND_OTHER;
283
    }
284

    
285
///////////////////////////////////////////////////////////////////////////////////////////////////
286

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

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

    
303
    return "other";
304
    }
305

    
306
///////////////////////////////////////////////////////////////////////////////////////////////////
307

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

    
328
///////////////////////////////////////////////////////////////////////////////////////////////////
329

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

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

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

    
362
    return sbuf.toString();
363
    }
364

    
365
///////////////////////////////////////////////////////////////////////////////////////////////////
366

    
367
  private void sendDebug()
368
    {
369
    String url = constructDebugURL();
370

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

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

    
390
///////////////////////////////////////////////////////////////////////////////////////////////////
391

    
392
  private boolean network(String url)
393
    {
394
    try
395
      {
396
      java.net.URL connectURL = new URL(url);
397
      HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
398

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

    
406
      InputStream is = conn.getInputStream();
407
      BufferedReader r = new BufferedReader(new InputStreamReader(is));
408
      StringBuilder total = new StringBuilder();
409

    
410
      for (String line; (line = r.readLine()) != null; )
411
        {
412
        total.append(line).append('\n');
413
        }
414

    
415
      mScores = total.toString();
416
      }
417
    catch( final UnknownHostException e )
418
      {
419
      if( mReceiver!=null ) mReceiver.message("No access to Internet");
420
      return false;
421
      }
422
    catch( final SecurityException e )
423
      {
424
      if( mReceiver!=null ) mReceiver.message("Application not authorized to connect to the Internet");
425
      return false;
426
      }
427
    catch( final Exception e )
428
      {
429
      if( mReceiver!=null ) mReceiver.message(e.getMessage());
430
      return false;
431
      }
432

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

    
439
    return true;
440
    }
441

    
442
///////////////////////////////////////////////////////////////////////////////////////////////////
443

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

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

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

    
460
    return url;
461
    }
462

    
463
///////////////////////////////////////////////////////////////////////////////////////////////////
464

    
465
  private static String getObjectList()
466
    {
467
    StringBuilder list = new StringBuilder();
468

    
469
    for(int i=0; i<mNumObjects; i++)
470
      {
471
      RubikObject object = RubikObjectList.getObject(i);
472

    
473
      if( object!=null )
474
        {
475
        if( i>0 ) list.append(',');
476
        list.append(object.getName());
477
        }
478
      }
479

    
480
    return list.toString();
481
    }
482

    
483
///////////////////////////////////////////////////////////////////////////////////////////////////
484

    
485
  private String constructDownloadURL()
486
    {
487
    RubikScores scores = RubikScores.getInstance();
488
    String name = URLencode(scores.getName());
489
    int numRuns = scores.getNumRuns();
490
    int numPlay = scores.getNumPlays();
491
    String country = scores.getCountry();
492

    
493
    String url="https://distorted.org/magic/cgi-bin/download.cgi";
494
    url += "?n="+name+"&r="+numRuns+"&p="+numPlay+"&c="+country+"&e="+mVersion;
495

    
496
    return url;
497
    }
498

    
499
///////////////////////////////////////////////////////////////////////////////////////////////////
500

    
501
  private String constructSubmitURL()
502
    {
503
    RubikScores scores = RubikScores.getInstance();
504
    String name = URLencode(scores.getName());
505
    String veri = scores.isVerified() ? "1" : "";
506
    int numRuns = scores.getNumRuns();
507
    int numPlay = scores.getNumPlays();
508
    int deviceID= scores.getDeviceID();
509
    String reclist = scores.getRecordList("&o=","&l=","&t=");
510
    String country = scores.getCountry();
511
    long epoch = System.currentTimeMillis();
512
    String salt = "cuboid";
513

    
514
    String renderer = DistortedLibrary.getDriverRenderer();
515
    String version  = DistortedLibrary.getDriverVersion();
516

    
517
    int type = getRendererType(renderer);
518
    renderer = parseRenderer(type,renderer);
519
    version  = parseVersion(type,version);
520

    
521
    String url1="https://distorted.org/magic/cgi-bin/submit.cgi";
522
    String url2 = "n="+name+"&v="+veri+"&r="+numRuns+"&p="+numPlay+"&i="+deviceID+"&e="+mVersion;
523
    url2 += "&d="+renderer+"&s="+version+reclist+"&c="+country+"&f="+epoch;
524
    String hash = computeHash( url2, salt.getBytes() );
525

    
526
    return url1 + "?" + url2 + "&h=" + hash;
527
    }
528

    
529
///////////////////////////////////////////////////////////////////////////////////////////////////
530

    
531
  private boolean gottaDownload()
532
    {
533
    return ((mScores.length()==0) && !mRunning);
534
    }
535

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

    
538
  @Override
539
  public void run()
540
    {
541
    boolean receiveValues=true;
542

    
543
    initializeStatics();
544

    
545
    try
546
      {
547
      if( mMode==DOWNLOAD && gottaDownload() )
548
        {
549
        mRunning = true;
550
        receiveValues = network(constructDownloadURL());
551
        }
552
      if( mMode==SUBMIT )
553
        {
554
        mRunning = true;
555

    
556
        if( RubikScores.getInstance().thereAreUnsubmittedRecords() )
557
          {
558
          receiveValues = network(constructSubmitURL());
559
          }
560
        }
561
      if( mMode==DEBUG )
562
        {
563
        sendDebug();
564
        receiveValues = false;
565
        mRunning = false;
566
        }
567
      }
568
    catch( Exception e )
569
      {
570
      if( mReceiver!=null ) mReceiver.message("Exception downloading records: "+e.getMessage() );
571
      }
572

    
573
    if( mRunning )
574
      {
575
      receiveValues = fillValues();
576
      mRunning = false;
577
      }
578

    
579
    if( receiveValues )
580
      {
581
      if( mReceiver!=null ) mReceiver.receive(mCountry, mName, mTime);
582

    
583
      if( mMode==SUBMIT )
584
        {
585
        RubikScores.getInstance().successfulSubmit();
586
        }
587
      }
588
    }
589

    
590
///////////////////////////////////////////////////////////////////////////////////////////////////
591

    
592
  private RubikNetwork()
593
    {
594

    
595
    }
596

    
597
///////////////////////////////////////////////////////////////////////////////////////////////////
598
// PUBLIC API
599
///////////////////////////////////////////////////////////////////////////////////////////////////
600

    
601
  public static void onPause()
602
    {
603
    mRunning = false;
604
    }
605

    
606
///////////////////////////////////////////////////////////////////////////////////////////////////
607

    
608
  public static RubikNetwork getInstance()
609
    {
610
    if( mThis==null )
611
      {
612
      mThis = new RubikNetwork();
613
      }
614

    
615
    return mThis;
616
    }
617

    
618
///////////////////////////////////////////////////////////////////////////////////////////////////
619

    
620
  private void start(Receiver receiver, Activity act, int mode)
621
    {
622
    mReceiver = receiver;
623
    mMode     = mode;
624

    
625
    try
626
      {
627
      PackageInfo pInfo = act.getPackageManager().getPackageInfo( act.getPackageName(), 0);
628
      mVersion = pInfo.versionName;
629
      }
630
    catch (PackageManager.NameNotFoundException e)
631
      {
632
      mVersion = "0.9.2";
633
      }
634

    
635
    Thread networkThrd = new Thread(this);
636
    networkThrd.start();
637
    }
638

    
639
///////////////////////////////////////////////////////////////////////////////////////////////////
640

    
641
  public void download(Receiver receiver, FragmentActivity act)
642
    {
643
    start(receiver, act, DOWNLOAD);
644
    }
645

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

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

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

    
655
  public void debug(AppCompatActivity act)
656
    {
657
    start(null, act, DEBUG);
658
    }
659
}
(1-1/2)