Пример #1
0
        public Task <AgentContext <RecipientState> > Run(object state, AgentCapability self, object message)
        {
            var agentState = state as RecipientState ?? new RecipientState();
            var context    = new AgentContext <RecipientState>(agentState, self);

            if (message is AbstractTrigger msg)
            {
                var pair = new AgentTriggerPair(msg.Sender, message.GetType());
                if (context.State.TriggerToAction.ContainsKey(pair))
                {
                    (IList <object>, IList <object>)result = RecipientState.TriggerCheck(context.State, pair, msg);

                    foreach (var action in result.Item1)
                    {
                        context.SendMessage(context.State.TokenManagerAgent, action, null);
                    }

                    foreach (var publication in result.Item2)
                    {
                        context.MakePublication(publication);
                    }

                    return(Task.FromResult(context));
                }
            }

            switch (message)
            {
            case TokenActionAgentInitMessage initMessage:
                context.State.TokenManagerAgent = initMessage.TokenManagerAgentCapability;
                context.State.TriggerToAction   = initMessage.TriggererToAction ?? new Dictionary <AgentTriggerPair, ITriggeredAction>();
                context.State.Recipients        = initMessage.Recipients;// ?? new List<IAgentTokenKey>();
                context.State.SelfId            = self.Issuer;

                var distributeCapabilityMessage = new DistributeCapabilitiesMessage
                {
                    Id = self.Issuer,
                    AgentCapabilities = new Dictionary <string, AgentCapability>()
                    {
                        { "AddRecipientMessage", context.IssueCapability(new[] { typeof(AddRecipientMessage) }) },
                        { "RemoveRecipientMessage", context.IssueCapability(new[] { typeof(RemoveRecipientMessage) }) },
                        { "AddTriggerToActionMessage", context.IssueCapability(new[] { typeof(AddTriggerToActionMessage) }) },
                    }
                };
                if (initMessage.Subscription != null)
                {
                    foreach (var agent in initMessage.Subscription)
                    {
                        context.AddSubscription(agent);
                    }
                }
                context.SendMessage(initMessage.CreatorAgentCapability, distributeCapabilityMessage, null);
                break;

            case AddRecipientMessage addMessage:
                if (context.State.AddRecipient(addMessage.Recipient))
                {
                    context.MakePublication(new RecipientAddedPublication(addMessage.Recipient));
                }
                break;

            case RemoveRecipientMessage removeMessage:
                if (context.State.RemoveRecipient(removeMessage.Recipient))
                {
                    context.MakePublication(new RecipientRemovedPublication(removeMessage.Recipient));
                }
                break;

            case AddTriggerToActionMessage addTriggerMessage:
                context.State.TriggerToAction.Add(addTriggerMessage.Trigger, addTriggerMessage.Action);
                break;
            }

            return(Task.FromResult(context));
        }
Пример #2
0
 public AddTriggerToActionMessage(AgentTriggerPair trigger, ITriggeredAction action)
 {
     Trigger = trigger;
     Action  = action;
 }
Пример #3
0
        public Task <AgentContext <TokenBurnerState> > Run(object state, AgentCapability self, object message)
        {
            var agentState = state as TokenBurnerState ?? new TokenBurnerState();
            var context    = new AgentContext <TokenBurnerState>(agentState, self);

            if (message is AbstractTrigger msg)
            {
                var pair = new AgentTriggerPair(msg.Sender, message.GetType());
                if (context.State.TriggerToAction.ContainsKey(pair))
                {
                    (IList <object>, IList <object>)result = RecipientState.TriggerCheck(context.State, pair, msg);

                    foreach (BurnTokenMessage action in result.Item1)
                    {
                        context.SendMessage(context.State.TokenManagerAgent, action, null);
                    }

                    foreach (var publication in result.Item2)
                    {
                        context.MakePublication(publication);
                    }

                    return(Task.FromResult(context));
                }
            }

            switch (message)
            {
            case TokenActionAgentInitMessage initMessage:
                context.State.TokenManagerAgent = initMessage.TokenManagerAgentCapability;
                context.State.TriggerToAction   = initMessage.TriggererToAction ?? new Dictionary <AgentTriggerPair, ITriggeredAction>();
                context.State.SelfId            = self.Issuer;

                var distributeCapabilityMessage = new DistributeCapabilitiesMessage
                {
                    Id = self.Issuer,
                    AgentCapabilities = new Dictionary <string, AgentCapability>()
                    {
                        { "GetTokensMessage", context.IssueCapability(new[] { typeof(GetTokensMessage) }) },
                        { "AddTriggerToActionMessage", context.IssueCapability(new[] { typeof(AddTriggerToActionMessage) }) },
                    }
                };
                if (initMessage.Subscription != null)
                {
                    foreach (var agent in initMessage.Subscription)
                    {
                        context.AddSubscription(agent);
                    }
                }
                context.SendMessage(initMessage.CreatorAgentCapability, distributeCapabilityMessage, null);
                break;

            //Tokens are minted on burner's addres so that he can burn them,
            //but they are actually for the user TokensMintedTriggerer.To
            //He is added as recipient and he can take ownership of the tokens with GetTokensMessage
            //This is done so that the system can work automaticly
            case TokensMintedTriggerer transferedMessage:
                if (context.State.AddRecipient(transferedMessage.To))
                {
                    context.State.MintedMessages.Add(transferedMessage);
                    context.MakePublication(new RecipientAddedPublication(transferedMessage.To));
                }
                break;

            case GetTokensMessage getTokensMessage:
                IAgentTokenKey agentSender;
                if (context.State.GetTokens(getTokensMessage.Recipient, getTokensMessage.Amount, out agentSender))
                {
                    var transfer = new TransferTokenMessage(getTokensMessage.Amount, agentSender, getTokensMessage.Recipient);
                    context.SendMessage(null, transfer, null);
                }
                break;

            case AddTriggerToActionMessage addTriggerMessage:
                context.State.TriggerToAction.Add(addTriggerMessage.Trigger, addTriggerMessage.Action);
                break;
            }

            return(Task.FromResult(context));
        }