Exemple #1
0
        public async Task StartAsync()
        {
            if (_alive)
            {
                return;
            }
            _alive = true;
            _stop  = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously);
            var lastStartTime = DateTime.Now;

            _work = Task.Run(async() =>
            {
                while (!_stop.Task.IsCompleted)
                {
                    var now = DateTime.Now;
                    if (lastStartTime.Minute != now.Minute && now.Minute % _minutes == 0)
                    {
                        lastStartTime = now;
                        _             = Task.Run(async() =>
                        {
                            await Task.Delay(_offset);
                            OnTime?.Invoke(new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, 0));
                        });
                    }
                    if (await Task.WhenAny(Task.Delay(900), _stop.Task) == _stop.Task)
                    {
                        break;
                    }
                }
            });
            await Task.CompletedTask;
        }
        public void SetOnTime(OnTime onTime)
        {
            OnTime = onTime;

            // set OnTime host label
            OnTimeHostLabel.Text = new Uri(OnTime.Settings.OnTimeUrl).Host;

            GetProjects();
            if(Projects.Count == 0)
            {
                // there are no projects. ask the user if they want to create a new project.
                var dialogResult = MessageBox.Show(
                    "Your OnTime database has no projects. To use this example, you will need to create a project. Would you like to create a new project called \"API Example Project\" now?",
                    "Create an OnTime project",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question);

                if(dialogResult == DialogResult.Yes)
                {
                    var project = new Project
                    {
                        name = "API Example Project"
                    };

                    OnTime.Post<DataResponse<Project>>("v1/projects", project);
                    GetProjects();
                }
                else
                {
                    Application.Exit();
                    return;
                }
            }
            GetItems();
        }
Exemple #3
0
        public Reservation Set(long time, OnTime onTime, OnCancel onCancel = null,
                               Reservation.IHandle handler = null, IAlarmPayload payload = null)
        {
            Dev.Assert(onTime != null, "Cannot set timer with null onTime delegate");

            if (handler?.Reservation != null)
            {
                return(null);
            }

            Reservation reservation = _reservationPool.Alloc();

            reservation.Time     = LogicTime.TIME() + time;
            reservation.OnTime   = onTime;
            reservation.OnCancel = onCancel;
            reservation.Handler  = handler;
            reservation.Payload  = payload;

            if (handler != null)
            {
                Dev.Assert(handler.Reservation == null, "Alarm reservation already used");
                reservation.Handler.Reservation = reservation;
            }

            _reservations.Add(reservation.Time, reservation);

            return(reservation);
        }
Exemple #4
0
 /// <summary>
 /// 添加消息分发
 /// </summary>
 /// <param name="reader"></param>
 private static void addReader(DistributionFileReader reader)
 {
     if (readerCount == readers.Length)
     {
         readers = readers.copyNew(Math.Max(readerCount << 1, sizeof(int)));
     }
     reader.readerIndex     = readerCount;
     readers[readerCount++] = reader;
     OnTime.Set(Date.NowTime.OnTimeFlag.CacheDistributionTimeout);
 }
Exemple #5
0
        public Reservation Set(OnTime onTime)
        {
            Dev.Assert(onTime != null, "Cannot set timer with null onTime delegate");

            Reservation reservation = this._reservationPool.Alloc();

            Dev.Assert(!_updates.ContainsKey(reservation.Index), "Already exist reservation");

            reservation.OnTime = onTime;
            _willadd.Enqueue(reservation);

            return(reservation);
        }
        public virtual int _GetUniqueIdentifier()
        {
            var hashCode = 399326290;

            hashCode = hashCode * -1521134295 + (Id?.GetHashCode() ?? 0);
            hashCode = hashCode * -1521134295 + (OnTime?.GetHashCode() ?? 0);
            hashCode = hashCode * -1521134295 + (InFull?.GetHashCode() ?? 0);
            hashCode = hashCode * -1521134295 + (AsOrdered?.GetHashCode() ?? 0);
            hashCode = hashCode * -1521134295 + (Impeachment?.GetHashCode() ?? 0);
            hashCode = hashCode * -1521134295 + (TotalDelivered?.GetHashCode() ?? 0);
            hashCode = hashCode * -1521134295 + (Supplier?.GetHashCode() ?? 0);
            return(hashCode);
        }
        private void LoginButton_Click(object sender, EventArgs e)
        {
            var OnTime = new OnTime(Program.Settings);
            try
            {
                OnTime.ObtainAccessTokenFromUsernamePassword(
                    username: LoginIdText.Text,
                    password: PasswordText.Text,
                    scope: "read write"
                );
            } catch (OnTimeException ex)
            {
                MessageBox.Show(
                    "An error occurred when obtaining access token from OnTime: " + ex.Message,
                    "Error obtaining access token",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
            }

            if(OnTime.HasAccessToken())
                LoggedIn(this, new LoginEventArgs(OnTime));
        }
Exemple #8
0
 public void OnFree()
 {
     this.OnTime = null;
 }
 public void RegisterCallback(object owner, OnTime callback)
 {
     delegates.Add(owner, callback);
 }
 public LoginEventArgs(OnTime onTime)
 {
     OnTime = onTime;
 }
Exemple #11
0
 public IntervalTrigger(TimeSpan time, OnTime eventFunction, object eventArgument)
 {
     this.time = time;
     this.eventFunction = eventFunction;
     this.eventArgument = eventArgument;
     this.timeSinceLastTrigger = new TimeSpan();
 }
Exemple #12
0
 public EventTrigger(TimeSpan time, OnTime eventFunction, object eventArgument)
 {
     this.time = time;
     this.eventFunction = eventFunction;
     this.eventArgument = eventArgument;
 }
Exemple #13
0
 public void AddIntervalEvent(TimeSpan interval, OnTime specificEvent, object arguement)
 {
     intervalEvents.Add(new IntervalTrigger(interval, specificEvent, arguement));
 }
Exemple #14
0
 public void AddEvent(TimeSpan eventTime, OnTime specificEvent)
 {
     AddEvent(eventTime, specificEvent, null);
 }
Exemple #15
0
 public void AddEvent(TimeSpan eventTime, OnTime specificEvent, object argument)
 {
     events.Add(new EventTrigger(eventTime, specificEvent, argument));
 }
Exemple #16
0
        public void ParseReply(string[] tokens)
        {
            ReplyCode code = (ReplyCode)int.Parse(tokens[1], CultureInfo.InvariantCulture);

            tokens[3] = RemoveLeadingColon(tokens[3]);
            switch (code)
            {
            //Messages sent upon successful registration
            case ReplyCode.RPL_WELCOME:
            case ReplyCode.RPL_YOURESERVICE:
                OnRegistered.Fire(this, new EventArgs());
                break;

            case ReplyCode.RPL_MOTDSTART:
            case ReplyCode.RPL_MOTD:
                OnMotd?.Invoke(CondenseStrings(tokens, 3), false);
                break;

            case ReplyCode.RPL_ENDOFMOTD:
                OnMotd?.Invoke(CondenseStrings(tokens, 3), true);
                break;

            case ReplyCode.RPL_ISON:
                OnIson?.Invoke(tokens[3]);
                break;

            case ReplyCode.RPL_NAMREPLY:
                ProcessNamesReply(tokens, false);
                break;

            case ReplyCode.RPL_ENDOFNAMES:
                ProcessNamesReply(tokens, true);
                break;

            case ReplyCode.RPL_LIST:
                tokens[5] = RemoveLeadingColon(tokens[5]);
                OnList?.Invoke(
                    tokens[3],
                    int.Parse(tokens[4], CultureInfo.InvariantCulture),
                    CondenseStrings(tokens, 5),
                    false);
                break;

            case ReplyCode.RPL_LISTEND:
                OnList?.Invoke("", 0, "", true);
                break;

            case ReplyCode.ERR_NICKNAMEINUSE:
            case ReplyCode.ERR_NICKCOLLISION:
                tokens[4] = RemoveLeadingColon(tokens[4]);
                OnNickError.Fire(this, new NickErrorEventArgs(tokens[3], CondenseStrings(tokens, 4)));
                //Trace.WriteLine("Nick collision", "IRC");
                break;

            case ReplyCode.RPL_NOTOPIC:
                OnError.Fire(this, new ErrorMessageEventArgs(code, CondenseStrings(tokens, 3)));
                break;

            case ReplyCode.RPL_TOPIC:
                tokens[4] = RemoveLeadingColon(tokens[4]);
                OnRecieveTopic?.Invoke(tokens[3], CondenseStrings(tokens, 4));
                break;

            case ReplyCode.RPL_INVITING:
                OnInviteSent.Fire(this, new InviteEventArgs(tokens[3], tokens[4]));
                break;

            case ReplyCode.RPL_AWAY:
                OnAway.Fire(this, new AwayEventArgs(tokens[3], RemoveLeadingColon(CondenseStrings(tokens, 4))));
                break;

            case ReplyCode.RPL_WHOREPLY:
                User user = new User(tokens[7], tokens[4], tokens[5]);
                OnWho?.Invoke(
                    user,
                    tokens[3],
                    tokens[6],
                    tokens[8],
                    int.Parse(RemoveLeadingColon(tokens[9]), CultureInfo.InvariantCulture),
                    tokens[10],
                    false);
                break;

            case ReplyCode.RPL_ENDOFWHO:
                OnWho?.Invoke(User.Empty, "", "", "", 0, "", true);
                break;

            case ReplyCode.RPL_WHOISUSER:
                User      whoUser   = new User(tokens[3], tokens[4], tokens[5]);
                WhoisInfo whoisInfo = LookupInfo(whoUser.Nick);
                whoisInfo.user     = whoUser;
                tokens[7]          = RemoveLeadingColon(tokens[7]);
                whoisInfo.realName = CondenseStrings(tokens, 7);
                break;

            case ReplyCode.RPL_WHOISCHANNELS:
                WhoisInfo whoisChannelInfo = LookupInfo(tokens[3]);
                tokens[4] = RemoveLeadingColon(tokens[4]);
                int      numberOfChannels = tokens.Length - 4;
                string[] channels         = new String[numberOfChannels];
                Array.Copy(tokens, 4, channels, 0, numberOfChannels);
                whoisChannelInfo.SetChannels(channels);
                break;

            case ReplyCode.RPL_WHOISSERVER:
                WhoisInfo whoisServerInfo = LookupInfo(tokens[3]);
                whoisServerInfo.ircServer = tokens[4];
                tokens[5] = RemoveLeadingColon(tokens[5]);
                whoisServerInfo.serverDescription = CondenseStrings(tokens, 5);
                break;

            case ReplyCode.RPL_WHOISOPERATOR:
                WhoisInfo whoisOpInfo = LookupInfo(tokens[3]);
                whoisOpInfo.isOperator = true;
                break;

            case ReplyCode.RPL_WHOISIDLE:
                WhoisInfo whoisIdleInfo = LookupInfo(tokens[3]);
                whoisIdleInfo.idleTime = long.Parse(tokens[5], CultureInfo.InvariantCulture);
                break;

            case ReplyCode.RPL_ENDOFWHOIS:
                string    nick         = tokens[3];
                WhoisInfo whoisEndInfo = LookupInfo(nick);
                OnWhois?.Invoke(whoisEndInfo);
                whoisInfos.Remove(nick);
                break;

            case ReplyCode.RPL_WHOWASUSER:
                User whoWasUser = new User(tokens[3], tokens[4], tokens[5]);
                tokens[7] = RemoveLeadingColon(tokens[7]);
                OnWhowas?.Invoke(whoWasUser, CondenseStrings(tokens, 7), false);
                break;

            case ReplyCode.RPL_ENDOFWHOWAS:
                OnWhowas?.Invoke(User.Empty, "", true);
                break;

            case ReplyCode.RPL_UMODEIS:
            {
                //First drop the '+'
                string     chars = tokens[3].Substring(1);
                UserMode[] modes = Rfc2812Util.UserModesToArray(chars);
                OnUserModeRequest?.Invoke(modes);
            }
            break;

            case ReplyCode.RPL_CHANNELMODEIS:
                try
                {
                    ChannelModeInfo[] modes = ChannelModeInfo.ParseModes(tokens, 4);
                    OnChannelModeRequest?.Invoke(tokens[3], modes);
                }
                catch (Exception)
                {
                    OnError.Fire(this, new ErrorMessageEventArgs(ReplyCode.UnparseableMessage, CondenseStrings(tokens, 0)));
                    Debug.WriteLineIf(Rfc2812Util.IrcTrace.TraceWarning, "[" + Thread.CurrentThread.Name + "] Listener::ParseReply() Bad IRC MODE string=" + tokens[0]);
                }
                break;

            case ReplyCode.RPL_BANLIST:
                OnChannelList?.Invoke(tokens[3], ChannelMode.Ban, tokens[4], Rfc2812Util.UserFromString(tokens[5]), Convert.ToInt64(tokens[6], CultureInfo.InvariantCulture), false);
                break;

            case ReplyCode.RPL_ENDOFBANLIST:
                OnChannelList?.Invoke(tokens[3], ChannelMode.Ban, "", User.Empty, 0, true);
                break;

            case ReplyCode.RPL_INVITELIST:
                OnChannelList?.Invoke(tokens[3], ChannelMode.Invitation, tokens[4], Rfc2812Util.UserFromString(tokens[5]), Convert.ToInt64(tokens[6]), false);
                break;

            case ReplyCode.RPL_ENDOFINVITELIST:
                OnChannelList?.Invoke(tokens[3], ChannelMode.Invitation, "", User.Empty, 0, true);
                break;

            case ReplyCode.RPL_EXCEPTLIST:
                OnChannelList?.Invoke(tokens[3], ChannelMode.Exception, tokens[4], Rfc2812Util.UserFromString(tokens[5]), Convert.ToInt64(tokens[6]), false);
                break;

            case ReplyCode.RPL_ENDOFEXCEPTLIST:
                OnChannelList?.Invoke(tokens[3], ChannelMode.Exception, "", User.Empty, 0, true);
                break;

            case ReplyCode.RPL_UNIQOPIS:
                OnChannelList?.Invoke(tokens[3], ChannelMode.ChannelCreator, tokens[4], User.Empty, 0, true);
                break;

            case ReplyCode.RPL_VERSION:
                OnVersion?.Invoke(CondenseStrings(tokens, 3));
                break;

            case ReplyCode.RPL_TIME:
                OnTime?.Invoke(CondenseStrings(tokens, 3));
                break;

            case ReplyCode.RPL_INFO:
                OnInfo?.Invoke(CondenseStrings(tokens, 3), false);
                break;

            case ReplyCode.RPL_ENDOFINFO:
                OnInfo?.Invoke(CondenseStrings(tokens, 3), true);
                break;

            case ReplyCode.RPL_ADMINME:
            case ReplyCode.RPL_ADMINLOC1:
            case ReplyCode.RPL_ADMINLOC2:
            case ReplyCode.RPL_ADMINEMAIL:
                OnAdmin?.Invoke(RemoveLeadingColon(CondenseStrings(tokens, 3)));
                break;

            case ReplyCode.RPL_LUSERCLIENT:
            case ReplyCode.RPL_LUSEROP:
            case ReplyCode.RPL_LUSERUNKNOWN:
            case ReplyCode.RPL_LUSERCHANNELS:
            case ReplyCode.RPL_LUSERME:
                OnLusers?.Invoke(RemoveLeadingColon(CondenseStrings(tokens, 3)));
                break;

            case ReplyCode.RPL_LINKS:
                OnLinks?.Invoke(tokens[3],                                                              //mask
                                tokens[4],                                                              //hostname
                                int.Parse(RemoveLeadingColon(tokens[5]), CultureInfo.InvariantCulture), //hopcount
                                CondenseStrings(tokens, 6), false);
                break;

            case ReplyCode.RPL_ENDOFLINKS:
                OnLinks?.Invoke(String.Empty, String.Empty, -1, String.Empty, true);
                break;

            case ReplyCode.RPL_STATSLINKINFO:
            case ReplyCode.RPL_STATSCOMMANDS:
            case ReplyCode.RPL_STATSUPTIME:
            case ReplyCode.RPL_STATSOLINE:
                OnStats?.Invoke(GetQueryType(code), RemoveLeadingColon(CondenseStrings(tokens, 3)), false);
                break;

            case ReplyCode.RPL_ENDOFSTATS:
                OnStats?.Invoke(Rfc2812Util.CharToStatsQuery(tokens[3][0]), RemoveLeadingColon(CondenseStrings(tokens, 4)), true);
                break;

            default:
                HandleDefaultReply(code, tokens);
                break;
            }
        }