aboutsummaryrefslogtreecommitdiff
path: root/CS2452/Skype
diff options
context:
space:
mode:
Diffstat (limited to 'CS2452/Skype')
-rw-r--r--CS2452/Skype/src/Skype/Client.java63
-rw-r--r--CS2452/Skype/src/Skype/Gui.java222
-rw-r--r--CS2452/Skype/src/Skype/MGui.java27
-rw-r--r--CS2452/Skype/src/Skype/Server.java88
-rw-r--r--CS2452/Skype/src/Skype/audio.java36
-rw-r--r--CS2452/Skype/src/Skype/mic.java77
-rw-r--r--CS2452/Skype/src/Skype/numberFilter.java48
-rw-r--r--CS2452/Skype/src/Skype/speaker.java90
8 files changed, 651 insertions, 0 deletions
diff --git a/CS2452/Skype/src/Skype/Client.java b/CS2452/Skype/src/Skype/Client.java
new file mode 100644
index 0000000..f1716fe
--- /dev/null
+++ b/CS2452/Skype/src/Skype/Client.java
@@ -0,0 +1,63 @@
+package Skype;
+
+import java.io.IOException;
+import java.net.*;
+import java.util.concurrent.Semaphore;
+
+public class Client extends Thread
+{
+ Socket s;
+ InetAddress ip;
+ int port;
+ audio aud;
+ Gui g;
+ Semaphore audflg;
+ Semaphore guiflg;
+
+ public Client(Gui g, String ip, String port, audio aud, Semaphore audflg,Semaphore guiflg)
+ {
+ this.aud = aud;
+ this.audflg = audflg;
+ this.guiflg = guiflg;
+ this.g = g;
+
+ try
+ {
+ this.ip = InetAddress.getByName(ip);
+ } catch (UnknownHostException e)
+ {
+ e.printStackTrace();
+ }
+ this.port = Integer.parseInt(port);
+ this.start();
+ }
+
+ public void run()
+ {
+ //System.out.printf("Client Started\n");
+ try
+ {
+ s = new Socket(ip, port);
+ } catch (IOException e)
+ {
+ e.printStackTrace();
+ }
+ try
+ {
+ audflg.acquire();
+ } catch (InterruptedException e)
+ {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ aud.setOpen(true);
+ audflg.release();
+
+ speaker t1 = new speaker(s, audio.getFormat(), aud, audflg,g);
+ mic t2 = new mic(s, audio.getFormat(), g, guiflg);
+
+ t1.start();
+ t2.start();
+ }
+
+}
diff --git a/CS2452/Skype/src/Skype/Gui.java b/CS2452/Skype/src/Skype/Gui.java
new file mode 100644
index 0000000..6e0f0a0
--- /dev/null
+++ b/CS2452/Skype/src/Skype/Gui.java
@@ -0,0 +1,222 @@
+package Skype;
+
+import java.awt.Color;
+import java.awt.GridBagConstraints;
+import java.awt.GridBagLayout;
+import java.awt.GridLayout;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.concurrent.Semaphore;
+
+import javax.swing.*;
+import javax.swing.event.ChangeEvent;
+import javax.swing.event.ChangeListener;
+import javax.swing.text.AbstractDocument;
+
+public class Gui extends JFrame implements ActionListener, ChangeListener
+{
+ JButton call;
+ JCheckBox mute;
+ JSlider volume;
+ JLabel time, ipL, portL, volumeL,ipLH,portLH;
+ JTextField ipT, portT;
+ JPanel panel, ip, volumeP, slider;
+ GridBagConstraints c;
+ Timer timer;
+ int sec;
+
+ boolean incall, servercall;
+
+ Semaphore audflg;
+ Semaphore svrflg;
+ Semaphore guiflg;
+ audio aud;
+
+ public Gui(audio aud, Semaphore svrflg, Semaphore audflg, Semaphore guiflg,int port)
+ {
+ this.guiflg = guiflg;
+ timer = new Timer(1000, this);
+ incall = false;
+ this.svrflg = svrflg;
+ this.audflg = audflg;
+ this.aud = aud;
+
+ c = new GridBagConstraints();
+ c.ipadx = 10;
+ c.ipady = 10;
+
+ panel = new JPanel(new GridBagLayout());
+
+ call = new JButton("Call");
+ call.addActionListener(this);
+
+ //Volume Panel setup
+ volume = new JSlider(SwingConstants.VERTICAL);
+ volume.addChangeListener(this);
+ volume.setPaintTicks(true);
+ volume.setMajorTickSpacing(25);
+ volume.setMinorTickSpacing(5);
+ volume.setPaintTicks(true);
+ volume.setPaintLabels(true);
+
+ volumeL = new JLabel("Volume");
+
+ volumeP = new JPanel();
+ volumeP.setLayout(new GridBagLayout());
+
+ volumeP.add(volume, c);
+ c.gridx = 0;
+ c.gridy = 1;
+ volumeP.add(volumeL, c);
+
+ mute = new JCheckBox("Mute");
+ mute.addActionListener(this);
+ c.gridx = 1;
+ c.gridy = 1;
+ volumeP.add(mute, c);
+ volumeP.setVisible(true);
+
+ //Address setup
+ ipL = new JLabel("IP:");
+ ipT = new JTextField(15);
+ ipT.setText("127.0.0.1");
+ ((AbstractDocument) ipT.getDocument()).setDocumentFilter(new numberFilter());
+ ip = new JPanel(new GridBagLayout());
+ c.gridx = 0;
+ c.gridy = 0;
+ c.anchor = GridBagConstraints.EAST;
+ ip.add(ipL, c);
+ c.gridx = 1;
+
+ c.anchor = GridBagConstraints.WEST;
+ ip.add(ipT, c);
+
+ portL = new JLabel("Port:");
+ portT = new JTextField(5);
+ portT.setText("7777");
+ ((AbstractDocument) portT.getDocument()).setDocumentFilter(new numberFilter());
+ c.gridy = 1;
+ c.anchor = GridBagConstraints.WEST;
+ ip.add(portT, c);
+ c.gridx = 0;
+ c.anchor = GridBagConstraints.EAST;
+ ip.add(portL, c);
+
+ try
+ {
+ ipLH = new JLabel("Your IP: ".concat(InetAddress.getLocalHost().getHostAddress().toString()));
+ } catch (UnknownHostException e)
+ {
+ e.printStackTrace();
+ }
+ portLH = new JLabel("Your Port: ".concat(Integer.toString(port)));
+
+
+ c.anchor = GridBagConstraints.WEST;
+ c.gridy = 2;
+ c.gridx = 1;
+ ip.add(ipLH,c);
+ c.gridy++;
+ ip.add(portLH,c);
+
+ time = new JLabel("TIME");
+
+ c.gridx = 0;
+ c.gridy = 0;
+ c.anchor = GridBagConstraints.NORTHWEST;
+ panel.add(ip, c);
+ c.gridx = 1;
+ panel.add(volumeP, c);
+ c.gridy = 1;
+ c.anchor = GridBagConstraints.CENTER;
+ panel.add(call, c);
+ c.gridx = 0;
+ panel.add(time, c);
+ panel.setVisible(true);
+
+ this.add(panel);
+
+ }
+
+ public void actionPerformed(ActionEvent e)
+ {
+ try
+ {
+ guiflg.acquire();
+
+ if (e.getSource() == mute)
+ {
+ if (mute.isSelected())
+ {
+ volume.setEnabled(false);
+ aud.setVolume(0);
+ }
+ else
+ {
+ volume.setEnabled(true);
+ aud.setVolume(volume.getValue());
+ }
+ }
+ else if (e.getSource() == call)
+ {
+ if (incall)
+ {
+ call.setText("Call");
+ timer.stop();
+ //System.out.println("Hung up");
+ incall = false;
+ try
+ {
+ audflg.acquire();
+ } catch (InterruptedException e1)
+ {
+ e1.printStackTrace();
+ }
+ //System.out.println("False");
+ aud.setOpen(false);
+ audflg.release();
+ svrflg.release(); //allow server to accept connections again.
+ }
+ else
+ {
+ call.setText("Hang up");
+ sec = 0;
+ timer.start();
+ Client c = new Client(this, ipT.getText(), portT.getText(), aud, audflg,guiflg);
+ //System.out.printf("IP: %s, Port: %s\n", ipT.getText(), portT.getText());
+ incall = true;
+
+ }
+ }
+ else if ((e.getSource() == timer) && incall)
+ {
+ sec++ ;
+ if (sec < 60 * 60) time.setText(String.format("%02d:%02d", sec / 60, sec % 60));
+ else time.setText(String.format("%d:%02d:%02d", sec / (60 * 60), (sec / 60) % 60, sec % 60));
+ }
+ } catch (InterruptedException e2)
+ {
+ // TODO Auto-generated catch block
+ e2.printStackTrace();
+ }
+ guiflg.release();
+
+ }
+
+ public void stateChanged(ChangeEvent e)
+ {
+ if (e.getSource() == volume)
+ {
+ if (audflg.tryAcquire())
+ {
+ aud.setVolume(volume.getValue());
+ //change volume .getValue();
+ audflg.release();
+ }
+
+ }
+ }
+
+}
diff --git a/CS2452/Skype/src/Skype/MGui.java b/CS2452/Skype/src/Skype/MGui.java
new file mode 100644
index 0000000..c545ffe
--- /dev/null
+++ b/CS2452/Skype/src/Skype/MGui.java
@@ -0,0 +1,27 @@
+package Skype;
+
+import java.util.concurrent.Semaphore;
+
+import javax.swing.JFrame;
+
+public class MGui
+{
+ public static void main(String[] args)
+ {
+ audio aud = new audio(50);
+ Semaphore volflg = new Semaphore(1);
+ Semaphore svrflg = new Semaphore(1);
+ Semaphore guiflg = new Semaphore(1);
+
+ int port = 7777;
+
+ Gui g = new Gui(aud,svrflg,volflg,guiflg,port);
+ g.setSize(500, 500);
+ g.setLocation(32, 32);
+ g.setVisible(true);
+ g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ Server server = new Server(g, port,aud,svrflg,volflg,guiflg);
+ }
+
+}
diff --git a/CS2452/Skype/src/Skype/Server.java b/CS2452/Skype/src/Skype/Server.java
new file mode 100644
index 0000000..e8b8a0b
--- /dev/null
+++ b/CS2452/Skype/src/Skype/Server.java
@@ -0,0 +1,88 @@
+package Skype;
+
+import java.io.IOException;
+import java.net.*;
+import java.util.concurrent.Semaphore;
+
+public class Server extends Thread
+{
+ ServerSocket ss;
+ Socket s;
+ int port;
+ Gui g;
+ audio aud;
+ Semaphore svrflg;
+ Semaphore audflg;
+ Semaphore guiflg;
+
+ public Server(Gui g, int port, audio aud, Semaphore svrflg, Semaphore audflg, Semaphore guiflg)
+ {
+ this.guiflg = guiflg;
+ this.audflg = audflg;
+ this.svrflg = svrflg;
+ this.aud = aud;
+ this.port = port;
+ this.g = g;
+ try
+ {
+ this.ss = new ServerSocket(port);
+ } catch (IOException e)
+ {
+ e.printStackTrace();
+ }
+
+ this.start();
+
+ }
+
+ public void run()
+ {
+ while (true)
+ {
+ //System.out.printf("Server is waiting\n");
+ try
+ {
+ svrflg.acquire();
+ //System.out.println(ss);
+ s = ss.accept();
+ } catch (IOException e)
+ {
+ e.printStackTrace();
+ } catch (InterruptedException e)
+ {
+ e.printStackTrace();
+ }
+
+ try
+ {
+ audflg.acquire();
+ } catch (InterruptedException e)
+ {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ aud.setOpen(true);
+ audflg.release();
+ //System.out.println("ServerSpeaker");
+ speaker t1 = new speaker(s, audio.getFormat(), aud, audflg,g);
+ mic t2 = new mic(s, audio.getFormat(),g,guiflg);
+ try
+ {
+ guiflg.acquire();
+ g.call.setText("Hang up");
+ g.sec = 0;
+ g.timer.start();
+ g.incall = true;
+ guiflg.release();
+ } catch (InterruptedException e)
+ {
+ e.printStackTrace();
+ }
+
+ t1.start();
+ t2.start();
+
+ }
+ }
+
+} \ No newline at end of file
diff --git a/CS2452/Skype/src/Skype/audio.java b/CS2452/Skype/src/Skype/audio.java
new file mode 100644
index 0000000..d9181fd
--- /dev/null
+++ b/CS2452/Skype/src/Skype/audio.java
@@ -0,0 +1,36 @@
+package Skype;
+
+import java.net.Socket;
+
+import javax.sound.sampled.AudioFormat;
+
+public class audio
+{
+ double vol;
+ boolean open;
+
+ audio(int vol)
+ {
+ this.vol = vol / 100.00;
+ }
+
+ public static AudioFormat getFormat()
+ {
+ float sampleRate = 8000;
+ int sampleSizeInBits = 8;
+ int channels = 1;
+ boolean signed = true;
+ boolean bigEndian = true;
+ return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian);
+ }
+
+ public void setVolume(int volume)
+ {
+ vol = volume / 100.00;
+ }
+
+ public void setOpen(boolean a){
+ open = a;
+ }
+
+} \ No newline at end of file
diff --git a/CS2452/Skype/src/Skype/mic.java b/CS2452/Skype/src/Skype/mic.java
new file mode 100644
index 0000000..01cadfd
--- /dev/null
+++ b/CS2452/Skype/src/Skype/mic.java
@@ -0,0 +1,77 @@
+package Skype;
+
+import java.util.*;
+import java.util.concurrent.Semaphore;
+
+import javax.sound.sampled.*;
+
+import java.io.*;
+import java.net.*;
+
+class mic extends Thread
+{
+ Socket s;
+ AudioFormat format;
+ Gui g;
+ Semaphore guiflg;
+
+ public mic(Socket socket, AudioFormat format, Gui g, Semaphore guiflg)
+ {
+ s = socket;
+ this.format = format;
+ this.g = g;
+ this.guiflg = guiflg;
+ }
+
+ public void run()
+ {
+ BufferedOutputStream bout = null;
+ try
+ {
+ DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
+ final TargetDataLine line = (TargetDataLine) AudioSystem.getLine(info);
+
+ line.open(format, 1024);
+ line.start();
+ int bufferSize = 32;
+ byte buffer[] = new byte[bufferSize];
+ try
+ {
+ bout = new BufferedOutputStream(s.getOutputStream());
+
+ //System.out.printf("mic started\n");
+ while (true)
+ {
+ int count = line.read(buffer, 0, buffer.length);
+
+ if (count > 0) bout.write(buffer, 0, count);
+
+ bout.flush();
+ }
+ }catch (SocketException se)
+ {
+ try
+ {
+ guiflg.acquire();
+ } catch (InterruptedException e)
+ {
+ e.printStackTrace();
+ }
+
+ g.call.setText("Call");
+ g.incall = false;
+ guiflg.release();
+
+
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+ } catch (LineUnavailableException e1)
+ {
+ e1.printStackTrace();
+ }
+ }
+
+}
diff --git a/CS2452/Skype/src/Skype/numberFilter.java b/CS2452/Skype/src/Skype/numberFilter.java
new file mode 100644
index 0000000..def1b3e
--- /dev/null
+++ b/CS2452/Skype/src/Skype/numberFilter.java
@@ -0,0 +1,48 @@
+package Skype;
+
+import java.awt.Toolkit;
+
+import javax.swing.text.AttributeSet;
+import javax.swing.text.BadLocationException;
+import javax.swing.text.DocumentFilter;
+
+class numberFilter extends DocumentFilter
+{
+ @Override
+ public void insertString(DocumentFilter.FilterBypass fp, int offset, String string,AttributeSet aset) throws BadLocationException
+ {
+ int len = string.length();
+ boolean isValidInteger = true;
+
+ for (int i = 0; i < len; i++ )
+ {
+ if (!Character.isDigit(string.charAt(i)) && string.charAt(i) != '.')
+ {
+ isValidInteger = false;
+ break;
+ }
+ }
+ if (isValidInteger) super.insertString(fp, offset, string, aset);
+ else
+ Toolkit.getDefaultToolkit().beep();
+ }
+
+ public void replace(DocumentFilter.FilterBypass fp, int offset, int length, String string, AttributeSet aset) throws BadLocationException
+ {
+ int len = string.length();
+ boolean isValidInteger = true;
+
+ for (int i = 0; i < len; i++)
+ {
+ if (!Character.isDigit(string.charAt(i)) && string.charAt(i) != '.')
+ {
+ isValidInteger = false;
+ break;
+ }
+ }
+ if (isValidInteger)
+ super.replace(fp, offset, length, string, aset);
+ else
+ Toolkit.getDefaultToolkit().beep();
+ }
+} \ No newline at end of file
diff --git a/CS2452/Skype/src/Skype/speaker.java b/CS2452/Skype/src/Skype/speaker.java
new file mode 100644
index 0000000..596986f
--- /dev/null
+++ b/CS2452/Skype/src/Skype/speaker.java
@@ -0,0 +1,90 @@
+package Skype;
+
+import java.util.*;
+import java.util.concurrent.Semaphore;
+import java.io.*;
+import java.net.*;
+import javax.sound.sampled.*;
+
+class speaker extends Thread
+{
+ Socket s;
+ AudioFormat format;
+ audio aud;
+ Semaphore flg;
+ Gui g;
+
+ public speaker(Socket socket, AudioFormat format, audio aud, Semaphore audflg, Gui g)
+ {
+ this.g = g;
+ this.aud = aud;
+ this.flg = audflg;
+ s = socket;
+ this.format = format;
+ }
+
+ public void run()
+ {
+ try
+ {
+ DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
+ final SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
+ line.open(format, 1024);
+ line.start();
+
+ final AudioInputStream ais = new AudioInputStream(s.getInputStream(), format, 2000000);
+ int count;
+ int bufferSize = (int) format.getSampleRate() * format.getFrameSize();
+ byte buffer[] = new byte[bufferSize];
+
+ //System.out.printf("speakers started\n");
+ while (((count = ais.read(buffer, 0, buffer.length)) != -1) && aud.open)
+ {
+ flg.release();
+ if (count > 0)
+ {
+ if (flg.tryAcquire())
+ {
+
+ for (int i = 0; i < buffer.length; i++ )
+ {
+ buffer[i] *= aud.vol;
+ }
+ flg.release();
+ }
+
+ line.write(buffer, 0, count);
+ }
+
+ }
+ line.drain();
+ line.close();
+ } catch (IOException e)
+ {
+ System.err.println("I/O problems: " + e);
+
+ } catch (LineUnavailableException e)
+ {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ try
+ {
+ flg.acquire();
+ } catch (InterruptedException e)
+ {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ //System.out.println("out of while");
+ try
+ {
+ s.close();
+ //System.out.println("S closed");
+ } catch (IOException e)
+ {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+}