)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » Agent (Ia.Cl.Model)

Public general use code classes and xml files that we've compiled and used over the years:

Agent model

    1: using System;
    2: using System.Collections.Generic;
    3: using System.Linq;
    4: using System.Runtime.Serialization;
    5: using System.Threading.Tasks;
    6: using System.Reflection;
    7: using System.Runtime.InteropServices;
    8: using System.Threading;
    9:  
   10: namespace Ia.Cl.Model
   11: {
   12:     ////////////////////////////////////////////////////////////////////////////
   13:  
   14:     /// <summary publish="true">
   15:     /// Agent model
   16:     /// </summary>
   17:     /// 
   18:     /// <remarks> 
   19:     /// Copyright © 2006-2020 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
   20:     ///
   21:     /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
   22:     /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
   23:     ///
   24:     /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   25:     /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   26:     /// 
   27:     /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
   28:     /// 
   29:     /// Copyright notice: This notice may not be removed or altered from any source distribution.
   30:     /// </remarks> 
   31:     [DataContract(IsReference = true, Namespace = "kw.com.ia.api.agent", Name = "agent")]
   32:     public class Agent
   33:     {
   34:         private readonly AssemblyDescriptionAttribute assemblyDescriptionAttribute;
   35:         private readonly AssemblyTitleAttribute assemblyTitleAttribute;
   36:         private readonly AssemblyProductAttribute assemblyProductAttribute;
   37:         private readonly GuidAttribute guidAttribute;
   38:  
   39:         private Queue<Ia.Cl.Models.Packet> outboxPacketQueue, inboxPacketQueue;
   40:  
   41:         // make sure you include System.Text.Json in project
   42:  
   43:         ////////////////////////////////////////////////////////////////////////////
   44:  
   45:         /// <summary>
   46:         ///
   47:         /// </summary>
   48:         public Agent()
   49:         {
   50:             // this must be kept because it will be used by the API
   51:         }
   52:  
   53:         ////////////////////////////////////////////////////////////////////////////
   54:  
   55:         /// <summary>
   56:         ///
   57:         /// </summary>
   58:         public Agent(System.Reflection.Assembly assembly)
   59:         {
   60:             outboxPacketQueue = new Queue<Ia.Cl.Models.Packet>();
   61:             inboxPacketQueue = new Queue<Ia.Cl.Models.Packet>();
   62:  
   63:             assemblyTitleAttribute = (AssemblyTitleAttribute)AssemblyTitleAttribute.GetCustomAttribute(assembly, typeof(AssemblyTitleAttribute));
   64:             assemblyDescriptionAttribute = (AssemblyDescriptionAttribute)AssemblyDescriptionAttribute.GetCustomAttribute(assembly, typeof(AssemblyDescriptionAttribute));
   65:             assemblyProductAttribute = (AssemblyProductAttribute)AssemblyProductAttribute.GetCustomAttribute(assembly, typeof(AssemblyProductAttribute));
   66:             guidAttribute = (GuidAttribute)GuidAttribute.GetCustomAttribute(assembly, typeof(GuidAttribute));
   67:  
   68:             this.AssemblyDescription = assemblyDescriptionAttribute.Description;
   69:             this.AssemblyTitle = assemblyTitleAttribute.Title;
   70:             this.AssemblyProduct = assemblyProductAttribute.Product;
   71:             this.AssemblyGuid = guidAttribute.Value;
   72:  
   73:             this.InboxHasPacket = false;
   74:             this.OutboxHasPacket = false;
   75:  
   76:             this.ApplicationIsAllowedToRun = true; // or false pending true signal from API server?
   77:  
   78:             Hash = Ia.Cl.Models.Cryptography.Md5.Hash(this.AssemblyProduct);
   79:  
   80:             Created = Updated = DateTime.UtcNow.AddHours(3);
   81:  
   82:             //_ = StartTimer(10, 10);
   83:         }
   84:  
   85:         ////////////////////////////////////////////////////////////////////////////
   86:  
   87:         /// <summary/>
   88:         [DataMember(Name = "assemblyTitle")]
   89:         public string AssemblyTitle { get; set; }
   90:  
   91:         /// <summary/>
   92:         [DataMember(Name = "assemblyDescription")]
   93:         public string AssemblyDescription { get; set; }
   94:  
   95:         /// <summary/>
   96:         [DataMember(Name = "assemblyProduct")]
   97:         public string AssemblyProduct { get; set; }
   98:  
   99:         /// <summary/>
  100:         [DataMember(Name = "assemblyGuid")]
  101:         public string AssemblyGuid { get; set; }
  102:  
  103:         /// <summary/>
  104:         [DataMember(Name = "inboxHasPacket")]
  105:         public bool InboxHasPacket { get; set; }
  106:  
  107:         /// <summary/>
  108:         [DataMember(Name = "outboxHasPacket")]
  109:         public bool OutboxHasPacket { get; set; }
  110:  
  111:         /// <summary/>
  112:         [DataMember(Name = "applicationIsAllowedToRun")]
  113:         public bool ApplicationIsAllowedToRun { get; set; }
  114:  
  115:         /// <summary/>
  116:         [DataMember(Name = "hash")]
  117:         public string Hash { get; set; }
  118:  
  119:         /// <summary/>
  120:         [DataMember(Name = "created")]
  121:         public DateTime Created { get; set; }
  122:  
  123:         /// <summary/>
  124:         [DataMember(Name = "updated")]
  125:         public DateTime Updated { get; set; }
  126:  
  127:         ////////////////////////////////////////////////////////////////////////////
  128:  
  129:         /// <summary>
  130:         ///
  131:         /// </summary>
  132:         private async Task StartTimer(int periodInSeconds, int numberOfRounds)
  133:         {
  134:             Console.WriteLine("Starting timer...");
  135:  
  136:             var now = DateTime.UtcNow;
  137:             var lastSecond = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second, DateTimeKind.Utc);
  138:             var nextSecond = lastSecond.AddSeconds(periodInSeconds);
  139:  
  140:             using (var timer = new IKriv.Threading.Tasks.TaskTimer(periodInSeconds * 1000).StartAt(nextSecond))
  141:             {
  142:                 foreach (var task in timer.Take(numberOfRounds))
  143:                 {
  144:                     await task;
  145:  
  146:                     _ = SynchronizePacketsAsync();
  147:                 }
  148:             }
  149:  
  150:             Console.WriteLine("Done");
  151:         }
  152:  
  153:         ////////////////////////////////////////////////////////////////////////////
  154:  
  155:         /// <summary>
  156:         ///
  157:         /// </summary>
  158:         private async Task SynchronizePacketsAsync()
  159:         {
  160:             var list2 = await this.GetSentPacketsAsync();
  161:             var list3 = await this.GetReceivedPacketsAsync();
  162:  
  163:             //await Ia.Cl.Models.Packet.PostPacketAsync(recipientAssemblyProduct, this.AssemblyProduct, Guid.Empty, name, payload);
  164:  
  165:             Console.WriteLine(DateTime.UtcNow.ToString("HH:mm:ss"));
  166:         }
  167:  
  168:         ////////////////////////////////////////////////////////////////////////////
  169:  
  170:         /// <summary>
  171:         ///
  172:         /// </summary>
  173:         public async Task PeriodicSynchronizePacketsAsync(TimeSpan interval, CancellationToken cancellationToken)
  174:         {
  175:             while (true)
  176:             {
  177:                 await SynchronizePacketsAsync();
  178:                 await Task.Delay(interval, cancellationToken);
  179:             }
  180:         }
  181:  
  182:         ////////////////////////////////////////////////////////////////////////////
  183:  
  184:         /// <summary>
  185:         ///
  186:         /// </summary>
  187:         public async Task SendHeartbeatAsync()
  188:         {
  189:             await Ia.Cl.Models.Heartbeat.SendHeartbeatAsync(AssemblyProduct);
  190:         }
  191:  
  192:         ////////////////////////////////////////////////////////////////////////////
  193:  
  194:         /// <summary>
  195:         ///
  196:         /// </summary>
  197:         public async Task RegisterAgentAsync()
  198:         {
  199:             var list = await AgentListAsync();
  200:  
  201:             if (!list.Any(u => u.AssemblyGuid == this.AssemblyGuid))
  202:             {
  203:                 var agent = await Ia.Cl.Models.Http.PostAsync<Ia.Cl.Models.Agent>("https://api.ia.com.kw", "api/agents", this);
  204:             }
  205:         }
  206:  
  207:         ////////////////////////////////////////////////////////////////////////////
  208:  
  209:         /// <summary>
  210:         ///
  211:         /// </summary>
  212:         public async Task<List<Agent>> AgentListAsync()
  213:         {
  214:             var list = await Ia.Cl.Models.Http.GetAsync<List<Ia.Cl.Models.Agent>>("https://api.ia.com.kw", "api/agents");
  215:  
  216:             return list;
  217:         }
  218:  
  219:         ////////////////////////////////////////////////////////////////////////////
  220:  
  221:         /// <summary>
  222:         ///
  223:         /// </summary>
  224:         public static List<Agent> AgentList()
  225:         {
  226:             var list = Ia.Cl.Models.Http.GetAsync<List<Ia.Cl.Models.Agent>>("https://api.ia.com.kw", "api/agents");
  227:  
  228:             return list.Result;
  229:         }
  230:  
  231:         ////////////////////////////////////////////////////////////////////////////
  232:  
  233:         /// <summary>
  234:         ///
  235:         /// </summary>
  236:         public void PostPacket(string recipientAssemblyProduct, string name, string payload)
  237:         {
  238:             var packet = new Packet() { Name = name, RecipientAssemblyProduct = recipientAssemblyProduct, Payload = payload };
  239:  
  240:             outboxPacketQueue.Enqueue(packet);
  241:         }
  242:  
  243:         ////////////////////////////////////////////////////////////////////////////
  244:  
  245:         /// <summary>
  246:         ///
  247:         /// </summary>
  248:         public async Task<List<Ia.Cl.Models.Packet>> GetSentPacketsAsync()
  249:         {
  250:             var list = await Ia.Cl.Models.Packet.SenderPacketListAsync(this.AssemblyProduct);
  251:  
  252:             return list;
  253:         }
  254:  
  255:         ////////////////////////////////////////////////////////////////////////////
  256:  
  257:         /// <summary>
  258:         ///
  259:         /// </summary>
  260:         public async Task<List<Ia.Cl.Models.Packet>> GetReceivedPacketsAsync()
  261:         {
  262:             var list = await Ia.Cl.Models.Packet.RecipientPacketListAsync(this.AssemblyProduct);
  263:  
  264:             return list;
  265:         }
  266:  
  267:         ////////////////////////////////////////////////////////////////////////////
  268:         ////////////////////////////////////////////////////////////////////////////
  269:     }
  270:  
  271:     ////////////////////////////////////////////////////////////////////////////
  272:     ////////////////////////////////////////////////////////////////////////////
  273: }