1 package autotest.moblab.rpc;
2 
3 import com.google.gwt.json.client.JSONArray;
4 import com.google.gwt.json.client.JSONBoolean;
5 import com.google.gwt.json.client.JSONObject;
6 import com.google.gwt.json.client.JSONString;
7 import com.google.gwt.json.client.JSONValue;
8 
9 /**
10  * The network information RPC entity.
11  */
12 public class NetworkInfo extends JsonRpcEntity {
13   public static final String JSON_FIELD_IS_CONNECTED = "is_connected";
14   public static final String JSON_FIELD_SERVER_IPS = "server_ips";
15   private String[] serverIps;
16   private boolean connectedToGoogle;
17 
NetworkInfo()18   public NetworkInfo() {
19     this(new String[0], false);
20   }
21 
NetworkInfo(String[] serverIps, boolean connectedToGoogle)22   public NetworkInfo(String[] serverIps, boolean connectedToGoogle) {
23     reset();
24     if (serverIps != null) {
25       this.serverIps = serverIps;
26     }
27     this.connectedToGoogle = connectedToGoogle;
28   }
29 
getServerIps()30   public String[] getServerIps() {
31     return serverIps;
32   }
33 
isConnectedToGoogle()34   public boolean isConnectedToGoogle() {
35     return connectedToGoogle;
36   }
37 
reset()38   private void reset() {
39     serverIps = new String[0];
40     connectedToGoogle = false;
41   }
42 
43   @Override
fromJson(JSONObject object)44   public void fromJson(JSONObject object) {
45     reset();
46     if (object != null) {
47       JSONValue serverIpsObject = object.get(JSON_FIELD_SERVER_IPS);
48       if (serverIpsObject != null) {
49         JSONArray serverIpsArray = serverIpsObject.isArray();
50         serverIps = new String[serverIpsArray.size()];
51         for (int i = 0; i < serverIps.length; i++) {
52           serverIps[i] = serverIpsArray.get(i).isString().stringValue();
53         }
54       } else {
55         serverIps = new String[0];
56       }
57       JSONValue connectedObject = object.get(JSON_FIELD_IS_CONNECTED);
58       if (connectedObject != null) {
59         connectedToGoogle = connectedObject.isBoolean().booleanValue();
60       }
61     }
62   }
63 
64   @Override
toJson()65   public JSONObject toJson() {
66     JSONObject object = new JSONObject();
67     if (serverIps != null) {
68       JSONArray serverIpsArray = new JSONArray();
69       for (int index = 0; index < serverIps.length; index++) {
70         serverIpsArray.set(index, new JSONString(serverIps[index]));
71       }
72       object.put(JSON_FIELD_SERVER_IPS, serverIpsArray);
73     }
74     object.put(JSON_FIELD_IS_CONNECTED, JSONBoolean.getInstance(connectedToGoogle));
75     return object;
76   }
77 }
78