| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- using System;
- using System.Collections;
- using System.IO;
- using System.Runtime.Serialization;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Text.Json; // Cần thêm namespace này
- namespace ApiProcessToken.Models.common
- {
-
- public class Queue
- {
- private int maxQueueSize = 0;
- private ArrayList queueData = new ArrayList();
- private object mutex;
-
- public Queue()
- {
- maxQueueSize = 1000000;
- mutex = this;
- }
-
- public Queue(int maxSize)
- {
- maxQueueSize = maxSize;
- mutex = this;
- }
-
- public int Size()
- {
- lock (mutex)
- {
- return queueData.Count;
- }
- }
- public bool IsEmpty()
- {
- lock (mutex)
- {
- return (queueData.Count == 0);
- }
- }
-
- public object Dequeue()
- {
- lock (mutex)
- {
- object first = null;
- if (queueData.Count > 0)
- {
- first = queueData[0];
- queueData.RemoveAt(0);
- }
- return first;
- }
- }
- public object Dequeue(object obj)
- {
- object found = null;
- lock (mutex)
- {
- found = queueData.Contains(obj);
- if (found != null)
- {
- queueData.Remove(obj);
- }
- }
- return obj;
- }
-
- public void Enqueue(Object obj)
- {
- lock (mutex)
- {
- if (queueData.Count >= maxQueueSize)
- {
- throw new IndexOutOfRangeException("Queue is full. Element not added.");
- }
- queueData.Add(obj);
- }
- }
- public void Clear()
- {
- lock (mutex)
- {
- if (queueData.Count >= 0)
- {
- queueData.Clear();
- }
- }
- }
-
- public object Find(object obj)
- {
- lock (mutex)
- {
- object current;
- IEnumerator iter = queueData.GetEnumerator();
- while (iter.MoveNext())
- {
- current = iter.Current;
- if (current.Equals(obj))
- {
- return current;
- }
- }
- }
- return null;
- }
- public ArrayList ToArrayList()
- {
- return queueData;
- }
- public ArrayList ToArrayListAndClear()
- {
- lock (mutex)
- {
- ArrayList result = new ArrayList();
- if (queueData.Count >= 0)
- {
- result.AddRange(queueData);
- queueData.Clear();
- }
- return result;
- }
- }
- //public void SaveQueue(string file_name)
- //{
- // if (File.Exists(file_name))
- // {
- // File.Delete(file_name);
- // }
- // Stream st = File.Create(file_name);
- // BinaryFormatter bf = new BinaryFormatter();
- // bf.Serialize(st, queueData);
- // st.Close();
- //}
- //public int LoadQueue(string file_name)
- //{
- // if (File.Exists(file_name))
- // {
- // Stream st = File.OpenRead(file_name);
- // if (st.Length > 0)
- // {
- // BinaryFormatter bf = new BinaryFormatter();
- // queueData = (ArrayList)bf.Deserialize(st);
- // }
- // st.Close();
- // //File.Delete(file_name);
- // }
- // return queueData.Count;
- //}
- public void SaveQueue(string file_name)
- {
- // 1. Chuyển đổi đối tượng (queueData) thành chuỗi JSON.
- // Dùng System.Text.Json.JsonSerializer.Serialize
- string jsonString = JsonSerializer.Serialize(queueData);
- // 2. Ghi chuỗi JSON đó vào tệp.
- // File.WriteAllText tự động tạo tệp nếu nó không tồn tại hoặc ghi đè nếu đã tồn tại.
- File.WriteAllText(file_name, jsonString);
- }
- // Lưu ý: Thay vì gán trực tiếp, chúng ta sẽ trả về đối tượng đã tải.
- // Hãy điều chỉnh class chứa hàm này để nó có thể chấp nhận đối tượng trả về.
- public static ArrayList LoadQueue(string file_name)
- {
- if (!File.Exists(file_name))
- {
- // Trả về một ArrayList rỗng nếu tệp không tồn tại.
- return new ArrayList();
- }
- try
- {
- // 1. Đọc toàn bộ nội dung tệp thành chuỗi JSON.
- string jsonString = File.ReadAllText(file_name);
- // 2. Kiểm tra chuỗi JSON có rỗng không (thay cho st.Length > 0).
- if (string.IsNullOrWhiteSpace(jsonString))
- {
- return new ArrayList();
- }
- // 3. Chuyển đổi chuỗi JSON trở lại thành đối tượng ArrayList.
- // Cần truyền rõ ràng kiểu ArrayList
- ArrayList loadedData = JsonSerializer.Deserialize<ArrayList>(jsonString);
- // Trả về dữ liệu đã tải (hoặc một ArrayList rỗng nếu lỗi deserialize).
- return loadedData ?? new ArrayList();
- }
- catch (Exception ex)
- {
- // Nên ghi lại lỗi (logging) ở đây để xử lý các vấn đề về định dạng tệp.
- Console.WriteLine($"Lỗi khi tải queue từ JSON: {ex.Message}");
- return new ArrayList();
- }
- }
- }
- }
|