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

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.Models
   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:             /*
  141:             using (var timer = new IKriv.Threading.Tasks.TaskTimer(periodInSeconds * 1000).StartAt(nextSecond))
  142:             {
  143:                 foreach (var task in timer.Take(numberOfRounds))
  144:                 {
  145:                     await task;
  146: 
  147:                     _ = SynchronizePacketsAsync();
  148:                 }
  149:             }
  150:             */
  151:             Console.WriteLine("Done");
  152:         }
  153:  
  154:         ////////////////////////////////////////////////////////////////////////////
  155:  
  156:         /// <summary>
  157:         ///
  158:         /// </summary>
  159:         private async Task SynchronizePacketsAsync()
  160:         {
  161:             try
  162:             {
  163:                 var list2 = await this.GetSentPacketsAsync();
  164:                 var list3 = await this.GetReceivedPacketsAsync();
  165:             }
  166:             catch (Exception e)
  167:             {
  168:             }
  169:  
  170:             //await Ia.Cl.Models.Packet.PostPacketAsync(recipientAssemblyProduct, this.AssemblyProduct, Guid.Empty, name, payload);
  171:  
  172:             Console.WriteLine(DateTime.UtcNow.ToString("HH:mm:ss"));
  173:         }
  174:  
  175:         ////////////////////////////////////////////////////////////////////////////
  176:  
  177:         /// <summary>
  178:         ///
  179:         /// </summary>
  180:         public async Task PeriodicSynchronizePacketsAsync(TimeSpan interval, CancellationToken cancellationToken)
  181:         {
  182:             while (true)
  183:             {
  184:                 try
  185:                 {
  186:                     await SynchronizePacketsAsync();
  187:                     await Task.Delay(interval, cancellationToken);
  188:                 }
  189:                 catch (Exception e)
  190:                 {
  191:                 }
  192:             }
  193:         }
  194:  
  195:         ////////////////////////////////////////////////////////////////////////////
  196:  
  197:         /// <summary>
  198:         ///
  199:         /// </summary>
  200:         public async Task SendHeartbeatAsync()
  201:         {
  202:             try
  203:             {
  204:                 await Ia.Cl.Models.Heartbeat.SendHeartbeatAsync(AssemblyProduct);
  205:             }
  206:             catch (Exception e)
  207:             {
  208:             }
  209:         }
  210:  
  211:         ////////////////////////////////////////////////////////////////////////////
  212:  
  213:         /// <summary>
  214:         ///
  215:         /// </summary>
  216:         public async Task RegisterAgentAsync()
  217:         {
  218:             try
  219:             {
  220:                 var list = await AgentListAsync();
  221:  
  222:                 if (!list.Any(u => u.AssemblyGuid == this.AssemblyGuid))
  223:                 {
  224:                     var agent = await Ia.Cl.Models.Http.PostAsync<Ia.Cl.Models.Agent>("https://api.ia.com.kw", "api/agents", this);
  225:                 }
  226:             }
  227:             catch (Exception e)
  228:             {
  229:             }
  230:         }
  231:  
  232:         ////////////////////////////////////////////////////////////////////////////
  233:  
  234:         /// <summary>
  235:         ///
  236:         /// </summary>
  237:         public async Task<List<Agent>> AgentListAsync()
  238:         {
  239:             var list = new List<Ia.Cl.Models.Agent>();
  240:  
  241:             try
  242:             {
  243:                 list = await Ia.Cl.Models.Http.GetAsync<List<Ia.Cl.Models.Agent>>("https://api.ia.com.kw", "api/agents");
  244:             }
  245:             catch (Exception e)
  246:             {
  247:             }
  248:  
  249:             return list;
  250:         }
  251:  
  252:         ////////////////////////////////////////////////////////////////////////////
  253:  
  254:         /// <summary>
  255:         ///
  256:         /// </summary>
  257:         public static List<Agent> AgentList()
  258:         {
  259:             var list = Ia.Cl.Models.Http.GetAsync<List<Ia.Cl.Models.Agent>>("https://api.ia.com.kw", "api/agents");
  260:  
  261:             return list.Result;
  262:         }
  263:  
  264:         ////////////////////////////////////////////////////////////////////////////
  265:  
  266:         /// <summary>
  267:         ///
  268:         /// </summary>
  269:         public void PostPacket(string recipientAssemblyProduct, string name, string payload)
  270:         {
  271:             var packet = new Packet() { Name = name, RecipientAssemblyProduct = recipientAssemblyProduct, Payload = payload };
  272:  
  273:             outboxPacketQueue.Enqueue(packet);
  274:         }
  275:  
  276:         ////////////////////////////////////////////////////////////////////////////
  277:  
  278:         /// <summary>
  279:         ///
  280:         /// </summary>
  281:         public async Task<List<Ia.Cl.Models.Packet>> GetSentPacketsAsync()
  282:         {
  283:             var list = new List<Ia.Cl.Models.Packet>();
  284:  
  285:             try
  286:             {
  287:                 list = await Ia.Cl.Models.Packet.SenderPacketListAsync(this.AssemblyProduct);
  288:             }
  289:             catch (Exception e)
  290:             {
  291:             }
  292:  
  293:             return list;
  294:         }
  295:  
  296:         ////////////////////////////////////////////////////////////////////////////
  297:  
  298:         /// <summary>
  299:         ///
  300:         /// </summary>
  301:         public async Task<List<Ia.Cl.Models.Packet>> GetReceivedPacketsAsync()
  302:         {
  303:             var list = new List<Ia.Cl.Models.Packet>();
  304:  
  305:             try
  306:             {
  307:                 list = await Ia.Cl.Models.Packet.RecipientPacketListAsync(this.AssemblyProduct);
  308:             }
  309:             catch (Exception e)
  310:             {
  311:             }
  312:  
  313:             return list;
  314:         }
  315:  
  316:         ////////////////////////////////////////////////////////////////////////////
  317:         ////////////////////////////////////////////////////////////////////////////
  318:     }
  319:  
  320:     ////////////////////////////////////////////////////////////////////////////
  321:     ////////////////////////////////////////////////////////////////////////////
  322: }