1. Trang chủ
  2. » Công Nghệ Thông Tin

peer-topeer Networks phần 7 pot

28 130 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

P1: OTE/SPH P2: OTE SVNY285-Loo October 18, 2006 7:9 158 11. Power Server: Model 3 Note that it is important to distinguish the differences between getNextNumber() and getTaskNumber() methods. The getNextNumber() returns the number of next available sub-task but the sub-task is not assigned to the thread yet. On the other hand, the getNextTask() method returns the number next available sub-task and assigns it to the thread. The sub-task is assigned only when the server is ready to process it. 11.4.2.2 run() Method In the run() method, the connection is checked by the following statement: if (!connected) return; The sub-task is assigned by the following statement: subTask=answer.getNextTask(); The source codes of the addClientThread2 module are presented in Fig. 11.7. 11.5 divide.java Module This module divides the task into sub-tasks and returns the task array to the calling module. The source codes are presented in Fig. 11.8. 11.6 share2.java This module differs from the share.java (in Chapter 9) in the following ways: r Two new methods are added, i.e., getTaskNumber and getNextTask. r The setGoAhead method is removed as this synchronization is not required. The following are the functions of the new methods: getTaskNumber—return the next available task number to the calling thread. getNextTask—assign and return the next available sub-task number to the calling thread. The source codes of this module are presented in Fig. 11.9. 11.7 Testing Programs can be tested in a similar way to Sections 6.5 and 6.6. Again I would recommend that you start with one computer even if you have several computers in front of you. P1: OTE/SPH P2: OTE SVNY285-Loo October 18, 2006 7:9 Testing 159 import java.io.*; import java.util.*; public class divide { double x[]; long endPosition; long totalTasks; boolean debug; public divide(double[] x, long endPosition, long totalTasks, boolean debug ) { if (debug) System.out.println(  divide module  ); this.x = x; this.endPosition=endPosition; this.totalTasks = totalTasks; this.debug = debug; } public void go() { long width = endPosition / totalTasks; // ****** dividing job into sub-tasks ***************** for(inti=0;i<x.length;i=i+2) { if (i == 0) x[i] = 1; else x[i]=x[i-1]+1; if ((i + 2) == x.length) x[i + 1] = endPosition; else x[i + 1] = x[i] + width - 1; } if (debug) { System.out.print(  x[i]=  ); for(intj=0;j<x.length; j++) { System.out.print(   + x[j]); } System.out.println(  size of array =  + x.length); } } } Figure 11.8. divide.java P1: OTE/SPH P2: OTE SVNY285-Loo October 18, 2006 7:9 class Share2 { boolean debug = false; int subTask =0; int totalTask=0; int doneTask=0; double answer=0.0; public Share2(int totalTask,boolean debug) { this.debug= debug; this.totalTask = totalTask; } public synchronized int getTaskNumber() { int nextTaskNumber = subTask +1; return nextTaskNumber; // return the number of the next available sub-task } public synchronized int getNextTask() { if (subTask == totalTask) return 0; else { subTask++; // assign the task return subTask; // return the number of this sub-task } } public synchronized void updateAnswer(double ans) { answer =answer+ans; doneTask++; notifyAll(); } public synchronized double getAnswer() { if (debug) System.out.println(  waiting for answer by Server  ); while ( doneTask != totalTask ) { try { wait(); } catch ( InterruptedException e ) { System.err.println(  Exception:  + e.toString() ); } } return answer; } } Figure 11.9. share2.java 160 P1: OTE/SPH P2: OTE SVNY285-Loo October 18, 2006 7:9 Testing 161 11.7.1 First Test 1. Start the server program by typing the following command in DOS: java addServer2 Remember to type ‘2’ at the end of this command. More information can be displayed if you turn on the debug indicator. This indicator can be turned on by appending ‘1’ in the command. e.g., java addServer2 1 2. Start the client program by typing the following command in DOS: e.g., java addClient2 4 100 local.txt where 4 is the total number of sub-tasks, 100 is the range from 1 to 100 and local.txt is the IP file (refer to Section 6.5 for the content of this file). You can obtain more information by turning on the debug indicator. You can turn it by adding appendix ‘1’ in the above command. Refer to Section 11.3 for the description of the parameters. 11.7.2 Server and Client Screens Figure 11.10 shows the server screen. Four sub-totals are displayed. Figure 11.11 shows the client screen. In addition to showing the grand totals, the four sub-totals are also displayed. 11.7.3 Troubleshooting Make sure that you are using the right version of programs (i.e., addClient2 and addServer2). It is very easy to forget typing ‘2’ in either one of them, particularly if you have done a lot of tests using the models in Chapters 9 and 10. 11.7.4 Second Test If the first test is successful, you are ready to start the second test with two or more computers. 1. Copy all programs to all computers. 2. Type ‘java addServer’ in DOS of both server computers. 3. Obtain the IP addresses of both computers. 4. You can use a third computer or use one of the two computers as client. r Edit the ip.txt and replace the IP addresses with your server computers. r Type ‘java addClient2 4 1000 ip.txt’, where 4 is total number of sub-tasks and 1000 is the range 1 to 1000. The screen of the client is displayed in Fig. 11.12. P1: OTE/SPH P2: OTE SVNY285-Loo October 18, 2006 7:9 162 11. Power Server: Model 3 Figure 11.10. Server screen Figure 11.11. Screen of client P1: OTE/SPH P2: OTE SVNY285-Loo October 18, 2006 7:9 Comparison with Model 1 163 Figure 11.12. Screen from client of test2 (with two servers) 11.7.5 Further Tests If the second test is fine, you can try further tests, adding more computers, and using different combinations until you understand the operations. 11.8 Comparison with Model 1 The overall structure of model 3 is presented in Fig. 11.13. The model in this chapter has the following advantages and disadvantages compared with model 1: Advantages r It is more robust and fault tolerant. r It can maintain ‘load balance’ in both homogeneous and heterogeneous sys- tems. Thus completion time can be reduced in different systems. Disadvantage r The programming logic is more complex. It will take longer time to develop and debug the programs. r The overall communication cost is much higher, especially if the servers are deployed in different countries. r Users need to specify the number of sub-tasks for the system. However, it is not easy to find out the number that can ensure the shortest time of completion. A lot of factors can affect this number. Such factors include: ◦ CPU speed of each server. ◦ Communication speed (establishment of socket) and transmission of sub- task and sub-total. ◦ Overhead in generating threads within the server. P1: OTE/SPH P2: OTE SVNY285-Loo October 18, 2006 7:9 164 11. Power Server: Model 3 addClient2 addServerThread2 Answer Server computer Client computer Server computer Sub-task Answer addClientThread2 (1) addServerThread2 addClientThread2 (2) addServer2 Create threads Sub-task timer share addadd addServer2 readIpFile IP File Divide Figure 11.13. Overall structure of model 3 Note that you can test the system with different numbers of sub-tasks with the same configuration andproblemsize. Such experiments will help you to understand the aforementioned factors. P1: OTE/SPH P2: OTE SVNY285-Loo October 18, 2006 7:9 12 Power Server: Model 4 12.1 Introduction In model 3, you need to type a command to invoke the server side program. It is good for systems with the following characteristics: r There is a small number of servers in the network. r There are only one or two applications in the network, and these applications are frequently used. Thus, users can simply invoke the program and await the request from the client. Users do not need to interface anymore after the unitization. If these characteristics are not true, model 3 can be modified to run under a web server using a similar method in Chapter 11. 12.2 Power Server with Web Server—Model 4 Model 3 can easily be modified to work with any web server. A program ‘invoke- Server2’ for the client is written to send HTTP messages to invoke servlets in server computers. A schematic diagram is presented in Fig. 12.1. This program reads the IP addresses in a local file. It then creates a thread to invoke servlets from remote servers by sending them HTTP messages. 12.2.1 invokeServer2 Module The source codes of this module are presented in Fig. 12.2. The operations of this module are similar to invokeServer.java. This module differs from invoke- Server.java in the following way: r A module startServlet2 is invoked instead of startServlet. 165 P1: OTE/SPH P2: OTE SVNY285-Loo October 18, 2006 7:9 166 12. Power Server: Model 4 invokeServer2 readIpFile addServlet2 HTTP to invoke addServlet Response Server computer Client computer Server computer HTTP to invoke addServlet Response startServlet2 (1) addServlet2 startServlet2 (2) Createthreads Figure 12.1. Invoking the servlet of remote computer. 12.2.2 startServlet2 Module This module calls the URL connect module to send the HTTP message to the server. It will get a true value if the connection is normal, otherwise it will get a false value from the URL connect module. The complete coding list is given in Fig. 12.3. The name and IP address of the client computer are obtained by the following line: InetAddress address = InetAddress.getLocalHost(); The information of the client computer is sent to the server for debugging purposes. It is embedded in the following line: String urlString =  http://  +ip+  :8080/servlet/addServlet2?site=  +address; 12.3 Server Side Program This servlet enables the user to invoke the addServerThread2 program (Chapter 11) with an HTTP message. The structure of this servlet is presented in Fig. 12.4. 12.3.1 listen2 Module The listen Module establishes a socket (in port 3333). Whenever it receives a request from the client, an addServer2 thread will be invoked by the following statements: P1: OTE/SPH P2: OTE SVNY285-Loo October 18, 2006 7:9 Server Side Program 167 import java.net.*; import java.io.*; public class invokeServer2 { public static void main(String[] args) throws IOException { int Thread id=1; int totalProc =0; timert=newtimer(); // ************ get parameter from command argument ******* if (args.length >= 1) { totalProc = Integer.parseInt(args[0]); System.out.println(  number of servers :  + totalProc); } else { System.out.println(  number of servers  ); System.out.println(  , name of IP file  ); System.exit(-1); } String ipFile=  ip.txt  ; // default file name if (args.length >=2) ipFile= args[1]; String ip[]=new String[totalProc+1]; readIpFile reader= new readIpFile(ipFile,ip); System.out.println(  invoked  + Thread id); // *************************** ending operation *************** timer t1= new timer(); t1.start(); for (int i=0;i<totalProc;i++) { new startServlet2(ip[i]).run(); } t1.elasp(  ********************************* It takes :  ); System.out.println(  program terminated  ); } // end of main method } Figure 12.2. invokeServer2.java while (listening) { try { System.out.println(  tring to connect Thread :  + Thread id); new addServerThread2(serverSocket.accept()).start(); [...]... second parameter (‘2’) means two servers The contents of the office.txt file in this example are: 3 202.40.1 97. 26 202.40.1 97. 173 202.40.1 97. 69 The following figures show the screens of client side program (Fig 12.9) and server side programs (Fig 12.10) P1: OTE/SPH SVNY285-Loo P2: OTE October 18, 2006 172 7: 9 12 Power Server: Model 4 Figure 12.9 Client screen (with three servers in the network) Figure 12.10... addServlet count = +count); count++; output.close(); }// end of method public void destroy() { System.out.println( destroy method called ); } } Figure 13.2 addServlet5.java 177 P1: OTE/SPH SVNY285-Loo P2: OTE October 18, 2006 178 7: 14 13 Power Server: Model 5 addClient5 addClientThread5 Share2 divide divide timer readIpFile HandleHttp Figure 13.3 Structure of the client program r An HTTP message is sent... SVNY285-Loo P2: OTE October 18, 2006 174 7: 9 12 Power Server: Model 4 Figure 12.14 Screen of third server (with three servers) 12.5.2 Further Testing As the server side program is running on the servers, you do not need to type anything on the server Try different configurations until you understand the operations of this model P1: OTE/SPH SVNY285-Loo P2: OTE October 18, 2006 7: 14 13 Power Server: Model 5... following ways: r All sub-tasks will be sent to the power server with HTTP message r Servers will not listen in port 3333 r Client computers will not use sockets for communication 175 P1: OTE/SPH SVNY285-Loo P2: OTE October 18, 2006 176 7: 14 13 Power Server: Model 5 addServer5 add timer Figure 13.1 Structure of server program 13.3 Server Side Program The logic of this program is simpler as the actual communications... (IOException e) { System.err.println( accept failed ); } }// end of while }// end of method } Figure 12.5 listen2.java P1: OTE/SPH SVNY285-Loo P2: OTE October 18, 2006 7: 9 Testing the invokeServer2 Program 171 Figure 12.6 Server window Figure 12 .7 Client window Figure 12.8 Screen of the browser 12.4.3 Test 2 In this test, you use several computers to test the programs in this chapter with the following steps:... local.txt Figure 13 .7 Screen of client (with one server) P1: OTE/SPH SVNY285-Loo P2: OTE October 18, 2006 184 7: 14 13 Power Server: Model 5 Figure 13.8 Screen of client (with one server) Figure 13.10 shows the screen of the client, while Figs 13.11 and 13.12 show the screens of server 1 and server 2 Figure 13.9 Screen of client (with two servers) P1: OTE/SPH SVNY285-Loo P2: OTE October 18, 2006 7: 14 Testing... http://localhost:8080/servlet/addServlet2 on your browser You will get the screen as shown in Fig 12.8 on your browser if the server side is working properly P1: OTE/SPH SVNY285-Loo P2: OTE October 18, 2006 170 7: 9 12 Power Server: Model 4 import java.io.*; import java.net.*; import java.util.*; public class listen2 extends Thread { Socket addSocket= null; ServerSocket serverSocket = null; boolean listening... servers Type the following command in your client computer: java addClient2 5 100 office.txt Figure 12.11 Client screen (with three servers) P1: OTE/SPH SVNY285-Loo P2: OTE October 18, 2006 7: 9 Testing the System 173 Figure 12.12 Screen of first server (with three servers) The job is divided into five sub-tasks They are distributed to three servers As you can see from the following figures, server 1 is... Module This is a new module, and it provides the following functions: r Accept input data from the calling module (i.e., addClientThread5) P1: OTE/SPH SVNY285-Loo P2: OTE October 18, 2006 7: 14 Client Side Program 179 import java.net.*; import java.io.*; public class addClient5 { public static void main(String[] args) throws IOException { int totalTasks = 0; //************ get parameter from command... return it to the calling module The source codes of this module are presented in Fig 13 .7 13.5 Testing Testing is simpler than previous models As everything on the server computer is controlled by the web server, all we need is to type the command from the client P1: OTE/SPH SVNY285-Loo P2: OTE October 18, 2006 7: 14 Testing 1 2 3 4 183 Copy all programs to the default path of your servers Make sure . means two servers. The contents of the office.txt file in this example are: 3 202.40.1 97. 26 202.40.1 97. 173 202.40.1 97. 69 The following figures show the screens of client side program (Fig. 12.9) and server. listen2.java P1: OTE/SPH P2: OTE SVNY285-Loo October 18, 2006 7: 9 Testing the invokeServer2 Program 171 Figure 12.6. Server window Figure 12 .7. Client window Figure 12.8. Screen of the browser 12.4.3. 3333. r Client computers will not use sockets for communication. 175 P1: OTE/SPH P2: OTE SVNY285-Loo October 18, 2006 7: 14 176 13. Power Server: Model 5 addServer5 add timer Figure 13.1. Structure

Ngày đăng: 07/08/2014, 17:21

Xem thêm: peer-topeer Networks phần 7 pot