Example #1
0
        private void NotifyCloudOfExternalPort(object nil)
        {
            logger.InfoFormat("Notify public port info to cloud: ext ip {0}, ext port {1} via heartbeat",
                              this.externalIP, this.externalPort);

            try
            {
                Model.StationInfo stationInfo = Model.StationCollection.Instance.FindOne();
                if (stationInfo == null)
                {
                    return;
                }

                Cloud.StationApi api = new Cloud.StationApi(stationInfo.Id, stationInfo.SessionToken);

                Cloud.StationDetail detail = StatusChecker.GetDetail();

                logger.Debug("detail: " + detail.ToFastJSON());

                api.Heartbeat(new WebClient(), detail);
            }
            catch (Exception e)
            {
                logger.Warn("Unable to notify external ip/port to cloud", e);
            }
        }
Example #2
0
 public void Stop()
 {
     timer.Change(Timeout.Infinite, Timeout.Infinite);
     using (WebClient agent = new WebClient())
     {
         Model.StationInfo sinfo = Model.StationCollection.Instance.FindOne();
         if (sinfo != null)
         {
             try
             {
                 Cloud.StationApi api = new Cloud.StationApi(sinfo.Id, sinfo.SessionToken);
                 api.Offline(agent);
             }
             catch (Exception ex)
             {
                 logger.Warn("cloud offline error", ex);
             }
         }
     }
 }
Example #3
0
        private static void LogOutStationFromCloud()
        {
            try
            {
                Model.StationInfo stationInfo = Model.StationCollection.Instance.FindOne();
                if (stationInfo == null)
                {
                    throw new InvalidOperationException("station is null in station collection");
                }

                logger.DebugFormat("Station logout with stationId = {0}", stationInfo.Id);
                Cloud.StationApi stationApi = new Cloud.StationApi(stationInfo.Id, stationInfo.SessionToken);
                stationApi.Offline(new System.Net.WebClient());

                logger.Debug("Station logout successfully");
            }
            catch (Exception ex)
            {
                logger.Debug("Unable to logout station", ex);
            }
        }
Example #4
0
        private void SendHeartbeat(Object obj)
        {
            StationDetail detail = GetDetail();

            Model.StationInfo sinfo = Model.StationCollection.Instance.FindOne();
            if (sinfo != null)
            {
                bool   locChange = false;
                string baseurl   = NetworkHelper.GetBaseURL();
                if (baseurl != sinfo.Location)
                {
                    // update location if baseurl changed
                    logger.DebugFormat("station location changed: {0}", baseurl);
                    sinfo.Location = baseurl;
                    locChange      = true;
                }

                try
                {
                    WebClient        agent = new WebClient();
                    Cloud.StationApi api   = new Cloud.StationApi(sinfo.Id, sinfo.SessionToken);
                    if (logon == false || DateTime.Now - sinfo.LastLogOn > TimeSpan.FromDays(1))
                    {
                        logger.Debug("cloud logon start");
                        api.LogOn(agent, detail);
                        logon = true;

                        // update station info in database
                        logger.Debug("update station information");
                        sinfo.LastLogOn = DateTime.Now;
                        Model.StationCollection.Instance.Save(sinfo);
                    }

                    if (locChange)
                    {
                        // update station info in database
                        logger.Debug("update station information");
                        Model.StationCollection.Instance.Save(sinfo);
                    }
                    api.Heartbeat(agent, detail);
                }
                catch (WammerCloudException ex)
                {
                    WebException webex = (WebException)ex.InnerException;
                    if (webex != null)
                    {
                        HttpWebResponse response = (HttpWebResponse)webex.Response;
                        if (response != null)
                        {
                            if (response.StatusCode == HttpStatusCode.Unauthorized)
                            {
                                // station's session token expired, it might be caused by:
                                // 1. server maintenance
                                // 2. driver registered another station
                                // in this situation, client has to re-login/re-register the station
                                functionServer.BlockAuth(true);
                            }
                        }
                    }
                    logger.Warn("cloud send heartbeat error", ex);
                }
                catch (Exception ex)
                {
                    logger.Warn("cloud send heartbeat error", ex);
                }
            }
        }