AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
ManualIpConfiguration.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using System;
5 using System.Collections;
6 using UnityEngine;
7 using UnityEngine.UI;
8 
9 namespace HoloToolkit.Sharing.Utilities
10 {
14  public class ManualIpConfiguration : MonoBehaviour
15  {
20  private const int MaximumCharacterLength = 15;
21 
22  public string IpAddress { get { return ipAddress.text; } }
23 
27  [Tooltip("Hides the UI when connection is made.")]
28  public bool HideWhenConnected;
29 
33  [Range(0.1f, 5f)]
34  [Tooltip("Hides the UI after this many seconds.")]
35  public float HideAfterSeconds = 1f;
36 
40  [Range(1, 30)]
41  [Tooltip("How many seconds before server connection times out.")]
42  public int Timeout = 5;
43 
44  [SerializeField]
45  private Text ipAddress = null;
46 
47  [SerializeField]
48  private Image connectionIndicator = null;
49 
50  private bool timerRunning;
51 
52  private float timer;
53 
54  private bool isTryingToConnect;
55 
56  private bool firstRun;
57 
58  private void Awake()
59  {
60  ipAddress.text = PlayerPrefs.GetString("SharingServerIp", "Not Connected");
61  firstRun = true;
62  }
63 
64  private void Start()
65  {
66  if (SharingStage.Instance != null)
67  {
68  SharingStage.Instance.SharingManagerConnected += OnConnected;
69  SharingStage.Instance.SharingManagerDisconnected += OnDisconnected;
70  }
71  else
72  {
73  Debug.LogError("Unable to subscribe to Sharing Stage!");
74  }
75  }
76 
77  private void OnEnable()
78  {
79  if (firstRun)
80  {
81  firstRun = false;
82  isTryingToConnect = true;
83  ConnectToSharingService();
84  }
85  else
86  {
87  isTryingToConnect = false;
88  }
89  }
90 
91  private void Update()
92  {
93  if (timerRunning && timer - Time.time < 0)
94  {
95  if (isTryingToConnect)
96  {
97  isTryingToConnect = false;
98  OnDisconnected();
99 
100  PlayerPrefs.SetString("SharingServerIp", "Not Connected");
101  }
102  }
103  }
104 
105  private void OnDestroy()
106  {
107  if (SharingStage.Instance != null)
108  {
109  SharingStage.Instance.SharingManagerConnected -= OnConnected;
110  SharingStage.Instance.SharingManagerDisconnected -= OnDisconnected;
111  }
112  }
113 
114  private void CheckConnection()
115  {
116  // SharingStage should be valid at this point, but we may not be connected.
117  if (SharingStage.Instance.IsConnected)
118  {
119  OnConnected();
120  }
121  else if (!timerRunning)
122  {
123  timer = Time.time + Timeout;
124  timerRunning = true;
125  }
126  }
127 
128  private void OnConnected(object sender = null, EventArgs e = null)
129  {
130  timerRunning = false;
131  connectionIndicator.color = Color.green;
132  ipAddress.text = SharingStage.Instance.Connection.GetRemoteAddress().ToString();
133 
134  PlayerPrefs.SetString("SharingServerIp", ipAddress.text);
135 
136  if (HideWhenConnected && isTryingToConnect)
137  {
138  StartCoroutine(Hide());
139  }
140 
141  isTryingToConnect = false;
142  }
143 
144  private void OnDisconnected(object sender = null, EventArgs e = null)
145  {
146  timerRunning = false;
147 
148  if (!isTryingToConnect)
149  {
150  connectionIndicator.color = Color.red;
151  ipAddress.text = "Not Connected";
152  }
153  }
154 
155  public void InputString(string input)
156  {
157  timerRunning = false;
158  isTryingToConnect = false;
159 
160  if (ipAddress.text.Contains("Connected") || ipAddress.text.Contains("127.0.0.1"))
161  {
162  ipAddress.text = string.Empty;
163  }
164 
165  if (ipAddress.text.Length < MaximumCharacterLength)
166  {
167  ipAddress.text += input;
168  }
169  }
170 
171  public void DeleteLastCharacter()
172  {
173  timerRunning = false;
174  isTryingToConnect = false;
175 
176  if (!string.IsNullOrEmpty(ipAddress.text))
177  {
178  ipAddress.text = ipAddress.text.Substring(0, ipAddress.text.Length - 1);
179  }
180  }
181 
182  public void ClearIpAddressString()
183  {
184  timerRunning = false;
185  isTryingToConnect = false;
186 
187  ipAddress.text = "";
188  }
189 
191  {
192  timerRunning = false;
193  isTryingToConnect = false;
194 
195  if (SharingStage.Instance == null || ipAddress.text.Contains("Connected"))
196  {
197  OnDisconnected();
198  return;
199  }
200 
201  isTryingToConnect = true;
202  connectionIndicator.color = Color.yellow;
203  SharingStage.Instance.ConnectToServer(ipAddress.text, SharingStage.Instance.ServerPort);
204  CheckConnection();
205  }
206 
207  private IEnumerator Hide()
208  {
209  yield return new WaitForSeconds(HideAfterSeconds);
210 
211  gameObject.SetActive(false);
212  }
213  }
214 }
Utility for connecting to Sharing Service by IP Address from inside application at runtime...
static T Instance
Returns the Singleton instance of the classes type. If no instance is found, then we search for an in...
Definition: Singleton.cs:26
bool HideWhenConnected
Hides the UI when connection is made.
The SharingStage is in charge of managing the core networking layer for the application.
Definition: SharingStage.cs:14