Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions RMISSLClientSocketFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import java.io.*;
import java.net.*;
import java.rmi.server.*;
import javax.net.ssl.*;

public class RMISSLClientSocketFactory
implements RMIClientSocketFactory, Serializable {

public Socket createSocket(String host, int port) throws IOException {
SSLSocketFactory factory = (SSLSocketFactory)SSLSocketFactory.getDefault();
SSLSocket socket = (SSLSocket)factory.createSocket(host, port);
return socket;
}

public int hashCode() {
return getClass().hashCode();
}

public boolean equals(Object obj) {
if (obj == this) {
return true;
} else if (obj == null || getClass() != obj.getClass()) {
return false;
}
return true;
}
}
62 changes: 62 additions & 0 deletions RMISSLServerSocketFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.rmi.server.RMIServerSocketFactory;
import java.security.KeyStore;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;

public class RMISSLServerSocketFactory implements RMIServerSocketFactory{
private static SSLServerSocketFactory ssf = null;

final String runRoot = "C:/eclipse/java/WarGame/bin/";
String ksName = runRoot + ".keystore/SSLSocketServerKey";

public RMISSLServerSocketFactory() throws Exception{
try {
KeyStore ks;
KeyManagerFactory kmf;
SSLContext sc ;


char[] keyStorePass = "123456".toCharArray();
char[] keyPass = "123456".toCharArray();

ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream(ksName), keyStorePass);
kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, keyPass);

sc = SSLContext.getInstance("TLS");
sc.init(kmf.getKeyManagers(), null, null);
ssf = sc.getServerSocketFactory();

} catch(Exception e) {
e.printStackTrace();
throw e;
}
}


public ServerSocket createServerSocket(int port) throws IOException {
return ssf.createServerSocket(port);
}

public int hashCode() {
return getClass().hashCode();
}

public boolean equals(Object obj) {
if (obj == this) {
return true;
} else if (obj == null || getClass() != obj.getClass()) {
return false;
}
return true;
}


}
Binary file added SSLSocketServerKey
Binary file not shown.
Binary file added SSLSocketServerKey.cert
Binary file not shown.
172 changes: 143 additions & 29 deletions WarGameClient.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
import java.rmi.AccessException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Scanner;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.rmi.NotBoundException;
import java.net.MalformedURLException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.Scanner;
import java.rmi.NotBoundException;



public class WarGameClient implements Runnable {//handle connection and use rmi function here from client side
// private static final long serialVersionUID = 1L;
private static final int PORT = 2019;

private String id;
boolean chkLog=false;
private WarGame server;
boolean boolean_ReadyBtnStatus;
boolean boolean_HitBtnStatus;
boolean boolean_DropBtnStatus;

ArrayList<String> client;
WarGameGUI gui;
protected WarGameClient(WarGame wg,String id,String pw){
boolean_ReadyBtnStatus=false;
boolean_HitBtnStatus=false;
Expand All @@ -28,72 +38,176 @@ protected WarGameClient(WarGame wg,String id,String pw){
System.out.println("wrong id or wrong passworld");
System.exit(0);
}


}catch(RemoteException re) {
System.out.println("RemoteException: "+re);

}catch(java.lang.ArithmeticException ae) {
System.out.println("java.lang.ArithmeticException: "+ae);
}

}
void drop() {
boolean_DropBtnStatus=gui.returnDropBtnStatus();
try {
server.doDrop(id);//id가 필요한지 모르겠음...

}catch(Exception e) {
e.printStackTrace();
}
}
void hit() {
boolean_HitBtnStatus=gui.returnHitBtnStatus();
try {
server.doHit(id);
}catch(Exception e) {
e.printStackTrace();
}
}

public void run() {
if(chkLog) {
System.out.println(id);
WarGameGUI gui=new WarGameGUI(id);
gui=new WarGameGUI(id);
gui.deactivatedHitDropBtn();
try {
while(server.checkAllReady()==false) {
boolean_ReadyBtnStatus=gui.returnReadyBtnStatus();
server.setReadyStatus(id, boolean_ReadyBtnStatus);
server.setReadyStatus(id, boolean_ReadyBtnStatus);
}
}catch(Exception e) {//디테일하게 예외 잡아 줘야 할 듯.. 예외처리 사이즈가

}catch(Exception e) {//디테일하게 예외 잡아 줘야 할 듯.. 예외처리 사이즈가
e.printStackTrace();
}
gui.activatedHitDropBtn();
System.out.println("game start");//this will be deleted in the end
gui.setGameInfo("game start");
gui.deactivateReadyBtn();

// try {
// do{//게임중... do while인 이유는 마지막 카드가 공격이어서 기회가 남아 있을수도 있다..
// ////////////////////
// }while(server.checkStatus());
// }catch(Exception e) {//디테일하게 예외 잡아 줘야 할 듯..예외처리 사이즈가 커
try {//최종 참가자 리스트 추가
client=server.getClientList();
String tmp=client.get(0);
String tmp2=client.get(1);
gui.setPlayerList("Player1: "+tmp);
gui.setPlayerList("Player2: "+tmp2);
}catch(Exception e) {
e.printStackTrace();
}
// try {//턴 돌아갈때 마다 올라가는 것으로 바꿔야 할것 같다. 1번에 국한되지 말고...
// if(id.contentEquals(server.whosTurn())) {
// String tmp=server.whosTurn();
// sendMsgToAll(tmp+"'s turn\n");
//
// }
//
//// System.out.println(tmp+"'s turn. good luck~!");
//// gui.setGameInfo(tmp+"'s turn. good luck~!");
//// String msg=receiveMsg();
//// gui.setMsgView(msg);
//
//
// }catch(Exception e) {
// e.printStackTrace();
// }
try {
while(server.checkEndingStatus()){//게임중...
while(id.equals(server.whosTurn())){//자신의 턴 동안...
boolean_DropBtnStatus=gui.returnDropBtnStatus();
boolean_HitBtnStatus=gui.returnHitBtnStatus();
if(boolean_DropBtnStatus) {
drop();
gui.setMsgView("플레이어 "+ id+" 가 drop을 했습니다.");
// server.doDrop(id);//id는 안쓰고, serverImpl에서 turn보고 알아서 판단하고 있음. 필요 없는거 확실해지면 그때 지우기.
gui.setDropStatusFalse();//버튼 눌리면 true로 변하기 때문에 변경 안하면 항상 true인 효과 한번 버튼 누르면 다시 false로 변환
//server.changeTurn();
gui.repaint();
}else if(boolean_HitBtnStatus) {
hit();
// server.doHit(id);
gui.setHitStatusFalse();

}else {

}
}
if(!id.equals(server.whosTurn())){//player가 잘못누르는 경우...
boolean_DropBtnStatus=gui.returnDropBtnStatus();
boolean_HitBtnStatus=gui.returnHitBtnStatus();
if(boolean_DropBtnStatus){
System.out.println("it's not your turn..please wait");
gui.setGameInfo("it's not your turn..please wait");
gui.setDropStatusFalse();
}
if(boolean_HitBtnStatus){
System.out.println("it's not your turn..please wait");
gui.setGameInfo("it's not your turn..please wait");
gui.setHitStatusFalse();
}
}
}

// gui.deactivatedHitDropBtn();

}catch(Exception e) {//디테일하게 예외 잡아 줘야 할 듯..예외처리 사이즈가 커
e.printStackTrace();
}
}
}
public void sendMsgToAll(String str) {//행동 취하고 나면 반드시 해당함수를 써서 메시지를 보내야한다. 그리고 바로 receiveMSG도 작동해야한다.
try {
server.sendMsg(str);

}catch(Exception e) {
e.printStackTrace();
}
}
// public String receiveMsg() {
// String msg="";
// try {
// msg=server.receiveMsg();
//// gui.setMsgView(msg+"\n");
//
// }catch(Exception e) {
// e.printStackTrace();
//
// }
// return msg;
// }
public static void main(String[] args) {
String mServer="127.0.0.1";
// String mServer=args[0];
String mServName="WarGame";

System.out.println("Remote Method Invocate to "+mServer+", service name: "+mServName);

try {
System.setProperty("javax.net.ssl.trustStore", "trustedcerts");
System.setProperty("javax.net.ssl.trustStorePasssword", "123456");

String id;
String pw;
Scanner sc=new Scanner(System.in);//this is for temporary
System.out.println("enter id: ");
System.out.print("enter id: ");
id=sc.nextLine();
System.out.println("enter pw: ");
System.out.print("enter pw: ");
pw=sc.nextLine();


Registry registry = LocateRegistry.getRegistry(
InetAddress.getLocalHost().getHostName(), PORT,
new RMISSLClientSocketFactory());

WarGame wg=(WarGame)Naming.lookup("rmi://"+mServer+":1099/"+mServName);
new Thread(new WarGameClient(wg,id,pw)).start();

}catch(MalformedURLException mue) {
System.out.println("MalformedURLException: "+mue);
WarGame c=(WarGame)registry.lookup("WarGame"/*"rmi://"+mServer+":1099/"+mServName*/);
new Thread(new WarGameClient(c,id,pw)).start();

}catch(RemoteException re) {
System.out.println("RemoteException: "+re);
}catch(NotBoundException nbe) {
System.out.println("NotBoundException: "+nbe);
}catch(java.lang.ArithmeticException ae) {
System.out.println("java.lang.ArithmeticException: "+ae);
} catch (UnknownHostException e) {
System.out.println("UnknownHostException: "+e);
}
}

}

14 changes: 11 additions & 3 deletions WarGameGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -234,16 +234,24 @@ public void setPlayerList(String str) {//참가자 리스트를 보여줌
public void deactivateReadyBtn() {
readyBtn.setEnabled(false);
}
public void deactivatedHitDropBtn() {
hitBtn.setEnabled(false);
dropBtn.setEnabled(false);
}
public void activatedHitDropBtn() {
hitBtn.setEnabled(true);
dropBtn.setEnabled(true);
}

public void actionPerformed(ActionEvent ae) {
try {
if(ae.getSource()==dropBtn) {
// System.out.println("drop");
boolean_DropBtn=true;


}
else if(ae.getSource()==hitBtn) {
System.out.println("hit");
boolean_HitBtn=true;
// System.out.println("hit");
}
// else if(ae.getSource()==tf_Send) {
// String msg=tf_Send.getText();
Expand Down
Loading