¼Ò¼³ java.rmi ¼Ò½ºÄڵ堠RMI äÆÃ  pooling±â¹ýÀ»»ç¿ëÇÑäÆÃ  

pooling±â¹ýÀ» »ç¿ëÇÑ Ã¤ÆÃ

PoolingChatServer.java

import java.rmi.*;
public interface PoolingChatServer extends 
Remote {
  
public abstract String[] getMessage(int index) throws 
RemoteException;
  
public abstract void addMessage(String id, String message) throws 
RemoteException;
}

C:\18>javac Chatserver.java

PoolingChatServerImpl.java

import java.rmi.*;
import 
java.util.*;
import 
java.rmi.server.*;
public class PoolingChatServerImpl extends UnicastRemoteObject implements 
PoolingChatServer {
  
protected 
Vector v;
  
public PoolingChatServerImpl() throws 
RemoteException {
    v = 
new 
Vector();
  }
  
public String[] getMessage(int 
index) {
    
int 
size = v.size();
    String[] messages=
new 
String[size-index];
    
for(int i=0
; i<size-index; ++i) {
      messages[i] = (String)v.elementAt(index+i);
    }
    
return 
messages;
  }
  
public void 
addMessage(String id, String message) {
    v.addElement(id+
": "
+message);
  }
  
public static void main(String[] args) throws 
Exception {
    PoolingChatServerImpl ChatServer = 
new 
PoolingChatServerImpl();
    System.out.println(
"ChatServer Start!"
);
    Naming.rebind(
"Chat"
, ChatServer);
  }
}

C:\18>javac PoolingChatServerImpl.java

PoolingChatClient.java

import java.rmi.*;
import 
java.rmi.registry.*;
import 
java.io.*;
public class PoolingChatClient implements 
Runnable {
  
protected static final int WAIT_TIME = 2000
;
  
protected 
String id;
  
protected 
String a;  
  
public 
PoolingChatClient(String id) {
    
this
.id = id;
    System.out.println(id+
"´Ô Ã¤ÆÃ ½ÃÀÛ"
);
  }
  
protected 
PoolingChatServer server;
  
protected 
Thread updater;
  
public synchronized void start() throws 
Exception {
    
if(updater == null
) {
      server = (PoolingChatServer)Naming.lookup(
"Chat"
);
      updater = 
new Thread(this
);
      updater.start();
    }
    BufferedReader br = 
new BufferedReader(new 
InputStreamReader(System.in));
    PoolingChatServer server = 
this
.server;
    
while((a=br.readLine()) != null
){
      
if(a.equals("exit"
)){
        server.addMessage(id, id+
"´Ô ÅðÀå "
);
        System.exit(-
1
);
      }
else
{
        server.addMessage(id, a);
      }
    }
  }
  
public synchronized void 
stop() {
    
if(updater != null
) {
      updater.interrupt();
      updater = 
null
;
      server = 
null
;
    }
  }
  
public void 
run() {  
    
int index = 0
;
    
try
{
      
while
(! Thread.interrupted()) {
        String[] messages = server.getMessage(index);
        
int 
n = messages.length;
        
for(int i=0
; i<n; ++i) {
          System.out.println(messages[i]);
          Thread.sleep(WAIT_TIME);
        }
        index += n;
      }
    } 
catch
(InterruptedException ie) {
      ie.printStackTrace();
    } 
catch
(RemoteException re) {
      re.printStackTrace();
    }
  }  
  
public static void main(String[] args) throws 
Exception {
    
if(args.length != 1
)
      
throw new IllegalArgumentException("ERROR!"
);
    PoolingChatClient c = 
new PoolingChatClient(args[0
]);
    c.start();
  }
}

C:\18>javac PoolingChatClient.java

C:\18>rmic PoolingChatServerImpl
C:\18>start rmiregistry

C:\18>java PoolingChatServerImpl
ChatServer Start!

C:\18>java PoolingChatClient haha
haha´Ô äÆÃ ½ÃÀÛ
hi~!
haha: hi~!
hoho: hoho´Ô ÅðÀå
exit

ÀϹÝÀûÀÎ RMIÀÇ ±â¹ýÀÔ´Ï´Ù.



jabookÀúÀÚ¸íÇÔ
Á¦¸ñ:¼Ò¼³°°Àº¹Ì´ÏÄÚµå
ÀÛ¼ºÀÚ:Àںϸâ¹ö ÀÌÇѼö