-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMBRC.java
More file actions
164 lines (118 loc) · 5.38 KB
/
MBRC.java
File metadata and controls
164 lines (118 loc) · 5.38 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package com.company;
import java.util.LinkedList;
import java.util.Queue;
// CLASS: Can call MBRC in other Java Classes and declare MBRC type variables
public class MBRC {
//Dec: Variables are private to this class only and cannot be called by other Java Classes.//Can use this function for instances
private int maxMessageBuffer = 1;
private int messageCount = 0;
private int maxReplyBuffer = 1;
private int replyCount = 0;
private static boolean messageBufferFull = false;
private static boolean replyBufferFull = false;
Queue<Message> messageBuffer = new LinkedList<Message>();
Queue<Message> replyBuffer = new LinkedList<Message>();
//
//
public void MBRC() {
}
/*
// MBRC CLASS METHOD MEMBER: send; MBRC.send(String op, int num);
// SYNCHRONIZED: Multiple threads cannot access MBRC.send concurrently changing the value of the output concurrently
// INPUT: String type variable op; int type variable num;
// OUTPUT: String type variable msgBackToSender;
public synchronized String send(String op, int num) throws InterruptedException {
String message = op + "" + num; //Dec: String type variable message;
System.out.println(message);
this.messageBuffer.add(message);// Adds to messageBuffer queue
this.messageCount++;
System.out.println("(\"" + op + "\"," + num + ") is placed in queue buffer.");
if(this.messageCount == this.maxMessageBuffer) {
this.messageBufferFull = true;
this.notifyAll();
}
while(!this.responseBufferFull) {
this.wait();
}
String msgBackToSender = this.replyBuffer.remove();
this.replyCount--;
this.responseBufferFull = false;
return msgBackToSender;
}
//
//
*/
// MBRC CLASS METHOD MEMBER: send; MBRC.send(String op, int num);
// SYNCHRONIZED: Multiple threads cannot access MBRC.send concurrently changing the value of the output concurrently
// INPUT: String type variable op; int type variable num;
// OUTPUT: String type variable msgBackToSender;
public synchronized Message sendEn(String senderGenOpString, String senderGenNumString, byte[] senderGenNumSignatureBytes) throws InterruptedException {
Message senderGenFullMsgString = new Message();
senderGenFullMsgString.message_content = senderGenOpString + " " + senderGenNumString; //Dec: String type variable message;
senderGenFullMsgString.message_signature = senderGenNumSignatureBytes;
System.out.println("Full message in MBRC : " + senderGenFullMsgString.message_content + " with Signature " + senderGenFullMsgString.message_signature);
this.messageBuffer.add(senderGenFullMsgString);// Adds to messageBuffer queue
++this.messageCount;
System.out.println("(\"" + senderGenOpString + "\"," + senderGenNumString + ") is placed in queue buffer.");
if(this.messageCount == this.maxMessageBuffer) {
this.messageBufferFull = true;
this.notifyAll();
}
//
//
while(!this.replyBufferFull) {
this.wait();
}
Message msgBackToSender = this.replyBuffer.remove();
--this.replyCount;
this.replyBufferFull = false;
return msgBackToSender;
}
//
//
// MBRC CLASS METHOD MEMBER: receive; MBRC.receive();
// SYNCHRONIZED: Multiple threads cannot access MBRC.receive concurrently changing the value of the output concurrently
// INPUT: None
// OUTPUT: String type variable msgBackToReceiver;
public synchronized Message receive() throws InterruptedException {
while(!this.messageBufferFull) {
this.wait();
}
Message msgToReceiver = this.messageBuffer.remove();
--this.messageCount;
this.messageBufferFull = false;
return msgToReceiver;
}
//
//
// MBRC CLASS METHOD MEMBER: receive; MBRC.reply(String op, int num);
// SYNCHRONIZED: Multiple threads cannot access MBRC.receive concurrently changing the value of the output concurrently
// INPUT: None
// OUTPUT: int type variable num;
public synchronized Message reply(String resultFromReceiverNumString, byte[] receiverGenNumMDBytes) {
//String response = op + " " + num;
Message receiverGenFullResult = new Message();
receiverGenFullResult.message_content = resultFromReceiverNumString; //Dec: String type variable message;
receiverGenFullResult.message_signature = receiverGenNumMDBytes;
this.replyBuffer.add(receiverGenFullResult);
++this.replyCount;
if (this.replyCount == this.maxReplyBuffer) {
this.replyBufferFull = true;
this.notifyAll();
}
return receiverGenFullResult;
}
/* public static void send(Message message4, byte[] signedmessage) {
Message messageBuffer = message4;
messageBufferFull = true;
while (responseBufferFull == false) {
}
}*/
/* public void receive(Message message4) throws InterruptedException {
*//*while(messageBufferFull == false) {
wait(500);
}
//messageBuffer = null;
messageBufferFull = false;*//*
}*/
}