import java.net.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import javax.sound.sampled.*;
class LogFile
{
static PrintStream writer;
static {
try{writer=new PrintStream(new FileOutputStream("log.txt"));}
catch(IOException ex){System.out.println(ex);}
}
public static void print(String obj)
{
writer.println(obj);
}
}
class Mic implements Runnable
{
TargetDataLine tdl;
public byte samples[];
DatagramPacket dp=null;
DatagramSocket ds=null;
Thread th=null;
InetAddress rip;
public Mic(DatagramSocket ds,InetAddress rip)
{
try
{
this.ds=ds;
this.rip=rip;
DataLine.Info dl=new DataLine.Info(TargetDataLine.class,getMicFormat());
tdl=(TargetDataLine)AudioSystem.getLine(dl);
tdl.open(getMicFormat());
samples=new byte[8000];
tdl.start();
th=new Thread(this);
th.start();
}
catch(Exception e)
{
LogFile.print(e.toString());
System.exit(0);
}
}
public byte[] read(int length)
{
try {
tdl.read(samples,0,length);
LogFile.print("Mic Read");
}
catch(Exception ex)
{
LogFile.print("Mic read Error" +ex);
}
return samples;
}
public AudioFormat getMicFormat()throws Exception
{
AudioFormat af=new AudioFormat(8000.0F,8,1,false,true);
return af;
}
public void run()
{
while (true)
{
byte data[]=read(8000);
dp=new DatagramPacket(data,8000,rip,4569);
try {ds.send(dp);Thread.sleep(500);}
catch(InterruptedException ex){System.out.println(ex);}
catch(IOException ex){System.out.println(ex);}
}
}
public void close()
{
th.destroy();
tdl.close();
}
}
class Speaker implements Runnable
{
SourceDataLine sl=null;
DatagramSocket ds=null;
Thread th=null;
public Speaker(DatagramSocket ds)
{
try
{
this.ds=ds;
DataLine.Info dl=new
DataLine.Info(SourceDataLine.class,getAudioFormat());
sl=(SourceDataLine)AudioSystem.getLine(dl);
sl.open(getAudioFormat());
sl.start();
LogFile.print("Speaker :"+sl);
th=new Thread(this);
th.start();
}
catch(Exception e)
{
LogFile.print("Speaker not open");
}
}
public void write(byte b[] ,int length)
{
try {
sl.write(b,0,length);
LogFile.print("Speaker Write"+ length);
}
catch(Exception ex)
{
LogFile.print("Speaker Write Errors" +ex);
}
}
public void run()
{
while (true)
{
byte samples []=new byte[8000];
DatagramPacket dp=new DatagramPacket(samples,8000);
try { ds.receive(dp);}catch(IOException ex){}
write(samples,8000);
try {Thread.sleep(500);}catch(InterruptedException ex){}
}
}
public AudioFormat getAudioFormat()throws Exception
{
AudioFormat af=new AudioFormat(8000.0F,8,1,true,true);
return af;
}
public void close()
{
th.destroy();
sl.close();
}
}
class Stream extends Thread
{
Socket cs=null;
InputStream is=null;
OutputStream os=null;
String cmd,value;
DatagramSocket ds=null;
Mic mc;Speaker spk;
InetAddress rip;
Stream()
{
try{
rip=InetAddress.getByName("system2");
cs=new Socket(rip,4567);
is=cs.getInputStream();
os=cs.getOutputStream();
}catch(Exception ex ){System.out.println(ex);}
start();
}
void dialno(String str)
{
byte b[]=new byte[50];
try
{
String str1="Dial="+str;
b=str1.getBytes();
os.write(b);
}
catch(Exception e){}
}
public void close()
{
byte b[]=new byte[50];
try
{
mc.close();spk.close();
String str="Disconnect="+value;
b=str.getBytes();
os.write(b);os.close();
is.close();cs.close();
}
catch(Exception e){}
}
public void run()
{
while(true)
{
byte b[]=new byte[50];
try{
is.read(b);
String str=new String(b);
str=str.trim();
status(str.trim());
}
catch(Exception e){}
}
}
void status(String s)
{
StringTokenizer token=new StringTokenizer(s,"=");
System.out.println(s);
if(token.hasMoreTokens())
{
cmd=token.nextToken();
value=token.nextToken();
if(cmd.equals("Dial")==true)
{
LogFile.print("Dialing to"+value);
System.out.println("dialing");
}
else if(cmd.equals("Disonnect")==true)
{
//mc.close();spk.close();
LogFile.print("Disconnect from"+value);
}
else if(cmd.equals("connect")==true)
{
System.out.println("connect");
try
{ds=new DatagramSocket(4568);}
catch(Exception ex)
{LogFile.print("Datagram create Exception");}
mc=new Mic(ds,rip);
spk=new Speaker(ds);
}
}
}
}
class Numkey extends JButton
{
int Width=50;
int Height=50;
String name;
Numkey(String name)
{
this.name=name;
}
public void paint(Graphics g)
{
setPreferredSize(new Dimension(Width,Height));
setFont(new Font
("New Times Roman",Font.ITALIC+Font.BOLD,17));
setForeground(Color.BLUE);
super.paint(g);
g.drawString(name,20,30);
}
}
class ActionButton extends JPanel implements ActionListener
{
JButton Dial,Dscnt;
JTextField display;
Numkey nk0,nk1,nk2,nk3,nk4,nk5,nk6,
nk7,nk8,nk9,nk10,nk11;
Stream x=null;
ActionButton()
{
JPanel action= new JPanel();
action.setLayout(new GridLayout(1,2));
Dial=new JButton("DIAL");
action.add(Dial);
Dial.addActionListener(this);
Dial.setForeground(new Color(220,100,60));
Dscnt=new JButton("HANG UP");
action.add(Dscnt);
Dscnt.addActionListener(this);
Dscnt.setForeground(new Color(220,100,60));
JPanel dis=new JPanel();
dis.setLayout(new FlowLayout());
display=new JTextField(12);
display.setForeground(Color.BLUE);
display.setFont(new Font("New Times Roman",Font.ITALIC+Font.BOLD,15));
dis.add(display);
setLayout(new BorderLayout());
add(dis,BorderLayout.NORTH);
add(action,BorderLayout.SOUTH);
add(new NumPanel(display),BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource()==Dial)
{
if (x==null){
x=new Stream();
String str=display.getText();
x.dialno(str);}
}
if (ae.getSource()==Dscnt)
{
//x.close();
System.out.println("Closed");
System.exit(0);
}
}
}
class NumPanel extends JPanel implements ActionListener
{
Numkey nk0,nk1,nk2,nk3,nk4,nk5,nk6,
nk7,nk8,nk9,nk10,nk11;
JTextField dsp;
public NumPanel(JTextField dsp)
{
this.dsp=dsp;
setLayout(new GridLayout(4,3));
nk1=new Numkey("1");
nk1.addActionListener(this);
add(nk1);
nk2=new Numkey("2");
nk2.addActionListener(this);
add(nk2);
nk3=new Numkey("3");
nk3.addActionListener(this);
add(nk3);
nk4=new Numkey("4");
nk4.addActionListener(this);
add(nk4);
nk5=new Numkey("5");
nk5.addActionListener(this);
add(nk5);
nk6=new Numkey("6");
nk6.addActionListener(this);
add(nk6);
nk7=new Numkey("7");
nk7.addActionListener(this);
add(nk7);
nk8=new Numkey("8");
nk8.addActionListener(this);
add(nk8);
nk9=new Numkey("9");
nk9.addActionListener(this);
add(nk9);
nk10=new Numkey("*");
nk10.addActionListener(this);
add(nk10);
nk0=new Numkey("0");
nk0.addActionListener(this);
add(nk0);
nk11=new Numkey("
- 1
- 2
前往页