Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

peer-topeer Networks phần 6 ppsx
Nội dung xem thử
Mô tả chi tiết
P1: OTE/SPH P2: OTE
SVNY285-Loo October 18, 2006 7:8
class Share {
private boolean go = true;
boolean debug = false;
int totalProcessor=0;
int doneProcessor=0;
double answer=0.0;
public Share(int totalProcessor,boolean debug)
{
this.debug= debug;
this.totalProcessor = totalProcessor;
}
public synchronized void setGoAhead( )
{
if (debug) System.out.println( Share called go: + go);
while ( !go )
{
try
{
wait();
}
catch ( InterruptedException e )
{
System.err.println( Exception: + e.toString() );
}
}
if (debug) System.out.println( Share setGoAhead go: + go);
go = false;
notifyAll();
}
public synchronized void updateAnswer(double ans)
{
answer =answer+ans;
doneProcessor++;
notifyAll();
}
public synchronized double getAnswer()
{
if (debug) System.out.println(waiting for answer by Server);
while ( doneProcessor != totalProcessor )
{
try
{
wait();
}
catch ( InterruptedException e )
{
System.err.println( Exception: + e.toString() );
}
}
return answer;
}
}
Figure 9.18. share.java
128
P1: OTE/SPH P2: OTE
SVNY285-Loo October 18, 2006 7:8
class ShareObj
{
boolean Done = false;
private int totalProcessor;
private int totalAccess;
private boolean free =true;
boolean debug;
boolean connected=false;
public ShareObj(int i,boolean debug)
{
totalProcessor=i;
this.debug=debug;
totalAccess= 0;
}
public synchronized void increaseConnection()
{
if (debug) System.out.println( Share obj free: + free
+ no. of Processor: +
totalProcessor);
while ( !free )
{
try
{
wait();
}
catch ( InterruptedException e ) {
System.err.println( Exception: + e.toString() );
}
}
free = false;
Done = true;
totalAccess++;
if (totalAccess == totalProcessor)
{
totalAccess =0;
connected=true;
if (debug) System.out.println( Share obj + access:+totalAccess);
}
free = true;
notify();
}
public synchronized void allConnected()
{
while ( !connected)
{
try
{
wait();
}
catch ( InterruptedException e )
{
System.err.println( Exception: + e.toString() );
}
}
}
}
Figure 9.19. shareObj.java
129
P1: OTE/SPH P2: OTE
SVNY285-Loo October 18, 2006 7:8
130 9. Power Server: Model 1
Figure 9.20. Screen of the server in waiting state.
9.3 First Test
You can use only one computer to run your first test. If you are not familiar with
client-server programming, I will recommend you to use one computer for the first
test. A computer can be both server and client concurrently. You must start the
server program first before you run the client program.
1. You start the server program by typing the following command in DOS:
java addServer
The computer will be in a wait state as in Fig. 9.20.
2. Start the client program by typing the following command in anther DOS session.
java addClient 1 10 local.txt
The first parameter ‘1’ indicates that there is only one server in the system. The
system will add the number from 1 to 10. There are only 2 lines in the ‘local.txt’ file:
1
localhost
The ‘1’ in the first line indicates there is only one address in the whole file.
‘localhost’ is the synonym of the IP address of your local computer.
The local.txt file enables you to access your own local computer without specifying its IP address. You can see the result in both windows: Fig. 9.21 is the screen
of the server window, while Fig. 9.22 is the screen of the client window.
Figure 9.21. Screen of server window.