1st version that works
This commit is contained in:
@ -0,0 +1,34 @@
|
||||
package com.safemobile.lib;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import com.safemobile.lib.radio.Zone;
|
||||
|
||||
public class Configuration {
|
||||
public ArrayList<Contact> contacts;
|
||||
public Radio radio;
|
||||
public ArrayList<Zone> zones;
|
||||
|
||||
public Configuration()
|
||||
{
|
||||
contacts = new ArrayList<Contact>();
|
||||
zones = new ArrayList<Zone>();
|
||||
}
|
||||
|
||||
public String toString()
|
||||
{
|
||||
return "Contacts: " + contacts.size() + " | Radio: " + radio.toString() + " | Zones: " + zones.size();
|
||||
}
|
||||
|
||||
public String toLongString()
|
||||
{
|
||||
String cont = "", zone="";
|
||||
for(Contact contact: contacts)
|
||||
cont += "Name: " + contact.name + " | Id: " + contact.id + "\n";
|
||||
|
||||
for(Zone zonee: zones)
|
||||
zone += "Name: " + zonee.ZoneName + " | Id: " + zonee.id + "\n";
|
||||
|
||||
return "# Contacts #\n" + cont + "# Radio #\n" + radio.toString() + "\n# Zones #\n" + zone;
|
||||
}
|
||||
}
|
48
safeDispatch/src/main/java/com/safemobile/lib/TCPclient.java
Normal file
48
safeDispatch/src/main/java/com/safemobile/lib/TCPclient.java
Normal file
@ -0,0 +1,48 @@
|
||||
package com.safemobile.lib;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
public class TCPclient implements Runnable
|
||||
{
|
||||
private String serverHostname = new String ("127.0.0.1");
|
||||
private int port = 13579;
|
||||
private Thread runner;
|
||||
private BufferedReader input =null;
|
||||
char[] buf = new char[1024];
|
||||
Boolean _shouldStop=false;
|
||||
Socket sock = null;
|
||||
|
||||
public TCPclient(BufferedReader Paraminput)
|
||||
{
|
||||
input = Paraminput;
|
||||
runner = new Thread(this, "a lu bigu");
|
||||
runner.start(); // (2) Start the thread.
|
||||
try
|
||||
{
|
||||
sock = new Socket(serverHostname, port);
|
||||
InputStream recv= sock.getInputStream();
|
||||
OutputStream writer = sock.getOutputStream();
|
||||
|
||||
} catch (UnknownHostException e) {
|
||||
Log.d("tcp rec error","break:"+e.toString());
|
||||
} catch (IOException e) {
|
||||
Log.d("tcp rec error","break:"+e.toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
public void RequestStop()
|
||||
{
|
||||
_shouldStop = true;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,201 @@
|
||||
package com.safemobile.lib.sound;
|
||||
|
||||
import android.media.AudioFormat;
|
||||
import android.media.AudioManager;
|
||||
import android.media.AudioRecord;
|
||||
import android.media.AudioTrack;
|
||||
import android.media.MediaRecorder;
|
||||
|
||||
import com.safemobile.lib.SM;
|
||||
|
||||
import java.io.DataOutputStream;
|
||||
|
||||
public class AudioHandle implements Runnable{
|
||||
|
||||
private final int SAMPLE_RATE = 8000;
|
||||
|
||||
public Boolean isAlive = false;
|
||||
public Boolean soundNeeded = false;
|
||||
private AudioRecord recDev =null;
|
||||
private AudioTrack playDev =null;
|
||||
private int bufferSize;
|
||||
private DataOutputStream outData =null;
|
||||
private Thread t_micListner;
|
||||
private UDPclient udp;
|
||||
private TCPaudioClient tcp;
|
||||
private int audioport = 50001;
|
||||
|
||||
public int typeUDP;
|
||||
public AudioHandle(String IP, int _typeUDP)
|
||||
{
|
||||
typeUDP = _typeUDP;
|
||||
SM.Debug("---AudioHandle construcort---" +_typeUDP);
|
||||
if(typeUDP==1)
|
||||
{
|
||||
try
|
||||
{
|
||||
udp = new UDPclient(IP);
|
||||
bufferSize = AudioRecord.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
|
||||
int bufsize = AudioTrack.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
|
||||
//init play device
|
||||
//playDev = new AudioTrack(AudioManager.STREAM_MUSIC,sampleRate,AudioFormat.CHANNEL_CONFIGURATION_STEREO, AudioFormat.ENCODING_PCM_16BIT,bufsize, AudioTrack.MODE_STREAM);
|
||||
playDev = new AudioTrack(AudioManager.STREAM_MUSIC, SAMPLE_RATE,
|
||||
AudioFormat.CHANNEL_OUT_MONO,
|
||||
AudioFormat.ENCODING_PCM_16BIT, bufferSize, AudioTrack.MODE_STREAM);
|
||||
|
||||
//init mic listener
|
||||
recDev = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize * 2);
|
||||
//recDev = findAudioRecord();
|
||||
recDev.startRecording();
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
SM.Exception("audioH = 1", ex.toString());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
try {
|
||||
tcp = new TCPaudioClient(IP,audioport);
|
||||
bufferSize = AudioTrack.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
|
||||
|
||||
|
||||
//init play device
|
||||
playDev = new AudioTrack(AudioManager.STREAM_MUSIC,SAMPLE_RATE,AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT,bufferSize, AudioTrack.MODE_STREAM);
|
||||
}
|
||||
catch(Exception ex) {
|
||||
SM.Exception("audioH = 0 record", ex.toString());
|
||||
}
|
||||
|
||||
try {
|
||||
//init mic listener
|
||||
int bufsize = AudioRecord.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
|
||||
recDev = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufsize);
|
||||
//recDev = findAudioRecord();
|
||||
if(recDev.getState() == AudioRecord.STATE_INITIALIZED)
|
||||
recDev.startRecording();
|
||||
/*
|
||||
else
|
||||
Toast.makeText(context, "Could not start Recording device (you will not be able to send voice)", Toast.LENGTH_SHORT).show();*/
|
||||
}
|
||||
catch(Exception ex) {
|
||||
SM.Exception("audioH = 0", ex.toString());
|
||||
//Toast.makeText(context, "Could not start Recording device (you will not be able to send voice)", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
isAlive = true;
|
||||
t_micListner = new Thread(this, "micListener");
|
||||
t_micListner.start();
|
||||
|
||||
|
||||
if(typeUDP==1)
|
||||
{
|
||||
udp.addUDPListener(new IUDPListener()
|
||||
{
|
||||
|
||||
@Override
|
||||
public void dataRecv(UDPevent event) {
|
||||
byte[] data = event.data();
|
||||
int len = event.len();
|
||||
SM.Debug("recv b:"+len+" data.length:"+data.length);
|
||||
if(data!=null)
|
||||
PlaySound(data, len);
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
tcp.addTCPListener(new ITCPaudioLis()
|
||||
{
|
||||
|
||||
@Override
|
||||
public void dataRecv(TCPaudioEvent event) {
|
||||
byte[] data = event.data();
|
||||
int len = event.len();
|
||||
|
||||
if(data!=null)
|
||||
PlaySound(data, len);
|
||||
else
|
||||
SM.Debug("data ==null");
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
//playDev.p
|
||||
SM.Debug("=====AudioHandle construcort ===END====");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
byte[] tempBuffer;
|
||||
|
||||
int bRead=0;
|
||||
while(isAlive)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (soundNeeded)
|
||||
{
|
||||
//SM.Debug("sending b:"+bRead);
|
||||
if(typeUDP==1)
|
||||
{
|
||||
tempBuffer = new byte[512];//128];//invers3.bufferSize];
|
||||
bRead = recDev.read(tempBuffer, 0, 512);// 128);//invers3.bufferSize);
|
||||
udp.Send(tempBuffer,bRead);
|
||||
SM.Debug("UDP sending b:"+bRead);
|
||||
}
|
||||
else
|
||||
{
|
||||
tempBuffer = new byte[2048];//128];//invers3.bufferSize];
|
||||
bRead = recDev.read(tempBuffer, 0, 2048);// 128);//invers3.bufferSize);
|
||||
/*
|
||||
tempBuffer = new byte[16384];//128];//invers3.bufferSize];
|
||||
bRead = recDev.read(tempBuffer, 0,16384);// 128);//invers3.bufferSize);*/
|
||||
tcp.Send(tempBuffer,bRead);
|
||||
SM.Debug("TCP sending b:"+bRead);
|
||||
}
|
||||
}
|
||||
else Thread.sleep(1);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
SM.Debug("break:"+e.toString());
|
||||
//break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void PlaySound(byte[] data, int len)
|
||||
{
|
||||
if(playDev.getState() == playDev.STATE_INITIALIZED)
|
||||
{
|
||||
playDev.write(data, 0 , len);
|
||||
playDev.play();
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
SM.Debug("Cannot play sound playDev NOT init corectly");
|
||||
}
|
||||
}
|
||||
|
||||
public void StopSound()
|
||||
{
|
||||
if(playDev.getState() == playDev.STATE_INITIALIZED)
|
||||
{
|
||||
if(playDev.getPlayState() == playDev.PLAYSTATE_PLAYING)
|
||||
playDev.stop();
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
SM.Debug("Cannot STOP playDev");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package com.safemobile.lib.sound;
|
||||
|
||||
public interface ITCPaudioLis {
|
||||
public void dataRecv( TCPaudioEvent event ) ;
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package com.safemobile.lib.sound;
|
||||
|
||||
public interface IUDPListener {
|
||||
public void dataRecv( UDPevent event );
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
package com.safemobile.lib.sound;
|
||||
|
||||
import com.safemobile.lib.SM;
|
||||
|
||||
import android.media.AudioFormat;
|
||||
import android.media.AudioManager;
|
||||
import android.media.AudioRecord;
|
||||
import android.media.AudioTrack;
|
||||
import android.media.MediaRecorder;
|
||||
|
||||
public class RecordingHandle {
|
||||
public Boolean isAlive = false;
|
||||
public Boolean soundNeeded = false;
|
||||
private AudioTrack playDev =null;
|
||||
private int bufferSize;
|
||||
private TCPaudioClient tcp;
|
||||
private int recport = 50002;
|
||||
|
||||
public RecordingHandle(String IP)
|
||||
{
|
||||
try {
|
||||
tcp = new TCPaudioClient(IP,recport);
|
||||
int sampleRate = 8000;
|
||||
int bufsize = AudioTrack.getMinBufferSize(sampleRate, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
|
||||
//init play device
|
||||
playDev = new AudioTrack(AudioManager.STREAM_MUSIC,sampleRate,AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT,bufsize, AudioTrack.MODE_STREAM);
|
||||
|
||||
tcp.addTCPListener(new ITCPaudioLis()
|
||||
{
|
||||
|
||||
@Override
|
||||
public void dataRecv(TCPaudioEvent event) {
|
||||
byte[] data = event.data();
|
||||
int len = event.len();
|
||||
SM.Debug("recv b:"+len+" data.length:"+data.length);
|
||||
if(data!=null)
|
||||
PlaySound(data, len);
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
catch(Exception ex) {
|
||||
SM.Exception("recordings", ex.toString());
|
||||
}
|
||||
}
|
||||
|
||||
private void PlaySound(byte[] data, int len)
|
||||
{
|
||||
if(soundNeeded)
|
||||
{
|
||||
if(playDev.getState() == playDev.STATE_INITIALIZED)
|
||||
{
|
||||
playDev.write(data, 0 , data.length);
|
||||
playDev.play();
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
SM.Debug("Cannot play sound playDev NOT init corectly");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void StartSound()
|
||||
{
|
||||
|
||||
soundNeeded = true;
|
||||
}
|
||||
|
||||
public void StopSound()
|
||||
{
|
||||
if(playDev.getState() == playDev.STATE_INITIALIZED)
|
||||
{
|
||||
if(playDev.getPlayState() == playDev.PLAYSTATE_PLAYING)
|
||||
playDev.stop();
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
SM.Debug("Cannot STOP playDev");
|
||||
}
|
||||
soundNeeded= false;
|
||||
}
|
||||
}
|
@ -0,0 +1,200 @@
|
||||
package com.safemobile.lib.sound;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.Socket;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import com.safemobile.lib.SM;
|
||||
import com.safemobile.lib.TCPmsg;
|
||||
|
||||
public class TCPaudioClient implements Runnable{
|
||||
private boolean alive = true;
|
||||
|
||||
public String serverHostname = new String ("10.120.1.114");//
|
||||
private int port = 50001;
|
||||
private Thread listenThread;
|
||||
private Socket soc =null;
|
||||
|
||||
private InputStream recv;
|
||||
private OutputStream writer;
|
||||
|
||||
private volatile int n=0;
|
||||
|
||||
public Boolean connOK=false;
|
||||
byte[] buffer = new byte[16384];
|
||||
private List<ITCPaudioLis> _listeners = new ArrayList<ITCPaudioLis>();
|
||||
|
||||
public TCPaudioClient(String hostName, int _port)
|
||||
{
|
||||
serverHostname=hostName;
|
||||
this.port = _port;
|
||||
SM.Debug("---TCPhandler construcort--- port:"+_port);
|
||||
listenThread = new Thread(this, "TCPlisten");
|
||||
listenThread.start(); // (2) Start the thread.
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
soc = new Socket(serverHostname, port);
|
||||
SM.Debug("Socket timeout:" + soc.getSoTimeout() );
|
||||
//soc.setSoTimeout(5000);
|
||||
recv= soc.getInputStream();
|
||||
writer =soc.getOutputStream() ;
|
||||
if(soc !=null)
|
||||
connOK = true;
|
||||
|
||||
}
|
||||
catch (UnknownHostException e)
|
||||
{
|
||||
SM.Debug("UnknownHostException", "break:"+e.toString());
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
SM.Debug("IOException", "break:"+e.toString());
|
||||
}
|
||||
|
||||
while(alive)
|
||||
{
|
||||
try {
|
||||
Thread.sleep(3000);
|
||||
} catch (InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
SM.Debug(e.toString());
|
||||
}
|
||||
while(connOK)
|
||||
{
|
||||
try
|
||||
{
|
||||
buffer = new byte[16384];
|
||||
n = recv.read(buffer);
|
||||
if(n==-1)
|
||||
break;
|
||||
_fireDataArrived(buffer,n);
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
SM.Debug("break:"+ex.toString());
|
||||
connOK = false;
|
||||
}
|
||||
}//while(connOK)
|
||||
|
||||
try {
|
||||
Thread.sleep(3000);
|
||||
} catch (InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
SM.Debug(e.toString());
|
||||
}
|
||||
if(alive)RestartTCP();
|
||||
}//while(alive)
|
||||
SM.Debug("==================================");
|
||||
SM.Debug("TCP listenThread stoped!! alive = false");
|
||||
SM.Debug("==================================");
|
||||
}
|
||||
|
||||
public boolean Send(byte[] data,int len)
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
if(writer != null)
|
||||
{
|
||||
writer.write(data,0,len);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
// TODO Auto-generated catch block
|
||||
SM.Debug(e.toString());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void RestartTCP()
|
||||
{
|
||||
try
|
||||
{
|
||||
SM.Debug("Restarting TCP...ip:"+serverHostname);
|
||||
soc = new Socket(serverHostname, port);
|
||||
recv= soc.getInputStream();
|
||||
writer =soc.getOutputStream();
|
||||
if(soc !=null)
|
||||
connOK = true;
|
||||
|
||||
}
|
||||
catch (UnknownHostException e)
|
||||
{
|
||||
SM.Debug("break:"+e.toString());
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
SM.Debug("break:"+e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isAlive() {
|
||||
return alive;
|
||||
}
|
||||
|
||||
public void setAlive(boolean alive) {
|
||||
this.alive = alive;
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
|
||||
this.alive = false;
|
||||
if(soc !=null)
|
||||
{
|
||||
try {
|
||||
soc.close();
|
||||
soc = null;
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
SM.Debug("break:"+e.toString());
|
||||
}
|
||||
connOK = false;
|
||||
}
|
||||
// stop thread
|
||||
if(listenThread != null)
|
||||
{
|
||||
Thread moribund = listenThread;
|
||||
listenThread = null;
|
||||
moribund.interrupt();
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void addTCPListener( ITCPaudioLis l ) {
|
||||
_listeners.add( (ITCPaudioLis) l );
|
||||
}
|
||||
|
||||
public synchronized void removeTCPListener( ITCPaudioLis l ) {
|
||||
_listeners.remove( l );
|
||||
}
|
||||
|
||||
private synchronized void _fireDataArrived(byte[] data, int len) {
|
||||
TCPaudioEvent event = new TCPaudioEvent( this, data, len );
|
||||
Iterator<ITCPaudioLis> listeners = _listeners.iterator();
|
||||
while( listeners.hasNext() ) {
|
||||
( (ITCPaudioLis) listeners.next() ).dataRecv(event);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.safemobile.lib.sound;
|
||||
|
||||
import java.util.EventObject;
|
||||
|
||||
public class TCPaudioEvent extends EventObject{
|
||||
private byte[] data;
|
||||
private int len;
|
||||
public TCPaudioEvent(Object source, byte[] _data, int _len) {
|
||||
super(source);
|
||||
data =_data;
|
||||
len = _len;
|
||||
}
|
||||
public byte[] data() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public int len()
|
||||
{
|
||||
return len;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,109 @@
|
||||
package com.safemobile.lib.sound;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import com.safemobile.lib.SM;
|
||||
|
||||
public class UDPclient implements Runnable{
|
||||
|
||||
public String serverHostname = new String ("10.120.1.114");//
|
||||
private int UDP_SERVER_PORT_IN = 50002;
|
||||
private int UDP_SERVER_PORT_OUT = 50001;
|
||||
private DatagramSocket ds = null;
|
||||
private Thread listenThread;
|
||||
|
||||
private InetAddress serverAddr=null;
|
||||
private DatagramPacket dp_send = null;
|
||||
private DatagramPacket dp_recv = null;
|
||||
byte[] buffer = new byte[4096];
|
||||
|
||||
public Boolean isAlive = false;
|
||||
private List<IUDPListener> _listeners = new ArrayList<IUDPListener>();
|
||||
|
||||
public UDPclient(String ip)
|
||||
{
|
||||
SM.Debug("---UDPclient construcort---IP:"+ip);
|
||||
serverHostname = ip;
|
||||
try {
|
||||
serverAddr = InetAddress.getByName(serverHostname);
|
||||
ds = new DatagramSocket(UDP_SERVER_PORT_IN);
|
||||
dp_recv = new DatagramPacket(buffer, buffer.length);
|
||||
isAlive = true;
|
||||
} catch (UnknownHostException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
catch (SocketException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
listenThread = new Thread(this, "UDPclient");
|
||||
listenThread.start(); // (2) Start the thread.
|
||||
Send(new byte[]{1}, 1);
|
||||
SM.Debug("=====UDPclient construcort ===END====");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while(isAlive)
|
||||
{
|
||||
try
|
||||
{
|
||||
ds.receive(dp_recv);
|
||||
//String msg = new String(buffer, 0, dp_recv.getLength());
|
||||
//SM.Debug("UDPclient recv b:"+dp_recv.getLength());
|
||||
_fireDataArrived(buffer,dp_recv.getLength());
|
||||
}
|
||||
catch (IOException e) {
|
||||
SM.Debug("break:"+e.toString());
|
||||
}
|
||||
/*
|
||||
try {
|
||||
Thread.sleep(1);
|
||||
} catch (InterruptedException e) {
|
||||
SM.Debug("break:"+e.toString());
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
public Boolean Send(byte[] data,int len)
|
||||
{
|
||||
Boolean ret = true;
|
||||
|
||||
try
|
||||
{
|
||||
dp_send = new DatagramPacket(data, len, serverAddr, UDP_SERVER_PORT_OUT);
|
||||
ds.send(dp_send);
|
||||
}
|
||||
catch (IOException e) {
|
||||
SM.Debug("break:"+e.toString());
|
||||
ret = false;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public synchronized void addUDPListener( IUDPListener l ) {
|
||||
_listeners.add( l );
|
||||
}
|
||||
|
||||
public synchronized void removeUDPListener( IUDPListener l ) {
|
||||
_listeners.remove( l );
|
||||
}
|
||||
|
||||
private synchronized void _fireDataArrived(byte[] data, int len) {
|
||||
UDPevent event = new UDPevent( this, data, len );
|
||||
Iterator<IUDPListener> listeners = _listeners.iterator();
|
||||
while( listeners.hasNext() ) {
|
||||
( (IUDPListener) listeners.next() ).dataRecv(event);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.safemobile.lib.sound;
|
||||
|
||||
import java.util.EventObject;
|
||||
|
||||
public class UDPevent extends EventObject {
|
||||
|
||||
private byte[] data;
|
||||
private int len;
|
||||
public UDPevent(Object source, byte[] _data, int _len) {
|
||||
super(source);
|
||||
data =_data;
|
||||
len = _len;
|
||||
}
|
||||
public byte[] data() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public int len()
|
||||
{
|
||||
return len;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user