-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultiCellBuffer.cs
90 lines (80 loc) · 2.71 KB
/
MultiCellBuffer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System;
using System.Threading;
namespace Flight_Ticket_Booking
{
class MultiCellBuffer
{
static public int BUFFER_LENGTH = 3;
static public Semaphore sema;
static ReaderWriterLock rwlock = new ReaderWriterLock();
public int numInBuffer = 0;
private string[] buffer;
// Returns true is there are no orders in buffer
public bool isEmpty()
{
if (numInBuffer <= 0)
return true;
else
return false;
}
// Constructor. Sets each cell to null.
// Sets semaphore to 3.
public MultiCellBuffer()
{
sema = new Semaphore(0, 3);
sema.Release(3);
buffer = new string[BUFFER_LENGTH];
for ( int x = 0; x < buffer.Length; x++)
{
buffer[x] = null;
}
}
// Called from travel agency
public void setOneCell(string order)
{
sema.WaitOne();
rwlock.AcquireWriterLock(300);
// cycle through each cell until an empty cell is reached and fill
// it with the order
for (int i = 0; i < buffer.Length; i++)
{
if (buffer[i] == null)
{
buffer[i] = order;
numInBuffer++;
break;
}
}
rwlock.ReleaseWriterLock();
}
// Called from Airline
public string getOneCell()
{
string cellToDeliver = "";
rwlock.AcquireReaderLock(300);
if (numInBuffer > 0)
{
OrderClass order_from_buffer;
// Cycle through each cell, checking if the Order Airline ID matches the
// callers Airline ID
for (int i = 0; i < buffer.Length; i++)
{
if (buffer[i] != null)
{
order_from_buffer = Coding.decode(buffer[i]);
if (order_from_buffer.ReceiverId == Convert.ToInt32(Thread.CurrentThread.Name))
{
cellToDeliver = buffer[i];
buffer[i] = null;
numInBuffer--;
sema.Release();
break;
}
}
}
}
rwlock.ReleaseLock();
return cellToDeliver;
}
}
}