/// <summary> /// インスタレーション情報をNebulaサーバから取得する /// </summary> /// <returns>NbSsePushInstallation</returns> /// <exception cref="InvalidOperationException">インスタレーションのパラメータ未設定、インスタレーション情報の保存・削除失敗</exception> /// <exception cref="NbHttpException">リクエスト送信失敗</exception> public static async Task <NbSsePushInstallation> RefreshCurrentInstallation() { NbSsePushInstallation installation = NbSsePushInstallation.GetCurrentInstallation(); NbUtil.NotNullWithInvalidOperation(installation.InstallationId, "Null installationId"); try { // リクエスト生成&送信 var json = await installation.SendRequestForRefresh(); // ストレージに保存 installation.SaveAndLoad(json); } catch (NbHttpException e) { // 該当するインスタレーションが存在しない場合 if (e.StatusCode == HttpStatusCode.NotFound) { // ストレージ内のインスタレーション情報を削除 installation.DeleteAndLoad(); } throw e; } return(installation); }
/// <summary> /// SSEサーバと接続する /// </summary> /// <exception cref="NbHttpException">リクエスト送信失敗</exception> /// <exception cref="InvalidOperationException">インスタレーションが存在しない</exception> public void Connect() { Debug.WriteLine("NbSsePushReceiveClient: Connect() <start>"); if (GetState() != State.Idle) { Debug.WriteLine("NbSsePushReceiveClient: Connect() State is not idle. state=" + GetState()); throw new NbHttpException(HttpStatusCode.BadRequest, "Connection is already being processed."); } // インスタレーション情報取得 NbSsePushInstallation installation = LoadCurrentInstallation(); // SSE情報チェック if (!HasSseInfo(installation)) { Debug.WriteLine("NbSsePushReceiveClient: Connect() SSE Infomation doesn't exist in Installation."); throw new NbHttpException(HttpStatusCode.BadRequest, "SSE Infomation doesn't exist in Installation."); } // ネットワーク接続状態検知イベント登録 RegisterNetworkChangedEvent(); // SSEサーバと接続 ConnectToPushClient(); Debug.WriteLine("NbSsePushReceiveClient: Connect() <end>"); }
// インスタレーション内にSSE情報(username, password, uri)が存在するかどうかチェック private bool HasSseInfo(NbSsePushInstallation installation) { if (!string.IsNullOrEmpty(installation.Username) && !string.IsNullOrEmpty(installation.Password) && !string.IsNullOrEmpty(installation.Uri)) { return(true); } return(false); }
/// <summary> /// 現在のSSE Pushインスタレーションを取得する /// </summary> /// <returns>NbSsePushInstallation</returns> public static NbSsePushInstallation GetCurrentInstallation() { lock (_lock) { if (_sInstance == null) { _sInstance = new NbSsePushInstallation(); } } return(_sInstance); }
// インスタレーション情報(SSE情報含む)が存在するかどうかチェック private bool IsInstallationExists() { NbSsePushInstallation installation = NbSsePushInstallation.GetCurrentInstallation(); if (!string.IsNullOrEmpty(installation.InstallationId) && HasSseInfo(installation)) { return(true); } return(false); }
// 端末ストレージに保存されているインスタレーション情報を取得し、SSE情報を保持する private NbSsePushInstallation LoadCurrentInstallation() { NbSsePushInstallation installation = NbSsePushInstallation.GetCurrentInstallation(); NbUtil.NotNullWithInvalidOperation(installation.InstallationId, "Null installationId"); // SSE情報を保持 this._username = installation.Username; this._password = installation.Password; this._uri = installation.Uri; return(installation); }
// 自動回復処理 internal bool AutoRecovery(HttpStatusCode statusCode, HttpWebResponse response) { try { // 排他ロック AcquireLock(); } catch (InvalidOperationException) { Debug.WriteLine("NbSsePushReceiveClient: Auto recovery fails because of lock"); return(false); } // インスタレーション(SSE情報含む)の存在チェック if (!IsInstallationExists()) { Debug.WriteLine("NbSsePushReceiveClient: Auto recovery fails because Installation doesn't exist."); ReleaseLock(); return(false); } // 自動回復処理を実施する try { // インスタレーション再登録 var installation = NbSsePushInstallation.GetCurrentInstallation().Save().Result; ReleaseLock(); // SSEサーバへ再接続 _sseClient.Open(installation.Username, installation.Password); return(true); } // Save()失敗時の処理。Open()のエラーはデリゲート関数で通知される。 catch (Exception) { Debug.WriteLine("NbSsePushReceiveClient: Auto recovery fails because Installation can't be Registered."); ReleaseLock(); return(false); } }