@@ -9,13 +9,18 @@ package netz; | |||
import java.io.IOException; | |||
import java.net.MalformedURLException; | |||
import java.net.URL; | |||
import javax.swing.JFrame; | |||
import javax.swing.WindowConstants; | |||
import netz.controller.BtnController; | |||
import netz.controller.ChatController; | |||
import netz.controller.ReceiveAdapter; | |||
import netz.controller.CommandConnect; | |||
import netz.controller.CommandSend; | |||
import netz.model.ChatModel; | |||
import netz.view2.ChatView; | |||
import netz.view.ChatView; | |||
import java.util.logging.Logger; | |||
import javax.swing.JOptionPane; | |||
import javax.swing.UIManager; | |||
import ohmlogger.OhmLogger; | |||
/** | |||
* Builder Class | |||
@@ -23,27 +28,10 @@ import netz.view2.ChatView; | |||
*/ | |||
public class Start | |||
{ | |||
public Start() throws MalformedURLException, IOException | |||
private static Logger lg = OhmLogger.getLogger(); | |||
public Start() | |||
{ | |||
// URL oUrl = new URL(urlString + "/" + dateiname); | |||
// InputStream iStream = oUrl.openStream(); | |||
// BufferedInputStream in = new BufferedInputStream(iStream); | |||
// | |||
// String tmpVerzeichnis = System.getProperty("java.io.tmpdir"); | |||
// String ausgabeDateiname = tmpVerzeichnis + File.separator + dateiname; | |||
// | |||
// FileOutputStream fos = new FileOutputStream(ausgabeDateiname); | |||
// BufferedOutputStream out = new BufferedOutputStream(fos); | |||
// | |||
// int wert = 0; | |||
// | |||
// while ( (wert = in.read()) >= 0) | |||
// { | |||
// out.write(wert); | |||
// } | |||
// in.close(); | |||
// out.close(); // flush! | |||
// System.out.println("Datei " + ausgabeDateiname + " erfolgreich erstellt"); | |||
JFrame frm = new JFrame(); | |||
frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); | |||
@@ -51,35 +39,33 @@ public class Start | |||
ChatView view = new ChatView(); | |||
ChatModel model = new ChatModel(); | |||
BtnController btncontroller = new BtnController(view, model); | |||
ChatController chatcontroller = new ChatController(view, model); | |||
btncontroller.registerEvents(); | |||
chatcontroller.registerEvents(); | |||
CommandConnect cmdConnect = new CommandConnect(view, model); | |||
cmdConnect.registerEvents(); | |||
CommandSend cmdSend = new CommandSend(view, model); | |||
cmdSend.registerEvents(); | |||
ReceiveAdapter recAdapter = new ReceiveAdapter(view, model); | |||
recAdapter.subscribe(); | |||
view.setVisible(true); | |||
view.setTitle("Chat"); | |||
view.setSize(800, 600); | |||
view.setVisible(true); | |||
} | |||
public static void main(String[] args) | |||
{ | |||
try | |||
{ | |||
new Start(); | |||
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); | |||
} | |||
catch (Exception ex) | |||
{ | |||
System.err.println(ex); | |||
ex.printStackTrace(); | |||
} | |||
try | |||
{ | |||
new Start(); | |||
} | |||
catch (Exception ex) | |||
{ | |||
System.err.println(ex); | |||
ex.printStackTrace(); | |||
JOptionPane.showMessageDialog(null, ex.toString()); | |||
} | |||
new Start(); | |||
} | |||
} | |||
} |
@@ -1,74 +0,0 @@ | |||
/* | |||
* To change this license header, choose License Headers in Project Properties. | |||
* To change this template file, choose Tools | Templates | |||
* and open the template in the editor. | |||
*/ | |||
package netz.controller; | |||
import java.awt.event.ActionEvent; | |||
import java.awt.event.ActionListener; | |||
import java.util.logging.Logger; | |||
import netz.model.ChatModel; | |||
import netz.view2.ChatView; | |||
import ohmlogger.OhmLogger; | |||
/** | |||
* | |||
* @author chris | |||
*/ | |||
public class BtnController implements ActionListener | |||
{ | |||
private ChatView view; | |||
private ChatModel model; | |||
private static Logger lg = OhmLogger.getLogger(); | |||
/** | |||
* | |||
* @param view | |||
* @param model | |||
*/ | |||
public BtnController(ChatView view, ChatModel model) | |||
{ | |||
this.view = view; | |||
this.model = model; | |||
} | |||
public void registerEvents() | |||
{ | |||
view.getBtnSetClient().addActionListener(this); | |||
view.getBtnSetServer().addActionListener(this); | |||
} | |||
@Override | |||
public void actionPerformed(ActionEvent e) | |||
{ | |||
if(e.getSource() == view.getBtnSetClient()) | |||
{ | |||
view.getLblType().setText("Client"); | |||
try | |||
{ | |||
model.startClient(36000, "127.0.0.1",new ChatController(view, model)); | |||
} | |||
catch (Exception ex) | |||
{ | |||
System.err.println(ex); | |||
ex.printStackTrace(); | |||
} | |||
} else { | |||
view.getLblType().setText("Server"); | |||
try | |||
{ | |||
model.startServer(36000, new ChatController(view, model)); | |||
} | |||
catch (Exception ex) | |||
{ | |||
System.err.println(ex); | |||
ex.printStackTrace(); | |||
} | |||
} | |||
} | |||
} |
@@ -1,52 +0,0 @@ | |||
/* | |||
* To change this license header, choose License Headers in Project Properties. | |||
* To change this template file, choose Tools | Templates | |||
* and open the template in the editor. | |||
*/ | |||
package netz.controller; | |||
import java.awt.event.ActionEvent; | |||
import java.awt.event.ActionListener; | |||
import java.util.logging.Logger; | |||
import netz.model.ChatModel; | |||
import netz.view2.ChatView; | |||
import ohmlogger.OhmLogger; | |||
/** | |||
* | |||
* @author chris | |||
*/ | |||
public class ChatController implements ActionListener | |||
{ | |||
private ChatView view; | |||
private ChatModel model; | |||
private static Logger lg = OhmLogger.getLogger(); | |||
/** | |||
* | |||
* @param view | |||
* @param model | |||
*/ | |||
public ChatController(ChatView view, ChatModel model) | |||
{ | |||
this.view = view; | |||
this.model = model; | |||
} | |||
public void registerEvents() | |||
{ | |||
view.getBtnSend().addActionListener(this); | |||
} | |||
@Override | |||
public void actionPerformed(ActionEvent e) | |||
{ | |||
} | |||
public void upddateStatus(String status) | |||
{ | |||
view.getLblStatusDialog().setText(status); | |||
} | |||
} |
@@ -6,6 +6,9 @@ | |||
package netz.model; | |||
import ohmlogger.OhmLogger; | |||
import java.io.BufferedReader; | |||
import java.io.IOException; | |||
import java.io.InputStream; | |||
@@ -13,7 +16,6 @@ import java.io.InputStreamReader; | |||
import java.io.OutputStream; | |||
import java.io.OutputStreamWriter; | |||
import java.io.PrintWriter; | |||
import java.net.ServerSocket; | |||
import java.net.Socket; | |||
import java.util.concurrent.ExecutorService; | |||
import java.util.concurrent.Executors; | |||
@@ -21,119 +23,85 @@ import java.util.concurrent.Flow; | |||
import java.util.concurrent.SubmissionPublisher; | |||
import java.util.logging.Level; | |||
import java.util.logging.Logger; | |||
import netz.controller.ChatController; | |||
import ohmlogger.OhmLogger; | |||
/** | |||
* | |||
* @author chris | |||
* @author hd, chris | |||
*/ | |||
public class ChatModel | |||
public class ChatModel implements Runnable | |||
{ | |||
private static Logger lg = OhmLogger.getLogger(); | |||
private ServerRunnable serverRunnable; | |||
private ClientRunnable clientRunnable; | |||
// private ChatController controller; | |||
private ExecutorService eService; | |||
private SubmissionPublisher<String> iPublisher; | |||
private volatile boolean laufend; | |||
String nachricht; | |||
BufferedReader in; | |||
PrintWriter out; | |||
private Socket socket; | |||
public ChatModel() | |||
{ | |||
laufend = false; | |||
iPublisher = new SubmissionPublisher<>(); | |||
eService = Executors.newSingleThreadExecutor(); | |||
} | |||
public void startServer(int PORT, ChatController chatcontroller) throws IOException | |||
public void addSubscription(Flow.Subscriber<String> subscriber) | |||
{ | |||
ServerSocket sSocket = new ServerSocket(PORT); | |||
serverRunnable = new ServerRunnable(sSocket); | |||
chatcontroller.upddateStatus("Server: Warte auf Verbindung ..."); | |||
iPublisher.subscribe(subscriber); | |||
} | |||
public void startClient(int PORT, String ip_adresse, ChatController chatcontroller) throws IOException | |||
public synchronized void start(){ | |||
laufend = true; | |||
eService.submit(this); | |||
this.notifyAll();//muss sync | |||
lg.info("startet"); | |||
} | |||
@Override | |||
public void run() | |||
{ | |||
while(true){ | |||
try { | |||
nachricht = in.readLine(); | |||
lg.info("Nachricht empfangen: " + nachricht); | |||
iPublisher.submit(nachricht);//wenn neue Nachricht | |||
} catch (IOException ex) { | |||
lg.log(Level.SEVERE, ex.toString()); | |||
} | |||
} | |||
} | |||
public void setSocket(Socket s) throws IOException { | |||
this.socket = s; | |||
InputStream iStream = s.getInputStream(); | |||
OutputStream oStream = s.getOutputStream(); | |||
InputStreamReader isr = new InputStreamReader(iStream, "UTF-8"); | |||
OutputStreamWriter osr = new OutputStreamWriter(oStream, "UTF-8"); | |||
in = new BufferedReader(isr); | |||
//BufferedWriter out = new BufferedWriter(osr); | |||
out = new PrintWriter(osr); | |||
start(); | |||
} | |||
public void sendMessage(String msg) | |||
{ | |||
clientRunnable = new ClientRunnable(ip_adresse, PORT); | |||
chatcontroller.upddateStatus("Client: Warte auf Verbindung ..."); | |||
if(laufend){ | |||
lg.log(Level.INFO, "Sende Nachricht: " + msg); | |||
out.println(msg); | |||
out.flush(); | |||
} | |||
else{ | |||
lg.log(Level.INFO, "Keine Verbindung!"); | |||
} | |||
} | |||
} | |||
//public class Bandit implements Runnable | |||
//{ | |||
// private static Logger lg = OhmLogger.getLogger(); | |||
// | |||
// private BanditInfo info; | |||
// private volatile boolean laufend; | |||
// private ExecutorService eService; | |||
// private SubmissionPublisher<BanditInfo> iPublisher; | |||
// | |||
// public Bandit(int nr) | |||
// { | |||
// info = new BanditInfo(nr); | |||
// synchronized(this) | |||
// { | |||
// laufend = true; | |||
// } | |||
// eService = Executors.newSingleThreadExecutor(); | |||
// iPublisher = new SubmissionPublisher<>(); | |||
// | |||
// } | |||
// | |||
// /** | |||
// * startet würfeln | |||
// */ | |||
// public synchronized void start() | |||
// { | |||
// laufend = true; | |||
// eService.submit(this); | |||
// this.notifyAll(); | |||
// lg.info("Thread startet"); | |||
// } | |||
// | |||
// /** | |||
// * stopt würfeln | |||
// * | |||
// */ | |||
// public void stop() | |||
// { | |||
// synchronized(this) | |||
// { | |||
// laufend = false; | |||
// } | |||
// lg.info("Thread stopt"); | |||
// } | |||
// | |||
// public void addSubscription(Flow.Subscriber<BanditInfo> subscriber) | |||
// { | |||
// iPublisher.subscribe(subscriber); | |||
// } | |||
// | |||
// private synchronized void doWait() | |||
// { | |||
// try | |||
// { | |||
// this.wait(); | |||
// } catch (InterruptedException ex) | |||
// { | |||
// Logger.getLogger(Bandit.class.getName()).log(Level.SEVERE, null, ex); | |||
// } | |||
// } | |||
// | |||
// @Override | |||
// public void run() | |||
// { | |||
// | |||
// while(true) | |||
// { | |||
// | |||
// while(!laufend) | |||
// this.doWait(); | |||
// | |||
// try | |||
// { | |||
// Thread.sleep(10); | |||
// } catch (InterruptedException ex) | |||
// { | |||
// Logger.getLogger(Bandit.class.getName()).log(Level.SEVERE, null, ex); | |||
// } | |||
// info.setZaehlerWert((int)(Math.round(Math.random()*100) % 9) + 1); | |||
// iPublisher.submit(info); | |||
// } | |||
// } | |||
//} | |||
@@ -1,95 +0,0 @@ | |||
/* | |||
* To change this license header, choose License Headers in Project Properties. | |||
* To change this template file, choose Tools | Templates | |||
* and open the template in the editor. | |||
*/ | |||
package netz.model; | |||
import java.io.BufferedReader; | |||
import java.io.IOException; | |||
import java.io.InputStream; | |||
import java.io.InputStreamReader; | |||
import java.io.OutputStream; | |||
import java.io.OutputStreamWriter; | |||
import java.io.PrintWriter; | |||
import java.net.ServerSocket; | |||
import java.net.Socket; | |||
import java.util.concurrent.ExecutorService; | |||
import java.util.concurrent.Executors; | |||
import java.util.logging.Level; | |||
import java.util.logging.Logger; | |||
import ohmlogger.OhmLogger; | |||
/** | |||
* | |||
* @author chris | |||
*/ | |||
public class ClientRunnable implements Runnable | |||
{ | |||
private static Logger lg = OhmLogger.getLogger(); | |||
private String ip_adresse; | |||
private int port; | |||
private ExecutorService eService; | |||
public ClientRunnable(String IP_ADRESSE, int PORT) throws IOException | |||
{ | |||
this.ip_adresse = IP_ADRESSE; | |||
this.port = PORT; | |||
eService = Executors.newSingleThreadExecutor(); | |||
this.start(); | |||
} | |||
public synchronized void start() | |||
{ | |||
eService.submit(this); | |||
this.notifyAll(); | |||
lg.info("Thread startet"); | |||
} | |||
@Override | |||
public void run() | |||
{ | |||
try | |||
{ | |||
lg.info("Client: verbinde ..."); | |||
Socket s = new Socket(ip_adresse, port); // Achtung: blockiert! | |||
lg.info("Client: Verbindung hergestellt"); | |||
InputStream iStream = s.getInputStream(); | |||
OutputStream oStream = s.getOutputStream(); | |||
InputStreamReader isr = new InputStreamReader(iStream, "UTF-8"); | |||
OutputStreamWriter osr = new OutputStreamWriter(oStream, "UTF-8"); | |||
BufferedReader in = new BufferedReader(isr); | |||
//BufferedWriter out = new BufferedWriter(osr); | |||
PrintWriter out = new PrintWriter(osr); | |||
lg.info("Client: Stream initialisiert"); | |||
out.println("Hallo Du Server Du - ich bin der client"); | |||
out.flush(); // wirklich absenden!! | |||
lg.info("Client: Nachricht versendet"); | |||
String quittung = in.readLine(); // Achtung blockiert | |||
lg.info("Client: Quittung empfangen"); | |||
System.out.println("Client: Quittung EMPFANGEN - " + quittung); | |||
out.close(); | |||
in.close(); | |||
} | |||
catch (Exception ex) | |||
{ | |||
Logger.getLogger(ServerRunnable.class.getName()).log(Level.SEVERE, null, ex); | |||
} | |||
} | |||
} |
@@ -1,90 +0,0 @@ | |||
/* | |||
* To change this license header, choose License Headers in Project Properties. | |||
* To change this template file, choose Tools | Templates | |||
* and open the template in the editor. | |||
*/ | |||
package netz.model; | |||
import java.io.BufferedReader; | |||
import java.io.IOException; | |||
import java.io.InputStream; | |||
import java.io.InputStreamReader; | |||
import java.io.OutputStream; | |||
import java.io.OutputStreamWriter; | |||
import java.io.PrintWriter; | |||
import java.net.ServerSocket; | |||
import java.net.Socket; | |||
import java.util.concurrent.ExecutorService; | |||
import java.util.concurrent.Executors; | |||
import java.util.logging.Level; | |||
import java.util.logging.Logger; | |||
import ohmlogger.OhmLogger; | |||
/** | |||
* | |||
* @author chris | |||
*/ | |||
public class ServerRunnable implements Runnable | |||
{ | |||
private static Logger lg = OhmLogger.getLogger(); | |||
private ServerSocket sSocket; | |||
private Socket s; | |||
private ExecutorService eService; | |||
ServerRunnable(ServerSocket sSocket) throws IOException | |||
{ | |||
this.sSocket = sSocket; | |||
eService = Executors.newSingleThreadExecutor(); | |||
lg.info("Server: Warte auf Verbindung ..."); | |||
this.start(); | |||
} | |||
public synchronized void start() | |||
{ | |||
eService.submit(this); | |||
this.notifyAll(); | |||
lg.info("Thread startet"); | |||
} | |||
@Override | |||
public void run() | |||
{ | |||
try | |||
{ | |||
s = sSocket.accept(); // Achtung: blockiert! | |||
lg.info("Server: Verbindung akzeptiert"); | |||
InputStream iStream = s.getInputStream(); | |||
OutputStream oStream = s.getOutputStream(); | |||
InputStreamReader isr = new InputStreamReader(iStream, "UTF-8"); | |||
OutputStreamWriter osr = new OutputStreamWriter(oStream, "UTF-8"); | |||
BufferedReader in = new BufferedReader(isr); | |||
//BufferedWriter out = new BufferedWriter(osr); | |||
PrintWriter out = new PrintWriter(osr); | |||
lg.info("Server: Stream initialisiert"); | |||
lg.info("Server: warte auf Nachricht ..."); | |||
String nachricht = in.readLine(); // Achtung blockiert | |||
lg.info("Server: Nachricht empfangen"); | |||
System.out.println("Server: NACHRICHT EMPFANGEN - " + nachricht); | |||
out.println("Server -> ich habe die Nachricht erhalten"); | |||
lg.info("Server: Quittung versendet"); | |||
out.flush(); // wirklich absenden!! | |||
out.close(); | |||
in.close(); | |||
} | |||
catch (IOException ex) | |||
{ | |||
Logger.getLogger(ServerRunnable.class.getName()).log(Level.SEVERE, null, ex); | |||
} | |||
} | |||
} |
@@ -54,9 +54,9 @@ | |||
</SubComponents> | |||
</Container> | |||
<Component class="javax.swing.JTextField" name="txtField"> | |||
<Properties> | |||
<Property name="text" type="java.lang.String" value="jTextField1"/> | |||
</Properties> | |||
<Events> | |||
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="txtFieldActionPerformed"/> | |||
</Events> | |||
<Constraints> | |||
<Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout" value="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout$BorderConstraintsDescription"> | |||
<BorderConstraints direction="Center"/> | |||
@@ -83,5 +83,25 @@ | |||
</Constraint> | |||
</Constraints> | |||
</Component> | |||
<Container class="javax.swing.JScrollPane" name="jScrollPane1"> | |||
<AuxValues> | |||
<AuxValue name="autoScrollPane" type="java.lang.Boolean" value="true"/> | |||
</AuxValues> | |||
<Constraints> | |||
<Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout" value="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout$BorderConstraintsDescription"> | |||
<BorderConstraints direction="Before"/> | |||
</Constraint> | |||
</Constraints> | |||
<Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/> | |||
<SubComponents> | |||
<Component class="javax.swing.JTextArea" name="jTextArea"> | |||
<Properties> | |||
<Property name="columns" type="int" value="20"/> | |||
<Property name="rows" type="int" value="5"/> | |||
</Properties> | |||
</Component> | |||
</SubComponents> | |||
</Container> | |||
</SubComponents> | |||
</Form> |
@@ -3,7 +3,7 @@ | |||
* To change this template file, choose Tools | Templates | |||
* and open the template in the editor. | |||
*/ | |||
package netz.view2; | |||
package netz.view; | |||
/** | |||
* | |||
@@ -12,6 +12,14 @@ package netz.view2; | |||
public class ChatView extends javax.swing.JFrame | |||
{ | |||
/** | |||
* @return the jTextArea | |||
*/ | |||
public javax.swing.JTextArea getjTextArea() | |||
{ | |||
return jTextArea; | |||
} | |||
/** | |||
* @return the btnSend | |||
*/ | |||
@@ -109,6 +117,8 @@ public class ChatView extends javax.swing.JFrame | |||
txtField = new javax.swing.JTextField(); | |||
btnSend = new javax.swing.JButton(); | |||
lblStatusDialog = new javax.swing.JLabel(); | |||
jScrollPane1 = new javax.swing.JScrollPane(); | |||
jTextArea = new javax.swing.JTextArea(); | |||
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); | |||
setTitle("ChatTool"); | |||
@@ -126,7 +136,13 @@ public class ChatView extends javax.swing.JFrame | |||
getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_START); | |||
txtField.setText("jTextField1"); | |||
txtField.addActionListener(new java.awt.event.ActionListener() | |||
{ | |||
public void actionPerformed(java.awt.event.ActionEvent evt) | |||
{ | |||
txtFieldActionPerformed(evt); | |||
} | |||
}); | |||
getContentPane().add(txtField, java.awt.BorderLayout.CENTER); | |||
btnSend.setText("Send"); | |||
@@ -135,9 +151,20 @@ public class ChatView extends javax.swing.JFrame | |||
lblStatusDialog.setText("\"\""); | |||
getContentPane().add(lblStatusDialog, java.awt.BorderLayout.PAGE_END); | |||
jTextArea.setColumns(20); | |||
jTextArea.setRows(5); | |||
jScrollPane1.setViewportView(jTextArea); | |||
getContentPane().add(jScrollPane1, java.awt.BorderLayout.LINE_START); | |||
pack(); | |||
}// </editor-fold>//GEN-END:initComponents | |||
private void txtFieldActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_txtFieldActionPerformed | |||
{//GEN-HEADEREND:event_txtFieldActionPerformed | |||
// TODO add your handling code here: | |||
}//GEN-LAST:event_txtFieldActionPerformed | |||
/** | |||
* @param args the command line arguments | |||
*/ | |||
@@ -195,8 +222,10 @@ public class ChatView extends javax.swing.JFrame | |||
private javax.swing.JButton btnSetClient; | |||
private javax.swing.JButton btnSetServer; | |||
private javax.swing.JPanel jPanel1; | |||
private javax.swing.JScrollPane jScrollPane1; | |||
private javax.swing.JTextArea jTextArea; | |||
private javax.swing.JLabel lblStatusDialog; | |||
private javax.swing.JLabel lblType; | |||
private javax.swing.JTextField txtField; | |||
// End of variables declaration//GEN-END:variables | |||
} | |||
} |