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.Model.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.Model.Packet>();
61: inboxPacketQueue = new Queue<Ia.Cl.Model.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.Model.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:
130: /// <summary>
131: ///
132: /// </summary>
133: private async Task StartTimer(int periodInSeconds, int numberOfRounds)
134: {
135: Console.WriteLine("Starting timer...");
136:
137: var now = DateTime.UtcNow;
138: var lastSecond = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second, DateTimeKind.Utc);
139: var nextSecond = lastSecond.AddSeconds(periodInSeconds);
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: ////////////////////////////////////////////////////////////////////////////
157:
158: /// <summary>
159: ///
160: /// </summary>
161: private async Task SynchronizePacketsAsync()
162: {
163: var list2 = await this.GetSentPacketsAsync();
164: var list3 = await this.GetReceivedPacketsAsync();
165:
166: //await Ia.Cl.Model.Packet.PostPacketAsync(recipientAssemblyProduct, this.AssemblyProduct, Guid.Empty, name, payload);
167:
168: Console.WriteLine(DateTime.UtcNow.ToString("HH:mm:ss"));
169: }
170: */
171:
172: /*
173: ////////////////////////////////////////////////////////////////////////////
174:
175: /// <summary>
176: ///
177: /// </summary>
178: public async Task PeriodicSynchronizePacketsAsync(TimeSpan interval, CancellationToken cancellationToken)
179: {
180: while (true)
181: {
182: await SynchronizePacketsAsync();
183: await Task.Delay(interval, cancellationToken);
184: }
185: }
186: */
187:
188: ////////////////////////////////////////////////////////////////////////////
189:
190: /// <summary>
191: ///
192: /// </summary>
193: public async Task SendHeartbeatAsync()
194: {
195: await Ia.Cl.Model.Heartbeat.SendHeartbeatAsync(AssemblyProduct);
196: }
197:
198: /*
199: ////////////////////////////////////////////////////////////////////////////
200:
201: /// <summary>
202: ///
203: /// </summary>
204: public async Task RegisterAgentAsync()
205: {
206: var list = await AgentListAsync();
207:
208: if (!list.Any(u => u.AssemblyGuid == this.AssemblyGuid))
209: {
210: var agent = await Ia.Cl.Model.Http.PostAsync<Ia.Cl.Model.Agent>("https://api.ia.com.kw", "api/agents", this);
211: }
212: }
213: */
214:
215: /*
216: ////////////////////////////////////////////////////////////////////////////
217:
218: /// <summary>
219: ///
220: /// </summary>
221: public async Task<List<Agent>> AgentListAsync()
222: {
223: var list = await Ia.Cl.Model.Http.GetAsync<List<Ia.Cl.Model.Agent>>("https://api.ia.com.kw", "api/agents");
224:
225: return list;
226: }
227: */
228:
229: /*
230: ////////////////////////////////////////////////////////////////////////////
231:
232: /// <summary>
233: ///
234: /// </summary>
235: public static List<Agent> AgentList()
236: {
237: var list = Ia.Cl.Model.Http.GetAsync<List<Ia.Cl.Model.Agent>>("https://api.ia.com.kw", "api/agents");
238:
239: return list.Result;
240: }
241: */
242:
243: ////////////////////////////////////////////////////////////////////////////
244:
245: /// <summary>
246: ///
247: /// </summary>
248: public void PostPacket(string recipientAssemblyProduct, string name, string payload)
249: {
250: var packet = new Packet() { Name = name, RecipientAssemblyProduct = recipientAssemblyProduct, Payload = payload };
251:
252: outboxPacketQueue.Enqueue(packet);
253: }
254:
255: /*
256: ////////////////////////////////////////////////////////////////////////////
257:
258: /// <summary>
259: ///
260: /// </summary>
261: public async Task<List<Ia.Cl.Model.Packet>> GetSentPacketsAsync()
262: {
263: var list = await Ia.Cl.Model.Packet.SenderPacketListAsync(this.AssemblyProduct);
264:
265: return list;
266: }
267: */
268:
269: /*
270: ////////////////////////////////////////////////////////////////////////////
271:
272: /// <summary>
273: ///
274: /// </summary>
275: public async Task<List<Ia.Cl.Model.Packet>> GetReceivedPacketsAsync()
276: {
277: var list = await Ia.Cl.Model.Packet.RecipientPacketListAsync(this.AssemblyProduct);
278:
279: return list;
280: }
281: */
282:
283: ////////////////////////////////////////////////////////////////////////////
284: ////////////////////////////////////////////////////////////////////////////
285: }
286:
287: ////////////////////////////////////////////////////////////////////////////
288: ////////////////////////////////////////////////////////////////////////////
289: }