refactoring
This commit is contained in:
parent
0b043a0715
commit
cec961f46a
@ -18,10 +18,10 @@ public class ConnectController implements ActionListener
|
||||
private ChatView view;
|
||||
private static Logger logger = OhmLogger.getLogger();
|
||||
|
||||
public ConnectController(Transmitter m, ChatView v)
|
||||
public ConnectController(Transmitter model, ChatView view)
|
||||
{
|
||||
this.model = m;
|
||||
this.view = v;
|
||||
this.model = model;
|
||||
this.view = view;
|
||||
}
|
||||
|
||||
public void registerEvents()
|
||||
@ -34,9 +34,10 @@ public class ConnectController implements ActionListener
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent ae)
|
||||
{
|
||||
Object o = ae.getSource();
|
||||
Object object = ae.getSource();
|
||||
|
||||
if (o == view.getBtConnect()) {
|
||||
if (object == view.getBtConnect())
|
||||
{
|
||||
int port = -1;
|
||||
|
||||
try {
|
||||
@ -59,37 +60,43 @@ public class ConnectController implements ActionListener
|
||||
view.getRbServer().setEnabled(false);
|
||||
view.getTfIP().setEnabled(false);
|
||||
view.getTfPort().setEnabled(false);
|
||||
} else {
|
||||
view.getTfIP().setEnabled(view.getRbClient().isSelected());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean validIP (String ip) {
|
||||
try {
|
||||
if ( ip == null || ip.isEmpty() ) {
|
||||
private boolean validIP (String ip)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (ip == null || ip.isEmpty())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
String[] parts = ip.split( "\\." );
|
||||
if ( parts.length != 4 ) {
|
||||
String[] parts = ip.split("\\.");
|
||||
if ( parts.length != 4 )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for ( String s : parts ) {
|
||||
int i = Integer.parseInt( s );
|
||||
if ( (i < 0) || (i > 255) ) {
|
||||
for (String s : parts)
|
||||
{
|
||||
int i = Integer.parseInt(s);
|
||||
if ((i < 0) || (i > 255))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if ( ip.endsWith(".") ) {
|
||||
if (ip.endsWith("."))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (NumberFormatException e) {
|
||||
}
|
||||
catch (NumberFormatException e)
|
||||
{
|
||||
logger.severe(e.toString());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -20,9 +20,9 @@ public class ReceiveAdapterController implements Observer
|
||||
@Override
|
||||
public void update(Observable observable, Object object)
|
||||
{
|
||||
String msg = (String)object;
|
||||
String message = (String)object;
|
||||
|
||||
view.getTaCommunication().append(msg + "\n");
|
||||
view.getTaCommunication().append(message + "\n");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -14,10 +14,10 @@ public class SendController implements ActionListener
|
||||
private Transmitter model;
|
||||
private ChatView view;
|
||||
|
||||
public SendController(Transmitter m, ChatView v)
|
||||
public SendController(Transmitter model, ChatView view)
|
||||
{
|
||||
this.model = m;
|
||||
this.view = v;
|
||||
this.model = model;
|
||||
this.view = view;
|
||||
}
|
||||
|
||||
public void registerEvents()
|
||||
|
@ -43,7 +43,8 @@ public class Client extends Observable implements Runnable
|
||||
|
||||
public void init()
|
||||
{
|
||||
if (thd == null) {
|
||||
if (thd == null)
|
||||
{
|
||||
thd = new Thread(this);
|
||||
thd.start();
|
||||
}
|
||||
@ -51,10 +52,13 @@ public class Client extends Observable implements Runnable
|
||||
|
||||
public void sendMessage(String msg)
|
||||
{
|
||||
if (ready) {
|
||||
if (ready)
|
||||
{
|
||||
writer.println(msg);
|
||||
writer.flush();
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.warning("Server not ready to send message. Connect first");
|
||||
}
|
||||
}
|
||||
@ -66,63 +70,85 @@ public class Client extends Observable implements Runnable
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (socket == null) {
|
||||
try {
|
||||
if (socket == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
socket = new Socket(ip, port);
|
||||
logger.info("Connected to server");
|
||||
} catch (IOException e) {
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
logger.severe(e.getMessage());
|
||||
socket = null;
|
||||
try {
|
||||
try
|
||||
{
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException ex) {
|
||||
}
|
||||
catch (InterruptedException ex)
|
||||
{
|
||||
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
InputStream iStream;
|
||||
try {
|
||||
try
|
||||
{
|
||||
iStream = socket.getInputStream();
|
||||
} catch (IOException e) {
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
logger.severe(e.getMessage());
|
||||
return;
|
||||
}
|
||||
OutputStream oStream;
|
||||
try {
|
||||
try
|
||||
{
|
||||
oStream = socket.getOutputStream();
|
||||
} catch (IOException e) {
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
logger.severe(e.getMessage());
|
||||
return;
|
||||
}
|
||||
|
||||
InputStreamReader isr;
|
||||
try {
|
||||
try
|
||||
{
|
||||
isr = new InputStreamReader(iStream, "UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
}
|
||||
catch (UnsupportedEncodingException e)
|
||||
{
|
||||
logger.severe(e.getMessage());
|
||||
return;
|
||||
}
|
||||
OutputStreamWriter osr;
|
||||
try {
|
||||
try
|
||||
{
|
||||
osr = new OutputStreamWriter(oStream, "UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
}
|
||||
catch (UnsupportedEncodingException e)
|
||||
{
|
||||
logger.severe(e.getMessage());
|
||||
return;
|
||||
}
|
||||
|
||||
reader = new BufferedReader(isr);
|
||||
writer = new PrintWriter(osr);
|
||||
|
||||
writer = new PrintWriter(osr);
|
||||
ready = true;
|
||||
}
|
||||
|
||||
if (ready) {
|
||||
if (ready)
|
||||
{
|
||||
String msg;
|
||||
try {
|
||||
logger.info("Waiting for message");
|
||||
try
|
||||
{
|
||||
logger.info("Waiting");
|
||||
msg = reader.readLine();
|
||||
} catch (IOException e) {
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
logger.severe(e.toString());
|
||||
return;
|
||||
}
|
||||
|
@ -30,9 +30,7 @@ public class Server extends Observable implements Runnable
|
||||
private BufferedReader reader;
|
||||
private PrintWriter writer;
|
||||
private volatile boolean ready;
|
||||
private Thread thd;
|
||||
private static final SimpleDateFormat sdf = new SimpleDateFormat("HH.mm.ss");
|
||||
|
||||
private Thread thd;
|
||||
|
||||
public Server(int port)
|
||||
{
|
||||
@ -56,79 +54,100 @@ public class Server extends Observable implements Runnable
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
logger.info("Running server...");
|
||||
logger.info("Server running");
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (server == null) {
|
||||
try {
|
||||
if (server == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
server = new ServerSocket(port);
|
||||
} catch (IOException e) {
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
logger.severe(e.toString());
|
||||
return;
|
||||
}
|
||||
|
||||
logger.info("Waiting for client to connect");
|
||||
try {
|
||||
logger.info("Waiting for client");
|
||||
try
|
||||
{
|
||||
client = server.accept();
|
||||
} catch (IOException e) {
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
logger.severe(e.toString());
|
||||
return;
|
||||
}
|
||||
logger.info("Client connected");
|
||||
|
||||
InputStream iStream;
|
||||
try {
|
||||
try
|
||||
{
|
||||
iStream = client.getInputStream();
|
||||
} catch (IOException e) {
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
logger.severe(e.toString());
|
||||
return;
|
||||
}
|
||||
OutputStream oStream;
|
||||
try {
|
||||
try
|
||||
{
|
||||
oStream = client.getOutputStream();
|
||||
} catch (IOException e) {
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
logger.severe(e.toString());
|
||||
return;
|
||||
}
|
||||
|
||||
InputStreamReader isr;
|
||||
try {
|
||||
try
|
||||
{
|
||||
isr = new InputStreamReader(iStream, "UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
}
|
||||
catch (UnsupportedEncodingException e)
|
||||
{
|
||||
logger.severe(e.toString());
|
||||
return;
|
||||
}
|
||||
OutputStreamWriter osr;
|
||||
try {
|
||||
try
|
||||
{
|
||||
osr = new OutputStreamWriter(oStream, "UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
}
|
||||
catch (UnsupportedEncodingException e)
|
||||
{
|
||||
logger.severe(e.toString());
|
||||
return;
|
||||
}
|
||||
|
||||
reader = new BufferedReader(isr);
|
||||
writer = new PrintWriter(osr);
|
||||
|
||||
ready = true;
|
||||
|
||||
writer = new PrintWriter(osr);
|
||||
ready = true;
|
||||
}
|
||||
|
||||
if (ready) {
|
||||
try {
|
||||
if (ready)
|
||||
{
|
||||
try
|
||||
{
|
||||
logger.info("Waiting for message");
|
||||
String msg = reader.readLine();
|
||||
if (msg == null) {
|
||||
if (msg == null)
|
||||
{
|
||||
ready = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
this.setChanged();
|
||||
this.notifyObservers(msg);
|
||||
} catch (IOException e) {
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
logger.severe(e.toString());
|
||||
ready = false;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -136,11 +155,13 @@ public class Server extends Observable implements Runnable
|
||||
|
||||
public void sendMessage(String msg)
|
||||
{
|
||||
if (ready) {
|
||||
if (ready)
|
||||
{
|
||||
writer.println(msg);
|
||||
writer.flush();
|
||||
} else {
|
||||
logger.warning("Server not ready to send message. Connect first");
|
||||
} else
|
||||
{
|
||||
logger.warning("Server not ready");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -33,46 +33,53 @@ public class Transmitter extends Observable implements Observer
|
||||
|
||||
public void connectToPeer(boolean mode, String ip, int port)
|
||||
{
|
||||
if (initialized) {
|
||||
logger.info("Chat already running");
|
||||
if (initialized)
|
||||
{
|
||||
logger.info("Chat's already running");
|
||||
return;
|
||||
}
|
||||
|
||||
this.mode = mode;
|
||||
|
||||
if (mode) {
|
||||
if (mode)
|
||||
{
|
||||
logger.info("Running as server");
|
||||
srv = new Server(port);
|
||||
srv.addObserver(this);
|
||||
srv.init();
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.info("Running as client");
|
||||
cli = new Client(ip, port);
|
||||
cli.addObserver(this);
|
||||
cli.init();
|
||||
}
|
||||
|
||||
}
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
public void sendMessage(String msg)
|
||||
{
|
||||
if (!initialized) {
|
||||
if (!initialized)
|
||||
{
|
||||
logger.warning("Chat not initialized");
|
||||
return;
|
||||
}
|
||||
|
||||
if (mode) {
|
||||
if (mode)
|
||||
{
|
||||
srv.sendMessage(msg);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
cli.sendMessage(msg);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Observable o, Object o1)
|
||||
public void update(Observable observable, Object object)
|
||||
{
|
||||
this.setChanged();
|
||||
this.notifyObservers(o1);
|
||||
this.notifyObservers(object);
|
||||
}
|
||||
}
|
||||
|
@ -38,7 +38,7 @@
|
||||
<Component id="lbIP" min="-2" max="-2" attributes="0"/>
|
||||
<EmptySpace max="-2" attributes="0"/>
|
||||
<Component id="tfIP" min="-2" pref="191" max="-2" attributes="0"/>
|
||||
<EmptySpace pref="34" max="32767" attributes="0"/>
|
||||
<EmptySpace pref="247" max="32767" attributes="0"/>
|
||||
<Component id="btConnect" min="-2" max="-2" attributes="0"/>
|
||||
</Group>
|
||||
<Group type="102" alignment="0" attributes="0">
|
||||
@ -63,7 +63,7 @@
|
||||
<Group type="103" groupAlignment="0" attributes="0">
|
||||
<Group type="102" alignment="0" attributes="0">
|
||||
<EmptySpace max="-2" attributes="0"/>
|
||||
<Component id="jScrollPane1" pref="134" max="32767" attributes="0"/>
|
||||
<Component id="jScrollPane1" pref="243" max="32767" attributes="0"/>
|
||||
<EmptySpace type="separate" max="-2" attributes="0"/>
|
||||
<Group type="103" groupAlignment="3" attributes="0">
|
||||
<Component id="tfMessage" alignment="3" min="-2" pref="25" max="-2" attributes="0"/>
|
||||
@ -114,7 +114,7 @@
|
||||
</Component>
|
||||
<Component class="javax.swing.JTextField" name="tfPort">
|
||||
<Properties>
|
||||
<Property name="text" type="java.lang.String" value="3210"/>
|
||||
<Property name="text" type="java.lang.String" value="45000"/>
|
||||
<Property name="toolTipText" type="java.lang.String" value=""/>
|
||||
</Properties>
|
||||
</Component>
|
||||
|
@ -119,7 +119,7 @@ public class ChatView extends javax.swing.JFrame {
|
||||
|
||||
btSend.setText("send");
|
||||
|
||||
tfPort.setText("3210");
|
||||
tfPort.setText("45000");
|
||||
tfPort.setToolTipText("");
|
||||
|
||||
lbPort.setText("Port:");
|
||||
@ -164,7 +164,7 @@ public class ChatView extends javax.swing.JFrame {
|
||||
.addComponent(lbIP)
|
||||
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
|
||||
.addComponent(tfIP, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE)
|
||||
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
|
||||
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 247, Short.MAX_VALUE)
|
||||
.addComponent(btConnect))
|
||||
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
|
||||
.addComponent(rbClient, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
|
||||
@ -183,7 +183,7 @@ public class ChatView extends javax.swing.JFrame {
|
||||
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
||||
.addGroup(layout.createSequentialGroup()
|
||||
.addContainerGap()
|
||||
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
|
||||
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 243, Short.MAX_VALUE)
|
||||
.addGap(18, 18, 18)
|
||||
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
|
||||
.addComponent(tfMessage, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
|
||||
|
Loading…
x
Reference in New Issue
Block a user