My favorites | Sign in
Logo
                
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/**
* Copyright (C) 2007, 2008 Carnegie Mellon University and others.
*
* This file is part of Plural.
*
* Plural is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* Plural is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Plural; if not, see <http://www.gnu.org/licenses>.
*
* Linking Plural statically or dynamically with other modules is
* making a combined work based on Plural. Thus, the terms and
* conditions of the GNU General Public License cover the whole
* combination.
*
* In addition, as a special exception, the copyright holders of Plural
* give you permission to combine Plural with free software programs or
* libraries that are released under the GNU LGPL and with code
* included in the standard release of Eclipse under the Eclipse Public
* License (or modified versions of such code, with unchanged license).
* You may copy and distribute such a system following the terms of the
* GNU GPL for Plural and the licenses of the other code concerned.
*
* Note that people who make modified versions of Plural are not
* obligated to grant this special exception for their modified
* versions; it is their choice whether to do so. The GNU General
* Public License gives permission to release a modified version
* without this exception; this exception also makes it possible to
* release a modified version which carries forward this exception.
*/

package edu.cmu.cs.nimby.test.oopsla;



import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;

import edu.cmu.cs.crystal.annotations.PassingTest;
import edu.cmu.cs.crystal.annotations.UseAnalyses;
import edu.cmu.cs.plural.annot.ClassStates;
import edu.cmu.cs.plural.annot.Perm;
import edu.cmu.cs.plural.annot.Pure;
import edu.cmu.cs.plural.annot.Share;
import edu.cmu.cs.plural.annot.State;
import edu.cmu.cs.plural.annot.TrueIndicates;
import edu.cmu.cs.plural.annot.Unique;
import edu.cmu.cs.plural.annot.Use;

/**
* Here is a pedagogical example. It is meant to be a program similar in architecture to
* a chat application I saw on the Internet once.<br>
* <br>
* 9/12/08<br>
* This program has changed since publication. It has been updated to
* use the new invariant syntax, and the @Perm annotation.
*/
@PassingTest
@UseAnalyses("NIMBYChecker")
public class Chat {

public static void main(String[] args) {
Connection net = new Connection();
GUI chat_gui = new GUI(net);
NetworkListener listener = new NetworkListener(net);

(new Thread(chat_gui)).start();
(new Thread(listener)).start();
}

}


/**
* A thread that is listening to the network.
* Simulate the call-back thread from the network that can disconnect the device at
* will.
*/
@ClassStates(@State(name="alive",
inv="share(myNetwork)"))
class NetworkListener implements Runnable {

final private Connection myNetwork;

@Unique(use = Use.FIELDS)
public void run() {
for( int i = 0; i < 3; i++ ) {
try {
Thread.sleep(1000);

atomic: synchronized(Chat.class) {
if( myNetwork.isConnected() ) {
myNetwork.disconnect();
}
}
} catch(Exception e) {
break;
}
}
return;
}

@Perm(requires="share(#0)", ensures="unique(this!fr)")
NetworkListener(Connection myNetwork) {
this.myNetwork = myNetwork;
}
}

class BufferedWriter extends java.io.BufferedWriter {
@Perm(ensures="unique(this!fr)")
public BufferedWriter(FileWriter f) {
super(f);
}
}

@ClassStates({@State(name="alive", inv="full(socket)"),
@State(name="IDLE", inv="socket == null"),
@State(name="CONNECTED", inv="socket != null")})
class Connection {
private BufferedWriter socket;

@Perm(ensures="unique(this!fr)")
Connection() {
socket = null;
}

@Share(use = Use.FIELDS, requires="CONNECTED", ensures="IDLE")
void disconnect() throws IOException {
atomic: synchronized(Chat.class) {
socket.flush();
socket.close();
this.socket = null;
return;
}
}

@Pure(use = Use.FIELDS)
@TrueIndicates("IDLE")
public boolean isIdle() {
atomic: synchronized(Chat.class) {
if( this.socket == null ) {
return true;
}
else {
return false;
}
}
}

@Pure(use = Use.FIELDS)
@TrueIndicates("CONNECTED")
public boolean isConnected() {
atomic: synchronized(Chat.class)
{
if( this.socket != null ) {
return true;
}
else {
return false;
}
}
}

@Share(use = Use.FIELDS, requires="CONNECTED", ensures="CONNECTED")
void send(String txt) throws IOException {
atomic: synchronized(Chat.class) {
this.socket.write(txt + "\n");
return;
}
}

@Share(use = Use.FIELDS, requires="IDLE", ensures="CONNECTED")
void connect(String ip) throws IOException {
atomic: synchronized(Chat.class) {
this.socket = new BufferedWriter(new FileWriter("foo.out", true));
return;
}
}
}

/**
* A 'GUI' for the chat application.
* Simulates random button presses on GUI buttons via a thread.
*/
@ClassStates(@State(name="alive", inv="share(myNetwork)"))
class GUI implements Runnable {

final private Connection myNetwork;

@Perm(requires="share(#0)", ensures="unique(this!fr)")
GUI(Connection myNetwork) {
this.myNetwork = myNetwork;
}

/*
* Simulate random button presses from the user on enabled buttons.
*/
@Unique(use = Use.FIELDS)
public void run() {
Random r = new Random();
try {
for(int i = 0; i < 20; i++) {
Thread.sleep(300);

switch(r.nextInt(6)) {
case 0:
atomic: synchronized(Chat.class) {
if( myNetwork.isIdle() ) {
myNetwork.connect("fake.fake.fake.fake");
}
}
break;
case 1:
atomic: synchronized(Chat.class) {
if( myNetwork.isConnected() ) {
myNetwork.disconnect();
}
}
break;
case 2:
case 3:
case 4:
case 5:
atomic: synchronized(Chat.class) {
if( myNetwork.isConnected() ) {
myNetwork.send("Yo dude let's talk?!");
}
}
break;
default:
break;
}
}
} catch (Exception e) {
return;
}
finally {
atomic: synchronized(Chat.class) {
if( myNetwork.isConnected() ) {
try { myNetwork.disconnect(); } catch(Exception e) {}
}
}
}
}
}
Show details Hide details

Change log

r264 by kevin.bierhoff on Mar 26, 2009   Diff
Port to new "use" attribute, replacing
fieldAccess flag
Go to: 
Project members, sign in to write a code review

Older revisions

r118 by kevin.bierhoff on Oct 09, 2008   Diff
test case changes for r117
r49 by nels.beckman on Sep 17, 2008   Diff
[No log message]
All revisions of this file

File info

Size: 6818 bytes, 253 lines
Hosted by Google Code