Browse Source

add missing controller package

master
Hannes Dürr 3 years ago
parent
commit
45e094d635

+ 81
- 0
src/netz/controller/CommandConnect.java View File

/*
* 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.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Logger;
import ohmlogger.OhmLogger;
import netz.model.ChatModel;
import netz.view.ChatView;

/**
*
* @author hd, chris
*/
public class CommandConnect implements ActionListener
{
private static Logger lg = OhmLogger.getLogger();
ChatView view;
ChatModel model;
private static final int PORT = 35000;
private static final String IP_ADRESSE = "127.0.0.1";
public CommandConnect(ChatView view, ChatModel transmitter)
{
this.view = view;
this.model = transmitter;
}
public void registerEvents(){
view.getBtnSetServer().addActionListener(this);
view.getBtnSetClient().addActionListener(this);
}

@Override
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();

// connect to server
if(src == view.getBtnSetServer()){
synchronized (this){
try {
view.getLblType().setText("Server");
ServerSocket sSocket = new ServerSocket(PORT);
lg.info("Server: Warte auf Verbindung ...");
Socket s = sSocket.accept(); // Achtung: blockiert!
lg.info("Server: Verbindung akzeptiert");
model.setSocket(s);
} catch (IOException ex) {
lg.severe(ex.toString());
}
}
}

// connect to vlient
if(src == view.getBtnSetClient()){
synchronized (this){
try {
view.getLblType().setText("Client");
lg.info("Client: verbinde ...");
Socket s = new Socket(IP_ADRESSE, PORT); // Achtung: blockiert!
lg.info("Client: Verbindung hergestellt");
model.setSocket(s);
} catch (IOException ex) {
lg.severe(ex.toString());
}
}
}
}
}

+ 52
- 0
src/netz/controller/CommandSend.java View File

/*
* 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 ohmlogger.OhmLogger;
import netz.model.ChatModel;
import netz.view.ChatView;

/**
*
* @author hd, christ
*/
public class CommandSend implements ActionListener
{
private static Logger lg = OhmLogger.getLogger();
ChatView view;
ChatModel model;
public CommandSend(ChatView view, ChatModel model)
{
this.view = view;
this.model = model;
}
public void registerEvents(){
view.getBtnSend().addActionListener(this);
}

@Override
public void actionPerformed(ActionEvent e)
{
Object src = e.getSource();
if(src == view.getBtnSend()){
String msg = view.getTxtField().getText();
if(msg != ""){
model.sendMessage(msg);
view.getLblStatusDialog().setText("Nachricht gesendet");
view.getjTextArea().append("Ich: " + msg +"\n");
}
view.getTxtField().setText("");
}
}
}

+ 63
- 0
src/netz/controller/ReceiveAdapter.java View File

/*
* 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.util.concurrent.Flow;
import java.util.logging.Logger;
import ohmlogger.OhmLogger;
import netz.model.ChatModel;
import netz.view.ChatView;

/**
*
* @author hd, chris
*/

public class ReceiveAdapter implements Flow.Subscriber<String>
{
private static Logger lg = OhmLogger.getLogger();
private ChatView view;
private ChatModel model;
private Flow.Subscription subscription;
public ReceiveAdapter(ChatView view, ChatModel model)
{
this.view = view;
this.model = model;
}
public void subscribe()
{
model.addSubscription(this);
}

@Override
public void onSubscribe(Flow.Subscription subscription)
{
this.subscription = subscription;
subscription.request(1);
}

@Override
public void onNext(String item)
{
view.getLblStatusDialog().setText("Nachricht empfangen");
view.getjTextArea().append("Partner: " + item + "\n");
//System.out.println(item);
}

@Override
public void onError(Throwable throwable)
{
}

@Override
public void onComplete()
{
}
}

Loading…
Cancel
Save