Created
July 18, 2011 02:28
-
-
Save eduardodaluz/1088426 to your computer and use it in GitHub Desktop.
Arquivos para o post de Multicast BitMasters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.edu.utfpr.ap.sisdistr.multicast; | |
public class Main { | |
public static void main(String[] args) { | |
MulticastView view = new MulticastView(); | |
view.initialize(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.edu.utfpr.ap.sisdistr.multicast; | |
import java.io.IOException; | |
import java.net.DatagramPacket; | |
import java.net.MulticastSocket; | |
import javax.swing.JTextArea; | |
public class MulticastListener extends Thread { | |
MulticastSocket multicast; | |
DatagramPacket pacote; | |
JTextArea area; | |
public MulticastListener(MulticastSocket multicast) { | |
this.multicast = multicast; | |
} | |
public MulticastListener(MulticastSocket multicast, JTextArea area) { | |
this.multicast = multicast; | |
this.area = area; | |
} | |
@Override | |
public void run() { | |
while(true) { | |
try { | |
byte[] buffer = new byte[1000]; | |
pacote = new DatagramPacket(buffer, buffer.length); | |
multicast.receive(pacote); | |
String msg = "Msg from " + pacote.getAddress() + " >> " + new String(pacote.getData()); | |
if (area != null) { | |
area.append("\n" + msg); | |
} else { | |
System.out.println(msg); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.edu.utfpr.ap.sisdistr.multicast; | |
import java.io.IOException; | |
import java.net.InetAddress; | |
import java.net.MulticastSocket; | |
import java.net.UnknownHostException; | |
public class MulticastPeer { | |
InetAddress grupo; | |
MulticastSocket multicast; | |
public MulticastPeer(String ip, int porta) { | |
try { | |
grupo = InetAddress.getByName(ip); | |
multicast = new MulticastSocket(porta); | |
System.out.println(multicast.getLocalPort()); | |
} catch (UnknownHostException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
public void joinChat() { | |
try { | |
multicast.joinGroup(grupo); | |
} catch (UnknownHostException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
public void leaveChat() { | |
try { | |
multicast.leaveGroup(grupo); | |
} catch (UnknownHostException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
public MulticastSocket getSocket() { | |
return multicast; | |
} | |
public InetAddress getGrupo() { | |
return grupo; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.edu.utfpr.ap.sisdistr.multicast; | |
import java.io.IOException; | |
import java.net.DatagramPacket; | |
import java.net.InetAddress; | |
import java.net.MulticastSocket; | |
public class MulticastSender { | |
InetAddress grupo; | |
MulticastSocket multicast; | |
DatagramPacket pacote; | |
public MulticastSender(MulticastSocket multicast, InetAddress grupo) { | |
this.multicast = multicast; | |
this.grupo = grupo; | |
} | |
public void sendMsg(String msg) { | |
try { | |
byte[] buffer = msg.getBytes(); | |
pacote = new DatagramPacket(buffer, buffer.length, grupo, multicast.getLocalPort()); | |
multicast.send(pacote); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.edu.utfpr.ap.sisdistr.multicast; | |
import java.awt.BorderLayout; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import javax.swing.BoxLayout; | |
import javax.swing.JButton; | |
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
import javax.swing.JTextArea; | |
import javax.swing.JTextField; | |
public class MulticastView extends JFrame { | |
/** | |
* | |
*/ | |
private static final long serialVersionUID = -1202136001945439361L; | |
private JPanel painel; | |
private JButton entrar; | |
private JButton sair; | |
private JTextArea mensagens; | |
private JTextField mensagem; | |
private JButton enviar; | |
MulticastPeer peer; | |
MulticastListener listener; | |
MulticastSender sender; | |
public MulticastView() { | |
super("Chat Multicast - UTFPR AP."); | |
setSize(800, 600); | |
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
setLocationRelativeTo(null); | |
peer = new MulticastPeer("229.50.50.50", 7010); | |
} | |
public void initialize() { | |
createGroup(); | |
// pack(); | |
setVisible(true); | |
} | |
private void createGroup() { | |
painel = new JPanel(); | |
add(painel); | |
painel.setLayout(new BorderLayout()); | |
// Painel no page start | |
JPanel pageStart = new JPanel(); | |
painel.add(pageStart, BorderLayout.PAGE_START); | |
// Bt. entrar. | |
entrar = new JButton("Entrar"); | |
pageStart.add(entrar); | |
entrar.addActionListener(new EntrarListener()); | |
// Bt. Sair. | |
sair = new JButton("Sair"); | |
pageStart.add(sair); | |
sair.addActionListener(new SairListener()); | |
sair.setEnabled(false); | |
// Area de mensagens. | |
mensagens = new JTextArea(); | |
mensagens.setEditable(false); | |
painel.add(mensagens, BorderLayout.CENTER); | |
JPanel pageEnd = new JPanel(); | |
pageEnd.setLayout(new BoxLayout(pageEnd, BoxLayout.LINE_AXIS)); | |
painel.add(pageEnd, BorderLayout.PAGE_END); | |
mensagem = new JTextField(); | |
pageEnd.add(mensagem); | |
mensagem.addActionListener(new EnviarListener()); | |
enviar = new JButton("Enviar"); | |
pageEnd.add(enviar); | |
enviar.addActionListener(new EnviarListener()); | |
} | |
private void ativarBt(boolean ativar) { | |
entrar.setEnabled(!ativar); | |
sair.setEnabled(ativar); | |
mensagem.setEnabled(ativar); | |
enviar.setEnabled(ativar); | |
} | |
class EntrarListener implements ActionListener { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
peer.joinChat(); | |
sender = new MulticastSender(peer.getSocket(), peer.getGrupo()); | |
listener = new MulticastListener(peer.getSocket(), mensagens); | |
listener.start(); | |
mensagens.append("\nEntrei no chat..."); | |
ativarBt(true); | |
mensagem.requestFocus(); | |
} | |
} | |
class SairListener implements ActionListener { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
peer.leaveChat(); | |
mensagens.append("\nSai do chat..."); | |
ativarBt(false); | |
} | |
} | |
class EnviarListener implements ActionListener { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
if (!mensagem.getText().equals("")) { | |
sender.sendMsg(mensagem.getText()); | |
// mensagens.append("\nEu: " + mensagem.getText()); | |
mensagem.setText(""); | |
mensagem.requestFocus(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment