3. mud study Connection
package driver;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.LinkedList;
import lombok.Getter;
public class Connection {
private Socket socket;
private @Getter PrintWriter output;
private @Getter BufferedReader input;
private @Getter Player player;
private @Getter CustomBuffer inputBuffer;
private @Getter CustomBuffer outputBuffer;
private @Getter boolean isConnected;
public Connection(Socket s, Player p){
try {
socket = s;
output = new PrintWriter(socket.getOutputStream(),true);
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
player=p;
inputBuffer = new CustomBuffer();
outputBuffer = new CustomBuffer();
isConnected = true;
} catch (IOException e){
// TODO: handle exception
e.printStackTrace();
}
}
public synchronized void send(String message){
outputBuffer.addBuff(message);
}
public synchronized void sendFinal(String message){
outputBuffer.addBuff(message);
outputBuffer.addBuff( ANSI.YELLOW+"[" + ANSI.WHITE+player.getName()+ ANSI.YELLOW + "]" + ANSI.SANE);
}
public void disconnect(){
isConnected = false;
try{
output.flush();
output.close();
input.close();
socket.close();
}
catch (IOException e){
e.printStackTrace();
}
}
public synchronized boolean isConnected(){
return isConnected;
}
}
class CustomBuffer{
LinkedList linkedList ;
public CustomBuffer(){
linkedList = new LinkedList();
}
public synchronized void addBuff(String str){
linkedList.add(str);
}
public synchronized String getBuff(){
return linkedList.size() > 0?linkedList.poll():null;
}
}
package driver;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import lombok.Getter;
import lombok.Setter;
class ConnectionBuilder extends Thread{
private Socket socket;
private PrintWriter output;
private BufferedReader input;
private ConnectionList connectionList;
private Connection connection;
public ConnectionBuilder(Socket s, ConnectionList cl) {
try {
socket = s;
output = new PrintWriter(socket.getOutputStream(),true);
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
this.connectionList = cl;
connection = null;
start();
} catch (IOException e) {
// TODO: handle exception
}
}
@Override
public void run() {
// TODO Auto-generated method stub
output.println("Welcome!!!");
output.println("Enter Your id? : ");
String userInput = null;
try {
while(userInput == null){
userInput = input.readLine();
if(userInput !=null){
Player player = new Player(userInput);
connection = new Connection(socket,player);
player.setConnection(connection);
connectionList.add(connection);
if(connectionList !=null){
for(Connection conn : connectionList.getConnectionList()){
conn.sendFinal(ANSI.GREEN + "### just connected " + player.getName() + ANSI.SANE);
}
}
}
sleep(500);
}
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package driver;
import java.util.LinkedList;
public class ConnectionList {
private LinkedList connectionList;
public ConnectionList() {
// TODO Auto-generated constructor stubs
connectionList = new LinkedList();
}
public synchronized void add(Connection cl){
if (connectionList.contains(cl) == false){
connectionList.add(cl);
}
}
public synchronized void remove(Connection cl){
if (connectionList.contains(cl) == true)
{
connectionList.remove(cl);
}
}
public synchronized LinkedList getConnectionList(){
return connectionList;
}
}
package driver;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
class ConnectionListener extends Thread{
private ServerSocket serverSocket;
private ConnectionList connectionList;
public ConnectionListener(ConnectionList cl) {
try {
serverSocket = new ServerSocket(8888);
this.connectionList = cl;
start();
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
@Override
public void run() {
try {
while(true){
Socket socket = serverSocket.accept();
if(socket !=null){
new ConnectionBuilder(socket,connectionList);
}
sleep(500);
}
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package driver;
import driver.ConnectionList;
public class GMain {
private ConnectionList connectionList;
public GMain() {
// TODO Auto-generated constructor stub
connectionList = new ConnectionList();
new ConnectionListener(connectionList);
new OutputDriver(connectionList);
}
public static void main(String[] arge){
new GMain();
}
}
package driver;
public class OutputDriver extends Thread{
private ConnectionList connectionList;
public OutputDriver(ConnectionList cL){
connectionList = cL;
start();
}
public void run(){
while(true){
for (Connection connection : connectionList.getConnectionList()){
String str = connection.getOutputBuffer().getBuff();
if ((str != null) && !str.equals("")){
System.out.println("str /: "+str);
connection.getOutput().println(str);
}//if
connection.getOutput().flush();
}//for
try{
sleep(5);
}catch(InterruptedException e){
e.printStackTrace();
}
}//while
}
}
package driver;
import lombok.Getter;
import lombok.Setter;
class Player {
private @Getter @Setter Connection connection;
private @Getter @Setter String uniqueID;
private @Getter @Setter String name;
public Player() {
// TODO Auto-generated constructor stub
uniqueID ="" + System.currentTimeMillis();
}
public Player(String id){
uniqueID ="" + System.currentTimeMillis();
this.name = id;
}
}
================================================================================== result :
lombok(Jar)URL: http://ask39.blogspot.kr/2015/10/eclipse-lombok.html LinkedList Example URL : http://ask39.blogspot.kr/2015/11/linkedlist-example.html

