Chương III.Lập trình phân tán RMI

17 898 21
Chương III.Lập trình phân tán RMI

Đ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

III. Lập trình phân tán RMI Bài 1: Cài đặt theo mô hình RMI cho thủ tục tính USCLN của hai số nguyên dương a và b package hvktqs.rmi.bai1; import java.rmi.Remote; import java.rmi.RemoteException; /** * Bài 1: Cài đặt theo mô hình RMI cho thủ tục tính USCLN của hai số nguyên dương a và b */ public interface UsclnRmi extends Remote{ public int uscln(int a, int b) throws RemoteException; } package hvktqs.rmi.bai1; import java.rmi.ConnectException; import java.rmi.Naming; public class Client { public Client() { super(); } public static void main(String[] args) { try{ UsclnRmi rmi = (UsclnRmi)Naming.lookup("rmi://localhost/uscln"); System.out.println(rmi.uscln(7, 9)); }catch(ConnectException ce){ System.out.println("Can Not Connect To Server!"); System.exit(1); }catch(Exception e){ System.out.println("Error: " + e.toString()); System.exit(1); } } } package hvktqs.rmi.bai1; import java.rmi.Naming; public class Server { public Server() { super(); } public static void main(String[] args) throws Exception { RMI rmi = new RMI(); Naming.rebind("uscln", rmi); System.out.println("Binding Complete "); } } package hvktqs.rmi.bai1; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; public class RMI extends UnicastRemoteObject implements UsclnRmi{ @SuppressWarnings("compatibility:261358371696166678") private static final long serialVersionUID = 1L; public RMI() throws RemoteException { super(); } public int uscln(int a, int b) throws RemoteException { int t; if(a < b) { t = a; a = b; b = t; } while(b > 0) { t = a%b; a = b; b = t; } return a; } } Bài 2: Cài đặt theo mô hình RMI cho thủ tục kiểm tra đăng nhập theo username và password, thông tin này được lưu ở một CSDL trên server. package hvktqs.rmi.bai2; import java.rmi.Remote; import java.rmi.RemoteException; /** * Bài 2: Cài đặt theo mô hình RMI cho thủ tục kiểm tra đăng nhập theo username và password, * thông tin này được lưu ở một CSDL trên server. */ public interface LoginRmi extends Remote{ public boolean login(String username, String password) throws RemoteException; } package hvktqs.rmi.bai2; import java.rmi.ConnectException; import java.rmi.Naming; public class Client { public Client() { super(); } public static void main(String[] args) { try{ LoginRmi rmi = (LoginRmi)Naming.lookup("rmi://localhost/login"); if(rmi.login("hvktqs", "hvktqs2014")) System.out.println("Login Success"); else System.out.println("Login Failure"); }catch(ConnectException ce){ System.out.println("Can Not Connect To Server!"); System.exit(1); }catch(Exception e){ System.out.println("Error: " + e.toString()); System.exit(1); } } } package hvktqs.rmi.bai2; import java.rmi.Naming; public class Server { public Server() { super(); } public static void main(String[] args) throws Exception { RMI rmi = new RMI(); Naming.rebind("login", rmi); System.out.println("Binding Complete "); } } package hvktqs.rmi.bai2; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; public class RMI extends UnicastRemoteObject implements LoginRmi{ @SuppressWarnings("compatibility:-8550784655214878212") private static final long serialVersionUID = 1L; public RMI() throws RemoteException { super(); } /* * Connect Datasource on Server */ private static Connection getConnect() throws NamingException, SQLException { Connection conn = null; InitialContext context = new InitialContext(); DataSource ds = (DataSource)context.lookup("java:/dbms"); // Name Datasource conn = ds.getConnection(); if (conn == null){ return null; }else{ return conn; } } public boolean login(String username, String password) throws RemoteException{ boolean value = false; Connection connection = null; try { connection = getConnect(); PreparedStatement prepared = connection.prepareCall("SELECT COUNT(1) AS RESULT FROM S_USERS WHERE USER_NAME = '" + username + "' AND PASSWORD = '" + password + "'"); ResultSet result = prepared.executeQuery(); while (result.next()) { if(result.getInt("RESULT") == 1); value = true; } prepared.close(); } catch (Exception e) { System.out.println("Error: " + e.getMessage()); } finally { if (connection != null) { try { connection.close(); } catch (SQLException sqlException) { sqlException.printStackTrace(); } } } return value; } } Bài 3: Viết chương trình ứng dụng công nghệ RMI cho phép Client gửi tới server 1 số nguyên dương và server sẽ xem xét số này có phải là số nguyên tố không.Sau đó gửi kết quả về Client và Client hiển thị kết quả này package hvktqs.rmi.bai3; import java.rmi.Remote; import java.rmi.RemoteException; /** * Bài 3: Viết chương trình ứng dụng công nghệ RMI cho phép Client gửi tới server 1 số nguyên dương * và server sẽ xem xét số này có phải là số nguyên tố không. * Sau đó gửi kết quả về Client và Client hiển thị kết quả này */ public interface NguyentoRmi extends Remote{ public boolean soNguyenTo(int a) throws RemoteException; } package hvktqs.rmi.bai3; import hvktqs.rmi.bai2.LoginRmi; import java.rmi.ConnectException; import java.rmi.Naming; public class Client { public Client() { super(); } public static void main(String[] args) { try{ NguyentoRmi rmi = (NguyentoRmi)Naming.lookup("rmi://localhost/nguyento"); if(rmi.soNguyenTo(4)) System.out.println("la so nguyen to"); else System.out.println("khong la so nguyen to"); }catch(ConnectException ce){ System.out.println("Can Not Connect To Server!"); System.exit(1); }catch(Exception e){ System.out.println("Error: " + e.toString()); System.exit(1); } } } package hvktqs.rmi.bai3; import java.rmi.Naming; public class Server { public Server() { super(); } public static void main(String[] args) throws Exception { RMI rmi = new RMI(); Naming.rebind("nguyento", rmi); System.out.println("Binding Complete "); } } package hvktqs.rmi.bai3; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; public class RMI extends UnicastRemoteObject implements NguyentoRmi{ @SuppressWarnings("compatibility:-1168944100524430180") private static final long serialVersionUID = 1L; public RMI() throws RemoteException { super(); } public boolean soNguyenTo(int a) throws RemoteException{ for(int i = 2 ; i <= Math.sqrt(a) ; i++) { if(a%i == 0) return false; } return true; } } Bài 4: Viết chương trình RMI minh họa từ điển trực tuyến. Server chứa nội dung từ điển Anh-Việt. Client sẽ nhập từ tiếng Anh rồi tra nghĩa tương ứng trên server (chương trình từ điển đơn giản) package hvktqs.rmi.bai4; import java.rmi.Remote; import java.rmi.RemoteException; /** * Bài 4: Viết chương trình RMI minh họa từ điển trực tuyến. * Server chứa nội dung từ điển Anh-Việt. * Client sẽ nhập từ tiếng Anh rồi tra nghĩa tương ứng trên server (chương trình từ điển đơn giản) */ public interface DictionaryRmi extends Remote{ // Từ điển tiếng việt public static String VNI_hello = "Xin Chào"; public static String VNI_success = "Thành Công"; public static String VNI_failure = "Thất Bại"; public static String VNI_school = "Trường Học"; public static String VNI_go = "Đi"; public static String VNI_exit = "Thoát"; public static String VNI_sing = "Hát"; public static String VNI_song = "Bài Hát"; // Từ điển tiếng anh public static String EN_hello = "Hello"; public static String EN_success = "Success"; public static String EN_failure = "Failure"; public static String EN_school = "School"; public static String EN_go = "Go"; public static String EN_exit = "Exit"; public static String EN_sing = "Sing"; public static String EN_song = "Song"; public String translateEngToVni(String eng) throws RemoteException; public String translateVniToEng(String vni) throws RemoteException; } package hvktqs.rmi.bai4; import java.rmi.ConnectException; import java.rmi.Naming; public class Client { public Client() { super(); } public static void main(String[] args) { try{ DictionaryRmi rmi = (DictionaryRmi)Naming.lookup("rmi://localhost/dictionary"); System.out.println("Translate \"hello\" =>" + rmi.translateEngToVni("hello")); System.out.println("Translate \"xin chào\" =>" + rmi.translateVniToEng("xin chào")); }catch(ConnectException ce){ System.out.println("Can Not Connect To Server!"); System.exit(1); }catch(Exception e){ System.out.println("Error: " + e.toString()); System.exit(1); } } } package hvktqs.rmi.bai4; import java.rmi.Naming; public class Server { public Server() { super(); } public static void main(String[] args) throws Exception { RMI rmi = new RMI(); Naming.rebind("dictionary", rmi); System.out.println("Binding Complete "); } } package hvktqs.rmi.bai4; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; public class RMI extends UnicastRemoteObject implements DictionaryRmi{ @SuppressWarnings("compatibility:7796495920999807645") private static final long serialVersionUID = 1L; public RMI() throws RemoteException { super(); } public String translateEngToVni(String eng) throws RemoteException { String value = eng; if(eng.toLowerCase().equalsIgnoreCase(DictionaryRmi.EN_hello)) value = DictionaryRmi.VNI_hello; else if(eng.toLowerCase().equalsIgnoreCase(DictionaryRmi.EN_success)) value = DictionaryRmi.VNI_success; else if(eng.toLowerCase().equalsIgnoreCase(DictionaryRmi.EN_failure)) value = DictionaryRmi.VNI_failure; else if(eng.toLowerCase().equalsIgnoreCase(DictionaryRmi.EN_school)) value = DictionaryRmi.VNI_school; else if(eng.toLowerCase().equalsIgnoreCase(DictionaryRmi.EN_go)) value = DictionaryRmi.VNI_go; else if(eng.toLowerCase().equalsIgnoreCase(DictionaryRmi.EN_exit)) value = DictionaryRmi.VNI_exit; else if(eng.toLowerCase().equalsIgnoreCase(DictionaryRmi.EN_sing)) value = DictionaryRmi.VNI_sing; else if(eng.toLowerCase().equalsIgnoreCase(DictionaryRmi.EN_song)) value = DictionaryRmi.VNI_song; return value; } public String translateVniToEng(String vni) throws RemoteException { String value = vni; if(vni.toLowerCase().equalsIgnoreCase(DictionaryRmi.VNI_hello)) value = DictionaryRmi.EN_hello; else if(vni.toLowerCase().equalsIgnoreCase(DictionaryRmi.VNI_success)) value = DictionaryRmi.EN_success; else if(vni.toLowerCase().equalsIgnoreCase(DictionaryRmi.VNI_failure)) value = DictionaryRmi.EN_failure; else if(vni.toLowerCase().equalsIgnoreCase(DictionaryRmi.VNI_school)) value = DictionaryRmi.EN_school; else if(vni.toLowerCase().equalsIgnoreCase(DictionaryRmi.VNI_go)) value = DictionaryRmi.EN_go; else if(vni.toLowerCase().equalsIgnoreCase(DictionaryRmi.VNI_exit)) value = DictionaryRmi.EN_exit; else if(vni.toLowerCase().equalsIgnoreCase(DictionaryRmi.VNI_sing)) value = DictionaryRmi.EN_sing; else if(vni.toLowerCase().equalsIgnoreCase(DictionaryRmi.VNI_song)) value = DictionaryRmi.EN_song; return value; } } Bài 5: Viết chương trình RMI cho phép Client gửi tới Server số n, Server sẽ tìm tất cả các ước số của n, sau đó trả về cho Client. package hvktqs.rmi.bai5; import java.rmi.Remote; import java.rmi.RemoteException; import java.util.List; /** * Bài 5: Viết chương trình RMI cho phép Client gửi tới Server số n, * Server sẽ tìm tất cả các ước số của n, sau đó trả về cho Client. * * Ước của 1 số a: là những số mà a chia hết. */ public interface TimUocRmi extends Remote{ public List<Integer> timUoc(int n) throws RemoteException; } package hvktqs.rmi.bai5; import java.rmi.ConnectException; import java.rmi.Naming; public class Client { public Client() { super(); } public static void main(String[] args) { try{ TimUocRmi rmi = (TimUocRmi)Naming.lookup("rmi://localhost/timuoc"); System.out.println("Cac uoc so cua 5 la: "); for(int value: rmi.timUoc(5)) System.out.println(value); }catch(ConnectException ce){ System.out.println("Can Not Connect To Server!"); System.exit(1); }catch(Exception e){ System.out.println("Error: " + e.toString()); System.exit(1); } } } package hvktqs.rmi.bai5; import java.rmi.Naming; public class Server { public Server() { super(); } public static void main(String[] args) throws Exception { RMI rmi = new RMI(); Naming.rebind("timuoc", rmi); [...]... return list; } } Bài 6: Viết chương trình RMI cho phép Client gửi tới Server số n, Server sẽ tìm k lớn nhất để 4k bé hơn n, và trả về Client package hvktqs .rmi. bai6; import java .rmi. Remote; import java .rmi. RemoteException; /** * Bài 6: Viết chương trình RMI cho phép Client gửi tới Server số n, * Server sẽ tìm k lớn nhất để 4^k bé hơn n, và trả về Client */ public interface TimkRmi extends Remote{ public... Viết chương trình RMI để Client gửi 2 số nguyên dương n và m tới Server Server sẽ tìm hai chữ số cuối của n luỹ thừa m và trả về Client package hvktqs .rmi. bai8; import java .rmi. Remote; import java .rmi. RemoteException; /** * Bài 8: Viết chương trình RMI để Client gửi 2 số nguyên dương n và m tới Server * Server sẽ tìm hai chữ số cuối của n luỹ thừa m và trả về Client */ public interface LuythuaRmi extends... System.exit(1); } } } package hvktqs .rmi. bai8; import java .rmi. Naming; public class Server { public Server() { super(); } public static void main(String[] args) throws Exception { RMI rmi = new RMI( ); Naming.rebind("luythua", rmi) ; System.out.println("Binding Complete "); } } package hvktqs .rmi. bai8; import java .rmi. RemoteException; import java .rmi. server.UnicastRemoteObject; public class RMI extends UnicastRemoteObject... timHaiSoCuoi(int n, int m) throws RemoteException; } package hvktqs .rmi. bai8; import java .rmi. ConnectException; import java .rmi. Naming; public class Client { public Client() { super(); } public static void main(String[] args) { try{ LuythuaRmi rmi = (LuythuaRmi)Naming.lookup( "rmi: //localhost/luythua"); System.out.print("Hai so cuoi cua 2 luy thua 8 la: " + rmi. timHaiSoCuoi(2, 8)); }catch(ConnectException ce){ System.out.println("Can... toán trên */ public interface BieuthucRmi extends Remote{ public int tinhGiaTri(int a, int b, String operator) throws RemoteException; } package hvktqs .rmi. bai7; import java .rmi. ConnectException; import java .rmi. Naming; public class Client { public Client() { super(); } public static void main(String[] args) { try{ BieuthucRmi rmi = (BieuthucRmi)Naming.lookup( "rmi: //localhost/bieuthuc"); System.out.print("Gia... RMI( ); Naming.rebind("bieuthuc", rmi) ; System.out.println("Binding Complete "); } } package hvktqs .rmi. bai7; import java .rmi. RemoteException; import java .rmi. server.UnicastRemoteObject; public class RMI extends UnicastRemoteObject implements BieuthucRmi{ @SuppressWarnings("compatibility:-3011997292561344094") private static final long serialVersionUID = 1L; public RMI( ) throws RemoteException { super();... timK(int n) throws RemoteException; } package hvktqs .rmi. bai6; public class Client { public Client() { super(); } } package hvktqs .rmi. bai6; public class Server { public Server() { super(); } } package hvktqs .rmi. bai6; import java .rmi. RemoteException; import java .rmi. server.UnicastRemoteObject; public class RMI extends UnicastRemoteObject implements TimkRmi{ @SuppressWarnings("compatibility:3781807156587911576")...System.out.println("Binding Complete "); } } package hvktqs .rmi. bai5; import java .rmi. RemoteException; import java .rmi. server.UnicastRemoteObject; import java.util.ArrayList; import java.util.List; public class RMI extends UnicastRemoteObject implements TimUocRmi{ @SuppressWarnings("compatibility:-2622454186307461687") private static final long serialVersionUID = 1L; public RMI( ) throws RemoteException { super(); }... 3 = " + rmi. tinhGiaTri(5, 3, "+")); }catch (ConnectException ce){ System.out.println("Can Not Connect To Server!"); System.exit(1); }catch(Exception e){ System.out.println("Error: " + e.toString()); System.exit(1); } } } package hvktqs .rmi. bai7; import java .rmi. Naming; public class Server { public Server() { super(); } public static void main(String[] args) throws Exception { RMI rmi = new RMI( ); Naming.rebind("bieuthuc",... @SuppressWarnings("compatibility:3781807156587911576") private static final long serialVersionUID = 1L; public RMI( ) throws RemoteException { super(); } public int timK(int n) throws RemoteException { return 0; } } Bài 7: Nhập vào hai số và một ký hiệu phép toán, tính giá trị của biểu thức được thành lập từ hai số và phép toán trên package hvktqs .rmi. bai7; import java .rmi. Remote; import java .rmi. RemoteException; /** * Bài 7: Nhập vào hai số và một ký . III. Lập trình phân tán RMI Bài 1: Cài đặt theo mô hình RMI cho thủ tục tính USCLN của hai số nguyên dương a và b package hvktqs .rmi. bai1; import java .rmi. Remote; import java .rmi. RemoteException; /** . hiển thị kết quả này package hvktqs .rmi. bai3; import java .rmi. Remote; import java .rmi. RemoteException; /** * Bài 3: Viết chương trình ứng dụng công nghệ RMI cho phép Client gửi tới server 1. NguyentoRmi extends Remote{ public boolean soNguyenTo(int a) throws RemoteException; } package hvktqs .rmi. bai3; import hvktqs .rmi. bai2.LoginRmi; import java .rmi. ConnectException; import java .rmi. Naming; public

Ngày đăng: 17/04/2015, 01:12

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan