From bb436701dbf326c867aca4c617d8ca52272879a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=A1vio=20Luiz=20Schiavoni?= Date: Fri, 20 Jan 2017 14:49:29 -0200 Subject: [PATCH] First commit with Desktop ScreenCast Software. Be happy! --- build.xml | 73 ++++++++ src/br/edu/ufsj/sms/GUI/Login.form | 111 +++++++++++ src/br/edu/ufsj/sms/GUI/Login.java | 122 ++++++++++++ src/br/edu/ufsj/sms/GUI/Main.form | 74 ++++++++ src/br/edu/ufsj/sms/GUI/Main.java | 174 ++++++++++++++++++ src/br/edu/ufsj/sms/GUI/ScreenShot.java | 33 ++++ src/br/edu/ufsj/sms/Main.java | 65 +++++++ src/br/edu/ufsj/sms/control/AlarmClock.java | 57 ++++++ .../ufsj/sms/control/ImageTransformation.java | 70 +++++++ src/br/edu/ufsj/sms/net/Message.java | 53 ++++++ src/br/edu/ufsj/sms/net/Socket.java | 87 +++++++++ 11 files changed, 919 insertions(+) create mode 100644 build.xml create mode 100644 src/br/edu/ufsj/sms/GUI/Login.form create mode 100644 src/br/edu/ufsj/sms/GUI/Login.java create mode 100644 src/br/edu/ufsj/sms/GUI/Main.form create mode 100644 src/br/edu/ufsj/sms/GUI/Main.java create mode 100644 src/br/edu/ufsj/sms/GUI/ScreenShot.java create mode 100644 src/br/edu/ufsj/sms/Main.java create mode 100644 src/br/edu/ufsj/sms/control/AlarmClock.java create mode 100644 src/br/edu/ufsj/sms/control/ImageTransformation.java create mode 100644 src/br/edu/ufsj/sms/net/Message.java create mode 100644 src/br/edu/ufsj/sms/net/Socket.java diff --git a/build.xml b/build.xml new file mode 100644 index 0000000..3d81ee7 --- /dev/null +++ b/build.xml @@ -0,0 +1,73 @@ + + + + + + + + + + + Builds, tests, and runs the project ShareMySheet. + + + diff --git a/src/br/edu/ufsj/sms/GUI/Login.form b/src/br/edu/ufsj/sms/GUI/Login.form new file mode 100644 index 0000000..ddffecf --- /dev/null +++ b/src/br/edu/ufsj/sms/GUI/Login.form @@ -0,0 +1,111 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/br/edu/ufsj/sms/GUI/Login.java b/src/br/edu/ufsj/sms/GUI/Login.java new file mode 100644 index 0000000..3f66fef --- /dev/null +++ b/src/br/edu/ufsj/sms/GUI/Login.java @@ -0,0 +1,122 @@ +/* + * 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 br.edu.ufsj.sms.GUI; + +/** + * + * @author flavio + */ +public class Login extends java.awt.Dialog { + + public Login(java.awt.Frame parent, boolean modal) { + super(parent, modal); + initComponents(); + } + + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + // //GEN-BEGIN:initComponents + private void initComponents() { + + jPanel11 = new javax.swing.JPanel(); + jPanel4 = new javax.swing.JPanel(); + jPanel8 = new javax.swing.JPanel(); + jLabel1 = new javax.swing.JLabel(); + jTextField1 = new javax.swing.JTextField(); + jPanel9 = new javax.swing.JPanel(); + jPanel5 = new javax.swing.JPanel(); + jPanel7 = new javax.swing.JPanel(); + jLabel2 = new javax.swing.JLabel(); + jTextField2 = new javax.swing.JTextField(); + jPanel10 = new javax.swing.JPanel(); + jPanel6 = new javax.swing.JPanel(); + jButton1 = new javax.swing.JButton(); + + addWindowListener(new java.awt.event.WindowAdapter() { + public void windowClosing(java.awt.event.WindowEvent evt) { + closeDialog(evt); + } + }); + setLayout(new java.awt.GridLayout(4, 1)); + add(jPanel11); + + jPanel4.setLayout(new java.awt.GridLayout(1, 2, 5, 5)); + jPanel4.add(jPanel8); + + jLabel1.setText("Your name"); + jLabel1.setPreferredSize(new java.awt.Dimension(200, 18)); + jPanel4.add(jLabel1); + + jTextField1.setText("Calouro"); + jPanel4.add(jTextField1); + jPanel4.add(jPanel9); + + add(jPanel4); + + jPanel5.setLayout(new java.awt.GridLayout(1, 2, 5, 5)); + jPanel5.add(jPanel7); + + jLabel2.setText("Port"); + jLabel2.setPreferredSize(new java.awt.Dimension(200, 18)); + jPanel5.add(jLabel2); + + jTextField2.setText("8888"); + jPanel5.add(jTextField2); + jPanel5.add(jPanel10); + + add(jPanel5); + + jButton1.setText(" Ok "); + jButton1.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton1ActionPerformed(evt); + } + }); + jPanel6.add(jButton1); + + add(jPanel6); + + pack(); + }// //GEN-END:initComponents + + /** + * Closes the dialog + */ + private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog + setVisible(false); + }//GEN-LAST:event_closeDialog + + private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed + this.setVisible(false); + }//GEN-LAST:event_jButton1ActionPerformed + + public String getName() { + return jTextField1.getText(); + } + + public int getPort() { + return Integer.parseInt(jTextField2.getText()); + } + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JButton jButton1; + private javax.swing.JLabel jLabel1; + private javax.swing.JLabel jLabel2; + private javax.swing.JPanel jPanel10; + private javax.swing.JPanel jPanel11; + private javax.swing.JPanel jPanel4; + private javax.swing.JPanel jPanel5; + private javax.swing.JPanel jPanel6; + private javax.swing.JPanel jPanel7; + private javax.swing.JPanel jPanel8; + private javax.swing.JPanel jPanel9; + private javax.swing.JTextField jTextField1; + private javax.swing.JTextField jTextField2; + // End of variables declaration//GEN-END:variables +} diff --git a/src/br/edu/ufsj/sms/GUI/Main.form b/src/br/edu/ufsj/sms/GUI/Main.form new file mode 100644 index 0000000..5d36c09 --- /dev/null +++ b/src/br/edu/ufsj/sms/GUI/Main.form @@ -0,0 +1,74 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/br/edu/ufsj/sms/GUI/Main.java b/src/br/edu/ufsj/sms/GUI/Main.java new file mode 100644 index 0000000..4f84b0e --- /dev/null +++ b/src/br/edu/ufsj/sms/GUI/Main.java @@ -0,0 +1,174 @@ +/* + * 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 br.edu.ufsj.sms.GUI; + +import br.edu.ufsj.sms.control.AlarmClock; +import br.edu.ufsj.sms.net.Message; +import br.edu.ufsj.sms.net.Socket; +import java.awt.Component; +import java.awt.image.BufferedImage; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.net.UnknownHostException; +import java.util.Timer; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.imageio.ImageIO; +import javax.swing.ImageIcon; +import javax.swing.JLabel; + +/** + * + * @author flavio + */ +public class Main extends javax.swing.JFrame { + + /** + * Creates new form Main + */ + private Socket socket; + private AlarmClock alarmClock; + private Timer timer; + public final static String INET_ADDR = "224.0.0.3"; + public final static int TIME = 1000; + + private String name; + private int port; + + public Main(String name, int port) throws UnknownHostException { + initComponents(); + this.name = name; + this.port = port; + this.socket = new Socket(this); + this.socket.start(); + this.timer = new Timer(); + } + + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + @SuppressWarnings("unchecked") + // //GEN-BEGIN:initComponents + private void initComponents() { + + jToolBar1 = new javax.swing.JToolBar(); + shareButton = new javax.swing.JToggleButton(); + jScrollPane1 = new javax.swing.JScrollPane(); + jTabbedPane1 = new javax.swing.JTabbedPane(); + + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + setTitle("Share My Sheet"); + + jToolBar1.setRollover(true); + + shareButton.setText("Share My Sheet!"); + shareButton.setActionCommand("Share My Sheet!"); + shareButton.setFocusable(false); + shareButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); + shareButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); + shareButton.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + shareButtonActionPerformed(evt); + } + }); + jToolBar1.add(shareButton); + + jScrollPane1.setViewportView(jTabbedPane1); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); + getContentPane().setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE) + .addComponent(jScrollPane1) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 231, Short.MAX_VALUE)) + ); + + pack(); + }// //GEN-END:initComponents + + private void shareButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_shareButtonActionPerformed + // TODO add your handling code here: + System.out.println("" + shareButton.isSelected()); + if (shareButton.isSelected()) { + this.timer = new Timer(); + this.alarmClock = new AlarmClock(this.name, this.socket); + this.timer.scheduleAtFixedRate(this.alarmClock, 0, Main.TIME); + shareButton.setText("Stop sharing it!"); + } else { + this.timer.cancel(); + shareButton.setText("Share My Sheet!"); + } + }//GEN-LAST:event_shareButtonActionPerformed + + public void receiveMessage(Message message) { + // Verificar se a aba existe, se existir, atualizo. + ByteArrayInputStream bais = new ByteArrayInputStream(message.getImageByte()); + BufferedImage image; + Component tab = null; + try { + for (int i = 0; i < jTabbedPane1.getTabCount(); i++) { + String name = jTabbedPane1.getTitleAt(i); + if (name.equals(message.getName())) { + image = ImageIO.read(bais); + JLabel myShoot = new JLabel(new ImageIcon(image)); + jTabbedPane1.setComponentAt(i, myShoot); + return; + } + } + + image = ImageIO.read(bais); + JLabel myShoot = new JLabel(new ImageIcon(image)); + jTabbedPane1.addTab(message.getName(), myShoot); + } catch (IOException ex) { + Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); + } + } + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JScrollPane jScrollPane1; + private javax.swing.JTabbedPane jTabbedPane1; + private javax.swing.JToolBar jToolBar1; + private javax.swing.JToggleButton shareButton; + // End of variables declaration//GEN-END:variables + + /** + * @return the name + */ + public String getName() { + return name; + } + + /** + * @param name the name to set + */ + public void setName(String name) { + this.name = name; + } + + /** + * @return the port + */ + public int getPort() { + return port; + } + + /** + * @param port the port to set + */ + public void setPort(int port) { + this.port = port; + } + +} diff --git a/src/br/edu/ufsj/sms/GUI/ScreenShot.java b/src/br/edu/ufsj/sms/GUI/ScreenShot.java new file mode 100644 index 0000000..f23836b --- /dev/null +++ b/src/br/edu/ufsj/sms/GUI/ScreenShot.java @@ -0,0 +1,33 @@ +/* + * 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 br.edu.ufsj.sms.GUI; + +import java.awt.AWTException; +import java.awt.Rectangle; +import java.awt.Robot; +import java.awt.Toolkit; +import java.awt.image.BufferedImage; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import javax.imageio.ImageIO; + +/** + * + * @author flavio + */ +public class ScreenShot { + + public ScreenShot() { + } + + public BufferedImage takeAShot() throws AWTException, IOException { + Robot robot = new Robot(); + Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); + BufferedImage output = robot.createScreenCapture(screenRect); + return output; + } + +} diff --git a/src/br/edu/ufsj/sms/Main.java b/src/br/edu/ufsj/sms/Main.java new file mode 100644 index 0000000..3237c95 --- /dev/null +++ b/src/br/edu/ufsj/sms/Main.java @@ -0,0 +1,65 @@ +/* + * 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 br.edu.ufsj.sms; + +import br.edu.ufsj.sms.GUI.Login; +import java.net.UnknownHostException; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + * @author flavio + */ +public class Main { + + /** + * @param args the command line arguments + */ + public static void main(String args[]) { + /* Set the Nimbus look and feel */ + // + /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. + * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html + */ + try { + for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { + if ("Nimbus".equals(info.getName())) { + javax.swing.UIManager.setLookAndFeel(info.getClassName()); + break; + } + } + } catch (ClassNotFoundException ex) { + java.util.logging.Logger.getLogger(br.edu.ufsj.sms.GUI.Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (InstantiationException ex) { + java.util.logging.Logger.getLogger(br.edu.ufsj.sms.GUI.Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (IllegalAccessException ex) { + java.util.logging.Logger.getLogger(br.edu.ufsj.sms.GUI.Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (javax.swing.UnsupportedLookAndFeelException ex) { + java.util.logging.Logger.getLogger(br.edu.ufsj.sms.GUI.Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + // + + /* Create and display the form */ + java.awt.EventQueue.invokeLater(new Runnable() { + public void run() { + try { + Login login = new Login(null, true); + login.setVisible(true); + String name = login.getName(); + int port = login.getPort(); + login.dispose(); + br.edu.ufsj.sms.GUI.Main main; + main = new br.edu.ufsj.sms.GUI.Main(name, port); + main.setVisible(true); + } catch (UnknownHostException ex) { + Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); + } + } + }); + } + +} diff --git a/src/br/edu/ufsj/sms/control/AlarmClock.java b/src/br/edu/ufsj/sms/control/AlarmClock.java new file mode 100644 index 0000000..3a28800 --- /dev/null +++ b/src/br/edu/ufsj/sms/control/AlarmClock.java @@ -0,0 +1,57 @@ +/* + * 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 br.edu.ufsj.sms.control; + +import br.edu.ufsj.sms.GUI.ScreenShot; +import br.edu.ufsj.sms.net.Message; +import br.edu.ufsj.sms.net.Socket; +import java.awt.AWTException; +import java.awt.image.BufferedImage; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.Iterator; +import java.util.TimerTask; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.imageio.IIOImage; +import javax.imageio.ImageIO; +import javax.imageio.ImageWriteParam; +import javax.imageio.ImageWriter; +import javax.imageio.stream.ImageOutputStream; + +/** + * + * @author flavio + */ +public class AlarmClock extends TimerTask { + + ScreenShot ss; + String name; + Socket socket; + + public AlarmClock(String name, Socket socket) { + ss = new ScreenShot(); + this.name = name; + this.socket = socket; + } + + @Override + public void run() { + try { + BufferedImage image = ss.takeAShot(); + BufferedImage rescaled = ImageTransformation.resize(image, 1024, 768); + byte[] data = ImageTransformation.compress(rescaled, 0.45f); + Message message = new Message(this.name, data); + this.socket.send(message); + } catch (AWTException ex) { + Logger.getLogger(AlarmClock.class.getName()).log(Level.SEVERE, null, ex); + } catch (IOException ex) { + Logger.getLogger(AlarmClock.class.getName()).log(Level.SEVERE, null, ex); + } + + } + +} diff --git a/src/br/edu/ufsj/sms/control/ImageTransformation.java b/src/br/edu/ufsj/sms/control/ImageTransformation.java new file mode 100644 index 0000000..30164cc --- /dev/null +++ b/src/br/edu/ufsj/sms/control/ImageTransformation.java @@ -0,0 +1,70 @@ +/* + * 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 br.edu.ufsj.sms.control; + +import java.awt.Image; +import java.awt.image.BufferedImage; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.Iterator; +import javax.imageio.IIOImage; +import javax.imageio.ImageIO; +import javax.imageio.ImageWriteParam; +import javax.imageio.ImageWriter; +import javax.imageio.stream.ImageOutputStream; + +/** + * + * @author flavio + */ +public class ImageTransformation { + + public static byte[] toByteArray(BufferedImage image) throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ImageIO.write(image, "jpg", baos); + byte[] data = baos.toByteArray(); + return data; + } + + public static byte[] compress(BufferedImage image, float scale) throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + Iterator writers = ImageIO.getImageWritersByFormatName("jpg"); + ImageWriter writer = writers.next(); + ImageWriteParam param = writer.getDefaultWriteParam(); + param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); + param.setCompressionQuality(scale); + ImageOutputStream ios = ImageIO.createImageOutputStream(baos); + writer.setOutput(ios); + writer.write(null, new IIOImage(image, null, null), param); + byte[] data = baos.toByteArray(); + writer.dispose(); + return data; + } + + public static BufferedImage resize(BufferedImage img, int maxWidth, int maxHeight) throws IOException { + int scaledWidth = 0; + int scaledHeight = 0; + + scaledWidth = maxWidth; + scaledHeight = (int) (img.getHeight() * ((double) scaledWidth / img.getWidth())); + + if (scaledHeight > maxHeight) { + scaledHeight = maxHeight; + scaledWidth = (int) (img.getWidth() * ((double) scaledHeight / img.getHeight())); + + if (scaledWidth > maxWidth) { + scaledWidth = maxWidth; + scaledHeight = maxHeight; + } + } + + Image resized = img.getScaledInstance(scaledWidth, scaledHeight, Image.SCALE_SMOOTH); + BufferedImage buffered = new BufferedImage(scaledWidth, scaledHeight, Image.SCALE_REPLICATE); + buffered.getGraphics().drawImage(resized, 0, 0, null); + + return buffered; + } +} diff --git a/src/br/edu/ufsj/sms/net/Message.java b/src/br/edu/ufsj/sms/net/Message.java new file mode 100644 index 0000000..0893bc5 --- /dev/null +++ b/src/br/edu/ufsj/sms/net/Message.java @@ -0,0 +1,53 @@ +/* + * 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 br.edu.ufsj.sms.net; + +import java.awt.image.BufferedImage; +import java.io.Serializable; + +/** + * + * @author flavio + */ +public class Message implements Serializable { + + private String name; + private byte[] imageByte; + + public Message(String name, byte[] imageByte) { + this.name = name; + this.imageByte = imageByte; + } + + /** + * @return the name + */ + public String getName() { + return name; + } + + /** + * @param name the name to set + */ + public void setName(String name) { + this.name = name; + } + + /** + * @return the imageByte + */ + public byte[] getImageByte() { + return imageByte; + } + + /** + * @param imageByte the image to set + */ + public void setImageByte(byte[] imageByte) { + this.imageByte = imageByte; + } + +} diff --git a/src/br/edu/ufsj/sms/net/Socket.java b/src/br/edu/ufsj/sms/net/Socket.java new file mode 100644 index 0000000..102d99b --- /dev/null +++ b/src/br/edu/ufsj/sms/net/Socket.java @@ -0,0 +1,87 @@ +/* + * 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 br.edu.ufsj.sms.net; + +import br.edu.ufsj.sms.GUI.Main; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectInputStream; +import java.io.ObjectOutput; +import java.io.ObjectOutputStream; +import java.net.DatagramPacket; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.MulticastSocket; +import java.net.UnknownHostException; +import java.nio.ByteBuffer; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + * @author flavio + */ +public class Socket extends Thread { + + private MulticastSocket multicastSocket; + private InetAddress address; + private Main main; + + public Socket(Main main) throws UnknownHostException { + this.main = main; + this.address = InetAddress.getByName(Main.INET_ADDR); + try { + multicastSocket = new MulticastSocket(this.main.getPort()); + multicastSocket.setSendBufferSize(256000); + multicastSocket.setReceiveBufferSize(256000); + multicastSocket.setReuseAddress(true); + multicastSocket.joinGroup(address); + } catch (IOException ex) { + System.out.println("There is no socket connection. Sorry."); + System.out.println(ex.toString()); + } + } + + public void receive() throws UnknownHostException, IOException, ClassNotFoundException { + + byte[] buf = new byte[256000]; + + while (true) { + // Receive the information and print it. + DatagramPacket msgPacket = new DatagramPacket(buf, buf.length); + multicastSocket.receive(msgPacket); + + ByteArrayInputStream bis = new ByteArrayInputStream(msgPacket.getData()); + ObjectInput in = new ObjectInputStream(bis); + Message message = (Message) in.readObject(); + this.main.receiveMessage(message); + } + } + + public void send(Message message) throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutput out = new ObjectOutputStream(baos); + out.writeObject(message); + out.flush(); + byte[] msg = baos.toByteArray(); + DatagramPacket msgPacket; + msgPacket = new DatagramPacket(msg, msg.length, this.address, this.main.getPort()); + multicastSocket.send(msgPacket); + } + + @Override + public void run() { + try { + this.receive(); + } catch (IOException ex) { + Logger.getLogger(Socket.class.getName()).log(Level.SEVERE, null, ex); + } catch (ClassNotFoundException ex) { + Logger.getLogger(Socket.class.getName()).log(Level.SEVERE, null, ex); + } + } +}