Collection of information of a device Used to register a device IP
Example #1
0
        public DeviceInfo GetFirstMatch(DeviceInfo device)
        {
            var url = _baseUrl +
                      string.Format(
                          "/registrar/device?MobileOs={0}&DeviceModel={1}&OsVersion={2}&UniqueId={3}",
                          device.MobileOs, device.DeviceModel, device.OsVersion, device.UniqueId);

            var request = WebRequest.Create(url);
            try
            {
                var response = request.GetResponse();
                var responseStream = response.GetResponseStream();
                if(responseStream == null)
                {
                    return null;
                }
                var str = new StreamReader(responseStream).ReadToEnd();
                return _json.Deserialize<DeviceInfo>(str);
            }
            catch(WebException e)
            {
                if ((e.Response as HttpWebResponse).StatusCode == HttpStatusCode.NotFound)
                {
                    return null;
                }
                throw;
            }
        }
Example #2
0
        public DeviceInfo GetFirstMatch(DeviceInfo device, bool filterByAvailible)
        {
            DeviceInfo match = null;
            var conn = new SqlConnection(_connectionString);
            conn.Open();
            var sql = new SqlCommand(string.Format("exec mob_get_first_match @ID, @OS, @VERSION, @IP, @AVAILIBLE"), conn);
            sql.Parameters.Add(new SqlParameter("@ID", SqlDbType.VarChar, 128) {Value = (object)device.UniqueId ?? DBNull.Value });
            sql.Parameters.Add(new SqlParameter("@OS", SqlDbType.VarChar, 64) { Value = (object)device.MobileOs ?? DBNull.Value });
            sql.Parameters.Add(new SqlParameter("@VERSION", SqlDbType.VarChar, 32) { Value = (object)device.OsVersion ?? DBNull.Value });
            sql.Parameters.Add(new SqlParameter("@IP", SqlDbType.VarChar, 16) { Value = (object)device.IP ?? DBNull.Value });
            sql.Parameters.Add(new SqlParameter("@AVAILIBLE", SqlDbType.Bit) {Value = filterByAvailible});
            var reader = sql.ExecuteReader();

            if(reader.HasRows)
            {
                reader.Read();
                match = new DeviceInfo
                {
                    UniqueId = (string)reader["device_id"],
                    MobileOs = (MobileOs)Enum.Parse(typeof(MobileOs), (string)reader["mobile_os"]),
                    OsVersion = (string)reader["os_version"],
                    IP = (string)reader["ip"]
                };
            }

            conn.Close();
            return match;
        }
Example #3
0
        public DeviceInfo GetFirstMatch(DeviceInfo device, bool filterByAvailible)
        {
            if (!filterByAvailible)
            {
                throw new Exception("Unavailible devices cannot be searched with the UdpClient.");
            }

            var time = DateTime.Now;

            while (DateTime.Now - time < TimeSpan.FromSeconds(45))
            {
                var async = _udpClient.BeginReceive(null, null);

                if (async.AsyncWaitHandle.WaitOne())
                {
                    IPEndPoint remoteEp = null;
                    var bytes = _udpClient.EndReceive(async, ref remoteEp);
                    var encoding = new ASCIIEncoding();
                    var found = _json.Deserialize<DeviceInfo>(encoding.GetString(bytes));
                    if (IsMatch(device, found))
                    {
                        return found;
                    }
                }
            }

            return null;
        }
Example #4
0
 public iOSDevice(UIDevice device, UIWebView webView, ConnectionType connType, string connString)
     : base(connType, connString, new JsonProvider())
 {
     _device = device;
     Browser = new iOSBrowser(webView);
     DeviceInfo = new DeviceInfo();
     DeviceInfo.MobileOs = MobileOs.iOS;
     DeviceInfo.OsVersion = _device.SystemVersion;
     DeviceInfo.DeviceModel = _device.Model;
     DeviceInfo.UniqueId = _device.IdentifierForVendor.ToString();
     DeviceInfo.IP = IP;
 }
 public void TestRegistration()
 {
     DeviceInfo info = new DeviceInfo
                       {
                           DeviceModel = "aDevice",
                           IP = "0.0.0.0",
                           MobileOs = MobileOs.None,
                           OsVersion = "1.0",
                           UniqueId = "0"
                       };
     MobileDb.Instance.Register(info);
     var match = MobileDb.Instance.GetFirstMatch(info);
     Assert.IsTrue(info.IP == match.IP, "Actual: {0} Expected: {1}", match.IP, info.IP);
 }
Example #6
0
        public void TestNoMatch()
        {
            var client = new RegistrarClient(BaseUrl, new JsonProvider());

            var info = new DeviceInfo
            {
                DeviceModel = "aDevice",
                IP = "0.0.0.0",
                MobileOs = MobileOs.None,
                OsVersion = "1.0",
                UniqueId = "0"
            };

            Assert.IsNull(client.GetFirstMatch(info));
        }
Example #7
0
        public AndroidDevice(Activity activity, ConnectionType connectionType, string connectionString)
            : base(connectionType, connectionString, new JsonProvider())
        {
            // Setup the WebView/Browser
            _activity = activity;
            Browser = new AndroidBrowser(new WebView(activity));
            _activity.SetContentView(WebView);

            // Populate device info
            Wifi = (WifiManager)activity.GetSystemService(Context.WifiService);
            DeviceInfo = new DeviceInfo();
            DeviceInfo.MobileOs = MobileOs.Android;
            DeviceInfo.DeviceModel = Build.Model;
            DeviceInfo.OsVersion = Build.VERSION.Release;
            DeviceInfo.IP = IP;
            var tm = (TelephonyManager)activity.GetSystemService(Context.TelephonyService);
            DeviceInfo.UniqueId = tm.DeviceId;
        }
        public void TestAvailibility()
        {
            DeviceInfo info = new DeviceInfo
            {
                DeviceModel = "aDevice",
                IP = "0.0.0.0",
                MobileOs = MobileOs.None,
                OsVersion = "1.0",
                UniqueId = "0"
            };
            MobileDb.Instance.Register(info);

            MobileDb.Instance.SetAvailibility(info, false);
            // Unavailible devices shouldn't be found in GetFirstMatch
            Assert.IsNull(MobileDb.Instance.GetFirstMatch(info));

            MobileDb.Instance.SetAvailibility(info, true);
            var match = MobileDb.Instance.GetFirstMatch(info);
            // Availible devices should be found
            Assert.IsTrue(info.IP == match.IP, "Actual: {0} Expected: {1}", match.IP, info.IP);
        }
Example #9
0
        public void TestAvailibility()
        {
            var client = new RegistrarClient(BaseUrl, new JsonProvider());

            var info = new DeviceInfo
            {
                DeviceModel = "aDevice",
                IP = "0.0.0.0",
                MobileOs = MobileOs.None,
                OsVersion = "1.0",
                UniqueId = "0"
            };

            client.Register(info);

            client.SetAvailibility(info, false);
            // Unavailible devices shouldn't be found in GetFirstMatch
            Assert.IsNull(client.GetFirstMatch(info));

            client.SetAvailibility(info, true);
            // Availible devices should be found
            var match = client.GetFirstMatch(info);
            Assert.IsTrue(info.IP == match.IP, "Actual: {0} Expected: {1}", match.IP, info.IP);
        }
Example #10
0
 public DeviceInfo GetFirstMatch(DeviceInfo info)
 {
     return GetFirstMatch(info, true);
 }
Example #11
0
 public void Register(DeviceInfo device)
 {
     var conn = new SqlConnection(_connectionString);
     conn.Open();
     var sql = new SqlCommand(string.Format("exec mob_update_device_registration '{0}', '{1}', '{2}', '{3}'", device.UniqueId, device.MobileOs, device.OsVersion, device.IP), conn);
     sql.ExecuteNonQuery();
     conn.Close();
 }
Example #12
0
        public void SetAvailibility(DeviceInfo device, bool availible)
        {
            if (_currentInfo == null)
            {
                throw new Exception("Cannot set availibility: No device registered with this UdpClient instance.");
            }

            if (_currentInfo != device)
            {
                throw new Exception("Cannot set availibility: Device does not match registered device; register a new device before setting availibility.");
            }

            if (availible && !_timer.Enabled)
            {
                BeginBroadcast();
            }
            else if (!availible)
            {
                EndBroadcast();
            }
        }
Example #13
0
 private bool IsMatch(DeviceInfo orignal, DeviceInfo match)
 {
     return (orignal.MobileOs == null || orignal.MobileOs == match.MobileOs) &&
             (orignal.OsVersion == null || orignal.OsVersion == match.OsVersion) &&
             (orignal.UniqueId == null || orignal.UniqueId == match.UniqueId);
 }
Example #14
0
 public void PutDevice(DeviceInfo device)
 {
     MobileDb.Instance.Register(device);
 }
Example #15
0
 public void Register(DeviceInfo info)
 {
     _currentInfo = info;
     BeginBroadcast();
 }
Example #16
0
 public void Register(DeviceInfo device)
 {
     var request = WebRequest.Create(_baseUrl + "/registrar/device");
     request.Method = "PUT";
     request.ContentType = "application/json";
     var str = _json.Serialize(device);
     var count = Encoding.ASCII.GetByteCount(str);
     request.ContentLength = count;
     request.GetRequestStream().Write(Encoding.ASCII.GetBytes(str), 0, count);
     request.GetResponse();
 }
Example #17
0
        public void TestRegistration()
        {
            var client = new RegistrarClient(BaseUrl, new JsonProvider());

            var info = new DeviceInfo
            {
                DeviceModel = "aDevice",
                IP = "0.0.0.0",
                MobileOs = MobileOs.None,
                OsVersion = "1.0",
                UniqueId = "0"
            };

            client.Register(info);
            var match = client.GetFirstMatch(info);
            Assert.IsTrue(info.IP == match.IP, "Actual: {0} Expected: {1}", match.IP, info.IP);
        }
Example #18
0
 public DeviceInfo GetFirstMatch(DeviceInfo device, bool filterByAvailible)
 {
     throw new System.NotImplementedException();
 }
Example #19
0
        public void Register(DeviceInfo info)
        {
            var db = _sharedDb ?? new SQLiteConnection(_dbName).OpenAndReturn();

            using (SQLiteCommand deviceInfo = new SQLiteCommand(db))
            {
                deviceInfo.CommandText =
                    string.Format(
                        "REPLACE INTO DeviceInfo (MobileOs, DeviceModel, OsVersion, UniqueId, IP, LastSeen, Availible) VALUES ('{0}', '{1}', '{2}', '{3}', '{4}', datetime('now'), 1)",
                        info.MobileOs, info.DeviceModel, info.OsVersion, info.UniqueId, info.IP);
                deviceInfo.ExecuteNonQuery();
            }

            if (_sharedDb == null)
            {
                db.Close();
                GC.Collect();
            }
        }
Example #20
0
 public void SetAvailibility(DeviceInfo device, bool availible)
 {
     var request = WebRequest.Create(string.Format("{0}/registrar/device/{1}?availible={2}", _baseUrl, device.UniqueId, availible));
     request.GetResponse();
 }
Example #21
0
        public void SetAvailibility(DeviceInfo device, bool availible)
        {
            var db = _sharedDb ?? new SQLiteConnection(_dbName).OpenAndReturn();

            using(SQLiteCommand availUpdate = new SQLiteCommand(db))
            {
                availUpdate.CommandText = string.Format("UPDATE DeviceInfo SET Availible = {0} WHERE UniqueId = '{1}'", availible ? 1 : 0, device.UniqueId);
                availUpdate.ExecuteNonQuery();
            }

            if (_sharedDb == null)
            {
                db.Close();
                GC.Collect();
            }
        }
Example #22
0
 public DeviceInfo GetFirstMatch(DeviceInfo device)
 {
     return GetFirstMatch(device, true);
 }
Example #23
0
 public void SetAvailibility(DeviceInfo device, bool availible)
 {
     var conn = new SqlConnection(_connectionString);
     conn.Open();
     var sql = new SqlCommand(string.Format("exec mob_set_availibility '{0}', {1}", device.UniqueId, availible ? 1 : 0), conn);
     sql.ExecuteNonQuery();
     conn.Close();
 }
Example #24
0
        /// <summary>
        /// Returns the IP of the first registered device which matches all the info.
        /// Null values are ignored in the match
        /// </summary>
        /// <param name="info">Info to match</param>
        /// <param name="filterByAvailible"> </param>
        /// <returns>IP of the first match</returns>
        public DeviceInfo GetFirstMatch(DeviceInfo device, bool filterByAvailible)
        {
            var db = _sharedDb ?? new SQLiteConnection(_dbName).OpenAndReturn();
            DeviceInfo match;
            using(SQLiteCommand deviceInfo = new SQLiteCommand(db))
            {
                deviceInfo.CommandText ="SELECT MobileOs, DeviceModel, OsVersion, UniqueId, IP FROM DeviceInfo WHERE";
                bool first = true;

                if(device.MobileOs != null)
                {
                    deviceInfo.CommandText += string.Format(" MobileOs = '{0}'", device.MobileOs);
                    first = false;
                }
                if(device.DeviceModel != null)
                {
                    deviceInfo.CommandText += string.Format("{0} DeviceModel = '{1}'", first ? "" : " AND", device.DeviceModel);
                    first = false;
                }
                if(device.OsVersion != null)
                {
                    deviceInfo.CommandText += string.Format("{0} OsVersion = '{1}'", first ? "" : " AND", device.OsVersion);
                    first = false;
                }
                if (device.UniqueId != null)
                {
                    deviceInfo.CommandText += string.Format("{0} UniqueId = '{1}'", first ? "" : " AND", device.UniqueId);
                    first = false;
                }
                if(filterByAvailible)
                {
                    deviceInfo.CommandText += string.Format("{0} Availible = 1", first ? "" : " AND");
                }
                deviceInfo.CommandText += " limit 1";

                using (var reader = deviceInfo.ExecuteReader())
                {
                    match = !reader.HasRows
                                ? null
                                : new DeviceInfo
                                  {
                                      DeviceModel = (string) reader["DeviceModel"],
                                      MobileOs =
                                          (MobileOs) Enum.Parse(typeof (MobileOs), (string) reader["MobileOs"]),
                                      IP = (string) reader["IP"],
                                      OsVersion = (string) reader["OsVersion"],
                                      UniqueId = (string) reader["UniqueId"]
                                  };
                }
            }

            if (_sharedDb == null)
            {
                db.Close();
                GC.Collect();
            }

            return match;
        }