I have a multi-threaded process, 5 threads, and another thread working as a status object reporting the size of the BlockingQueue. Problem is the Status thread reports 100% first, which is correct, but then goes right to 0%.
I want it to count down the percentage.
Here is my code:
Thread[] workers = new Thread[NUMBER_OF_THREADS];
for (int x = 0; x < NUMBER_OF_THREADS; x++) {
workers[x] = new Thread(new S3ObjectDownloader(filesToDownload, currentYear));
workers[x].start();
}
for (int x = 0; x < NUMBER_OF_THREADS; x++) {
try {
workers[x].join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Here is the status object instantiation:
int downloadSize = filesToDownload.size();
Thread statusThread = new Thread(new Status(filesToDownload, currentYear,downloadSize,"DOWNLOADING..."));
statusThread.start();
Here is that actual Status object run method:
public void run() {
while(!queue.isEmpty()){
try {
float completion = (queue.size()*1)/this.queueSize;
System.out.println(this.jobeName+" : "+this.conferenceYear+ " completion..."+MessageFormat.format("{0,number,#.##%}",completion));
TimeUnit.SECONDS.sleep(30);;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Im adding in the actual S3ObjectDownloader:
public void run() {
//aws credentials
this.s3 = new AmazonS3Client(new ClasspathPropertiesFileCredentialsProvider());
//log4j configuration
PropertyConfigurator.configure("/home/ubuntu/log4j.properties");
//attempt to poll the queue
while (!queue.isEmpty()) {
String fileName = queue.poll() + ".mp4";
String FULL_PATH = "best_of_ats/" + this.conferenceYear + "/videos/" + fileName;
File f = new File("/home/ubuntu/" + fileName);
if (fileName != null && !f.exists() && s3.doesObjectExist(BUCKET_NAME, FULL_PATH)) {
OutputStream out = null;
InputStream in = null;
S3Object s3obj = null;
try {
s3obj = s3.getObject(this.BUCKET_NAME,
FULL_PATH);
in = s3obj.getObjectContent();
//System.out.println("Downloading File " + FULL_PATH + "....");
} catch (AmazonS3Exception s3e) {
// s3e.printStackTrace();
//System.out.println("Problem downloading file..." + FULL_PATH);
s3e.printStackTrace();
logger.info("Problem with file..." + FULL_PATH);
continue;
}
try {
out = new FileOutputStream(new File(fileName));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = in.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
out.flush();
out.close();
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//System.out.println("problem writing output..." + FULL_PATH);
logger.info("problem writing output..." +FULL_PATH);
continue;
}
}
} // end while...
}
And here is the Status Class:
public class Status implements Runnable {
private String conferenceYear;
private Queue<String>queue;
private int queueSize;
private String jobeName;
public Status(Queue<String> queue, String conferenceYear, int queueSize, String jobName){
this.conferenceYear = conferenceYear;
this.queue = queue;
this.queueSize = queueSize;
this.jobeName = jobName;
}
#Override
public void run() {
while(!queue.isEmpty()){
try {
float completion = (queue.size()*1)/this.queueSize;
System.out.println(this.jobeName+" : "+this.conferenceYear+ " completion..."+MessageFormat.format("{0,number,#.##%}",completion));
TimeUnit.SECONDS.sleep(30);;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Here is the calling class:
public static void main(String[] args) {
BlockingQueue<String> filesToDownload = new LinkedBlockingDeque<String>(1024);
BlockingQueue<String> filesToPreview = new LinkedBlockingDeque<String>(1024);
BlockingQueue<String> filesToUpload = new LinkedBlockingDeque<String>(1024);
String currentYear = String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
// DB connection.
ATSStoreDB db = new ATSStoreDB();
PreparedStatement st = null;
Connection conn = null;
conn = db.getConnection();
// get ids from ats_store.products.
try {
st = conn.prepareStatement(sql);
st.setString(1, currentYear);
ResultSet rs = st.executeQuery();
// add each id to IDS.
while (rs.next()) {
filesToDownload.add(rs.getString("product_id"));
filesToPreview.add(rs.getString("product_id"));
filesToUpload.add(rs.getString("product_id"));
}
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*
* Distribute IDS to several threads.
*/
//start up the Status Object class.
int downloadSize = filesToDownload.size();
Thread statusThread = new Thread(new Status(filesToDownload, currentYear,downloadSize,"DOWNLOADING..."));
statusThread.start();
/**
* download the files
*/
Thread[] workers = new Thread[NUMBER_OF_THREADS];
for (int x = 0; x < NUMBER_OF_THREADS; x++) {
workers[x] = new Thread(new S3ObjectDownloader(filesToDownload, currentYear));
workers[x].start();
}
for (int x = 0; x < NUMBER_OF_THREADS; x++) {
try {
workers[x].join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* create previews
*/
int previewSize = filesToPreview.size();
statusThread = new Thread(new Status(filesToPreview, currentYear,previewSize,"PREVIEWING..."));
statusThread.start();
workers = new Thread[NUMBER_OF_THREADS];
for (int x = 0; x < NUMBER_OF_THREADS; x++) {
workers[x] = new Thread(new Worker(filesToPreview, currentYear));
workers[x].start();
}
for (int x = 0; x < NUMBER_OF_THREADS; x++) {
try {
workers[x].join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I can spot one problem with your code right away:
float completion = (queue.size()*1)/this.queueSize;
What's the point of *1? Both queue.size() and this.queueSize are integers. You're turning integer division into... integer division. A good compiler will probably optimize it right away. You probably meant to write something like
float completion = (queue.size() * 1.0f) / this.queueSize;
Related
I have an array of threads called workers. There is another separate thread called status.
Both threads access a shared LinkedBlockingQueue. Workers(thread array) uses poll() to pull work and status reports the size of the queue every 30 seconds.
My problem in running this is I get the following printed from the status class:
UPLOADING...
PREVIEWING...
But PREVIEWING should appear before, and only before, UPLOADING.
So, I think my status object is not waiting for the first batch of workers to complete?
I want this:
DOWNLOADING....
PREVIEWING....
UPLOADING...
but instead things are a bit out of sync.
// start up the Status Object class.
int downloadSize = filesToDownload.size();
Thread statusThread = new Thread(new Status(filesToDownload, currentYear, downloadSize, "DOWNLOADING..."));
statusThread.start();
/**
* download the files
*/
Thread[] workers = new Thread[NUMBER_OF_THREADS];
for (int x = 0; x < NUMBER_OF_THREADS; x++) {
workers[x] = new Thread(new S3ObjectDownloader(filesToDownload, currentYear));
workers[x].start();
}
for (int x = 0; x < NUMBER_OF_THREADS; x++) {
try {
workers[x].join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* create previews
*/
int previewSize = filesToPreview.size();
statusThread = new Thread(new Status(filesToPreview, currentYear, previewSize, "PREVIEWING..."));
statusThread.start();
workers = new Thread[NUMBER_OF_THREADS];
for (int x = 0; x < NUMBER_OF_THREADS; x++) {
workers[x] = new Thread(new Worker(filesToPreview, currentYear));
workers[x].start();
}
for (int x = 0; x < NUMBER_OF_THREADS; x++) {
try {
workers[x].join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* upload previews to S3.
*/
// we need the TransferManager for the uploads.
TransferManager txManager = new TransferManager(new ClasspathPropertiesFileCredentialsProvider());
statusThread = new Thread(new Status(filesToUpload, currentYear, filesToUpload.size(), "UPLOADING..."));
statusThread.start();
workers = new Thread[NUMBER_OF_THREADS];
for (int x = 0; x < NUMBER_OF_THREADS; x++) {
workers[x] = new Thread(new S3ObjectUploader(filesToUpload, currentYear, txManager));
workers[x].start();
}
for (int x = 0; x < NUMBER_OF_THREADS; x++) {
try {
workers[x].join();
} catch (InterruptedException e) {
// TODO Auto-generated catch
// block
e.printStackTrace();
}
}
// shutdown transfer manager
txManager.shutdownNow();
Here is Status.java
public class Status implements Runnable {
private String conferenceYear;
private Queue<String>queue;
private int queueSize;
private String jobeName;
public Status(Queue<String> queue, String conferenceYear, int queueSize, String jobName){
this.conferenceYear = conferenceYear;
this.queue = queue;
this.queueSize = queueSize;
this.jobeName = jobName;
}
#Override
public void run() {
while(!queue.isEmpty()){
try {
float completion = (queue.size() * 1.0f) / this.queueSize;
System.out.println(this.jobeName+" : "+this.conferenceYear+ " remaining..."+MessageFormat.format("{0,number,#.##%}",completion));
TimeUnit.SECONDS.sleep(30);;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Java has CountDownlatches to support such scenarios.
Take a look at CountDownLatch class.
The link also contains a sample implementation which is very easy to understand. You can create latches to signal start and wait for end for each of your task.
I have manage to write a async class that send a picture over broadcast and wait for clients inverse ack and then send the packages that were not received...
The issue I am facing is that for sending 1mb picture it takes really long (1min aprox) to complete ussing only two phones connecting to each other via wifi. Here is my code (sorry for the long code)
SENDER CLASS
public class SenderBroadcastAsyncTask extends AsyncTask<File, Integer, String> {
Context context;
public SenderBroadcastAsyncTask(Context context) {
this.context = context.getApplicationContext();
}
#Override
protected String doInBackground(File... imagen) {
Log.d("SENDER","INICIANDO EL SOCKET PARA ENVIAR");
InetAddress group = null;
try {
group = InetAddress.getByName("192.168.49.255");
} catch (UnknownHostException e) {
e.printStackTrace();
}
int port =1212;
DatagramSocket socket = null;
try {
socket = new DatagramSocket(port);
} catch (SocketException e) {
e.printStackTrace();
}
try {
socket.setBroadcast(true);
} catch (SocketException e) {
e.printStackTrace();
}
DatagramSocket rsocket = null;
try {
rsocket = new DatagramSocket(1513);
} catch (SocketException e) {
e.printStackTrace();
}
try {
rsocket.setSoTimeout(2000);
} catch (SocketException e) {
e.printStackTrace();
}
for (File i:imagen) {
Bitmap bitmap = BitmapFactory.decodeFile(i.getAbsolutePath());
ByteArrayOutputStream stream2 = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream2);
byte[] prebuf = stream2.toByteArray();
mandar(prebuf, rsocket, socket, group, port);
}
return "ok";
}
public void mandar(byte[] prebuf,DatagramSocket rsocket,DatagramSocket socket,InetAddress group,int port) {
int DATAGRAM_MAX_SIZE = 50*1024;
int cantidadpedazos = (int) Math.ceil(prebuf.length / (float) DATAGRAM_MAX_SIZE);
Log.d("Pedazos", Integer.toString(cantidadpedazos));
//Loop peaces
for (int i = 1; i <= cantidadpedazos; i++) {
enviar(i,socket,group,port,prebuf,DATAGRAM_MAX_SIZE,cantidadpedazos);
}
////FINAL DONE
String message_to_send = "DONE";
byte[] ultimo = message_to_send.getBytes();
DatagramPacket ultimopaquete = new DatagramPacket(ultimo, ultimo.length, group, port);
try {
socket.send(ultimopaquete);
} catch (IOException e) {
e.printStackTrace();
}
/*AFTER DONE WAIT FOR INVERSE ACK OF NUMERED PACKETS*/
boolean mandarfaltan = false;
boolean ack = false;
int countertoend=0;
boolean cancelcheck = false;
while (!ack) {
byte[] recibir = new byte[5];
DatagramPacket pkgrecibir = new DatagramPacket(recibir, 0, recibir.length);
Log.d("UDP", "S: Receiving...");
try {
rsocket.receive(pkgrecibir);
} catch (SocketTimeoutException e) {
Log.d("timeout","TIMEOUT EXCEPTION");
if (mandarfaltan) {
try {
socket.send(ultimopaquete);
} catch (IOException q) {
q.printStackTrace();
}
if (countertoend ==7)
{
Log.d("error","timeout error no response to done");
socket.close();
rsocket.close();
break;
}
cancelcheck = true;
countertoend++;
} else {
Log.d("ACK", "TIMEOUT CANELING PASSING TO DONE");
socket.close();
rsocket.close();
break;
}
} catch (IOException e) {
e.printStackTrace();
}
mandarfaltan = true;
if (!cancelcheck) {
String faltast = new String(pkgrecibir.getData()).trim();
Integer faltaint = Integer.parseInt(faltast); ////euivalent to for i
enviar(faltaint, socket, group, port, prebuf, DATAGRAM_MAX_SIZE, cantidadpedazos);
if (new String(pkgrecibir.getData()).trim().contains("DONE")) {
Log.d("ACK", "Received" + " " + new String(pkgrecibir.getData()).trim());
ack = true;
}
}
cancelcheck = false;
}
}
public void enviar(int i,DatagramSocket socket,InetAddress group,int port,byte[] prebuf,int DATAGRAM_MAX_SIZE,int cantidadpedazos){
//for (int i = 1; i <= cantidadpedazos; i++) {
////EN cada interaccion del for clono el array con las ip q recibiran el paquete
//ArrayList<String> finallist = (ArrayList<String>) tosend.clone();
String final_str = null;
String counter_str = Integer.toString(i);
int len = counter_str.length();
byte[] final_strbyte = new byte[5];
Log.d("HEADER", counter_str + " TAMANO " + Integer.toString(len));
switch (len) {
case 1:
final_str = "0000" + Integer.toString(i);
final_strbyte = final_str.getBytes();
break;
case 2:
final_str = "000" + counter_str;
final_strbyte = final_str.getBytes();
break;
case 3:
final_str = "00" + counter_str;
final_strbyte = final_str.getBytes();
break;
case 4:
final_str = "0" + counter_str;
final_strbyte = final_str.getBytes();
break;
case 5:
final_str = counter_str;
final_strbyte = final_str.getBytes();
break;
}
byte[] slice, imagetosend;
DatagramPacket packet;
if (i == 1) {
slice = Arrays.copyOfRange(prebuf, 0, i * DATAGRAM_MAX_SIZE);
imagetosend = new byte[slice.length + 5];
for (int s = 0; s < 5; s++) {
imagetosend[s] = final_strbyte[s];
}
for (int s = 0; s < slice.length; s++) {
imagetosend[s + 5] = slice[s];
}
packet = new DatagramPacket(imagetosend, imagetosend.length, group, port);
for (int s = 0; s < 1; s++) {
try {
socket.send(packet);
} catch (IOException e) {
e.printStackTrace();
}
}
} else if (i == cantidadpedazos) {
slice = Arrays.copyOfRange(prebuf, (i - 1) * DATAGRAM_MAX_SIZE, prebuf.length);
imagetosend = new byte[slice.length + 5];
for (int s = 0; s < 5; s++) {
imagetosend[s] = final_strbyte[s];
}
for (int s = 0; s < slice.length; s++) {
imagetosend[s + 5] = slice[s];
}
packet = new DatagramPacket(imagetosend, imagetosend.length, group, port);
for (int s = 0; s < 2; s++) {
try {
socket.send(packet);
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
slice = Arrays.copyOfRange(prebuf, ((i - 1) * DATAGRAM_MAX_SIZE), i * DATAGRAM_MAX_SIZE);
imagetosend = new byte[slice.length + 5];
for (int s = 0; s < 5; s++) {
imagetosend[s] = final_strbyte[s];
}
for (int s = 0; s < slice.length; s++) {
imagetosend[s + 5] = slice[s];
}
packet = new DatagramPacket(imagetosend, imagetosend.length, group, port);
for (int s = 0; s < 2; s++) {
try {
socket.send(packet);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
RECEIVE CLASS
public class ReceiverBroadcastAsyncTask extends AsyncTask<Void, Integer ,String > {
boolean running = true;
Context context;
//public int counter = 1;
public ReceiverBroadcastAsyncTask(Context context) {
this.context = context.getApplicationContext();
}
#Override
protected String doInBackground(Void... params) {
Log.d("CLASS","INSIDE_MULTICAST RECEIVER");
////receive socket
int port =1212;
DatagramSocket socket = null;
try {
socket = new DatagramSocket(port);
} catch (SocketException e) {
e.printStackTrace();
}
try {
socket.setBroadcast(true);
} catch (SocketException e) {
e.printStackTrace();
}
//////send socket
int eport = 1513;
InetAddress eip = null;
try {
eip = InetAddress.getByName("192.168.49.1");
} catch (UnknownHostException e) {
e.printStackTrace();
}
DatagramSocket esocket = null;
try {
esocket = new DatagramSocket(eport);
} catch (SocketException e) {
e.printStackTrace();
}
//////end sockets
/////Array used for organize the received packages and log the received headers to send inverse ack
ArrayList<Integer> headers = new ArrayList<Integer>();
ArrayList<byte[]> contenido = new ArrayList<byte[]>();
for (int a=0;a<30000;a++)
{
contenido.add(null);
}
ByteArrayOutputStream imagen = new ByteArrayOutputStream();
int counter = 0;
////////end arrays
//////Start receive
while(true)
{
byte[] message = new byte[60*1024];
DatagramPacket recv_packet = new DatagramPacket(message, message.length);
try {
socket.receive(recv_packet);
} catch (IOException e) {
e.printStackTrace();
}
byte[] order = new byte[5];
Log.d("UDP", "S: Receiving...escuchando en el puerto " + recv_packet.getPort() );
String rec_str;
String rec_order;
Log.d("PACKAGE LENGTH",Integer.toString(recv_packet.getLength()));
if(recv_packet.getLength()>5) ////Packages that are minor to 5 are done packages
{
byte[] buff = new byte[recv_packet.getLength()-5];
System.arraycopy(recv_packet.getData(), 5, buff, 0, buff.length);
System.arraycopy(recv_packet.getData(), 0, order, 0, 5);
rec_str = new String(buff);
rec_order = new String(order);
Log.d("DATA", " " + counter + " " + rec_order);
/////add int counter to array
///////process buff to add to contenido array whit headers as index
if (process(headers,contenido,rec_str,imagen,buff,counter,Integer.parseInt(rec_order),esocket)){
counter =Integer.parseInt(rec_order);}
////end buff processing
}
else ////done package
{
byte[] buff = new byte[recv_packet.getLength()];
System.arraycopy(recv_packet.getData(), 0, buff, 0, buff.length);
rec_str = new String(buff);
counter=0;
///procesar paquetes done o querys..
if (process(headers,contenido,rec_str, imagen, buff, counter, 0,esocket)){
//counter++;
}
}
////end while
}
////end doInBackground
}
/*image processing bound to notification*/
public void procesar( ByteArrayOutputStream imagen) {
Log.d("IMAGEN_PROCESSING", "INSIDE....");
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath();
File file = new File(fullPath, "something" + timeStamp + ".png");
try {
File dir = new File(fullPath);
if (!dir.exists()) {
dir.mkdirs();
}
OutputStream fOut = null;
file.createNewFile();
fOut = new FileOutputStream(file.getPath());
Log.d("File created", file.getAbsolutePath());
fOut.write(imagen.toByteArray());
if(fOut!=null){fOut.close();}
} catch (Exception e) {
}
Bitmap bmp = BitmapFactory.decodeByteArray(imagen.toByteArray(),0,imagen.toByteArray().length);
notification(context,bmp,file);
}
/*end image processing */
/*INVERSE ACK*/
public void inverseack(ArrayList<byte[]> contenido ,ArrayList<Integer> headers,DatagramSocket esocket){
running = false;
Set<Integer> noduplicate = new HashSet<>();
noduplicate.addAll(headers);
headers.clear();
headers.addAll(noduplicate);
Collections.sort(headers);
ArrayList<Integer> faltante= new ArrayList<Integer>();
if (headers.size()!=0)
{
for (int o=1 ; o<=headers.get(headers.size()-1);o++)
{
if(!headers.contains(o)) {
faltante.add(o);
Log.d("Falta"," "+o);
}
}
if (faltante.isEmpty()) {
Log.d("PROCESAR","YA ESTA TODO, listo para procesar imagen");
ByteArrayOutputStream finalbyte = new ByteArrayOutputStream();
for(byte[] w:contenido)
{
if (w!=null) {
finalbyte.write(w, 0, w.length);
}
else break;
}
String message_to_send = "DONE";
byte[] ultimo = message_to_send.getBytes();
InetAddress group = null;
try {
group = InetAddress.getByName("192.168.49.1");
} catch (UnknownHostException e) {
e.printStackTrace();
}
DatagramPacket ultimopaquete = new DatagramPacket(ultimo, ultimo.length, group, 1513);
try {
esocket.send(ultimopaquete);
} catch (IOException e) {
e.printStackTrace();
}
procesar(finalbyte);
headers.clear();
contenido.clear();
/////imagen processing ready ... process
Log.d("PROCESAR","YA ESTA TODO, listo para procesar imagen");
}
else {
for (int enviar : faltante) {
String faltantevalue = Integer.toString(enviar);
byte[] enviarfaltante = new byte[faltantevalue.length()];
enviarfaltante = faltantevalue.getBytes();
InetAddress group = null;
try {
group = InetAddress.getByName("192.168.49.1"); //<-- Wifi direct group server ip address
} catch (UnknownHostException e) {
e.printStackTrace();
}
DatagramPacket ultimopaquete = new DatagramPacket(enviarfaltante, enviarfaltante.length, group, 1513);
try {
esocket.send(ultimopaquete);
} catch (IOException e) {
e.printStackTrace();
}
}
}
faltante.clear();
running=true;
///end for
}///end else
////end inverseack
}
///////Process received packages
public boolean process(ArrayList<Integer> headers,ArrayList<byte[]> contenido,String string,ByteArrayOutputStream total,byte[] buff,int counter,int rec,DatagramSocket esocket)
{
Log.d("rec" + Integer.toString(rec),"counter " + Integer.toString(counter));
if (string.contains("DONE")) {
Log.d("INVERSEACK","SENDING INVERSE ACK");
if (running){ inverseack(contenido,headers,esocket);}
counter = 0;
//return false;
}
else if (rec != counter) {
contenido.set(rec-1,buff);
headers.add(rec);
Log.d("RECIBED", "NEW PACKAGE WILL BE SEND");
return true;
}
else{return false;}
return false;
}
///////////////NOTIFICATION
public void notification(Context context,Bitmap bpm, File imagen) {
Intent pendingintent = new Intent();
pendingintent.setAction(Intent.ACTION_VIEW);
pendingintent.setDataAndType(Uri.fromFile(imagen),"image/*");
PendingIntent intentpending = PendingIntent.getActivity(this.context, 0, pendingintent, 0);
NotificationManager notification = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification noti = new Notification.Builder(context)
.setContentTitle("Photo!")
.setContentText("Photo CLICK to see")
.setSmallIcon(R.drawable.photo)
.setContentIntent(intentpending)
.setLargeIcon(bpm)
.setLights(-256, 100, 100)
.build();
notification.notify(0,noti);
}
#Override
protected void onPostExecute(String result) {
//do whatever...
}
protected void onProgressUpdate(Integer... Progress)
{
}
}
I really appreciate if someone can check and let me know what may be causing that BIG delay on file reception. (it is udp it´s suppose to be quick)
Thanks in advance
i have created a list filed which contains a Picture and some data in one row
the picture and data i am getting from Json. My code is working but after getting the list my UI is hanging,
There are Two steps
1) parse the data and image URL from json
2) and show in list field
I'll post my code and screen shot of List below
http://supportforums.blackberry.com/t5/image/serverpage/image-id/19759iD4089A71454F66E0/image-size/large/is-moderation-mode/true?v=mpbl-1&px=600
public final class MyScreen extends MainScreen implements ListFieldCallback
{
HttpConnection httpConn;
int responseCode;
private static ListField _list;
private static Vector listElements = new Vector();
private static Vector elements = new Vector();
private static Vector contentelements = new Vector();
private static Vector datelements = new Vector();
private static Vector Imageselements = new Vector();
LabelField label;
String imagename;
Bitmap bit ;
Connection connectionthread;
public MyScreen()
{
// Set the displayed title of the screen
//setTitle("MyTitle");
_list = new ListField();
_list.invalidate();
_list.setEmptyString("please wait..", DrawStyle.HCENTER);
_list.setRowHeight(100);
_list.setCallback(this);
add(_list);
connectionthread = new Connection();
connectionthread.start();
}
public class Connection extends Thread{
public void run() {
try {
String httpURL = "http://192.168.1.91/bjp_app/program"+ getConnectionString();
httpConn = (HttpConnection) Connector.open(httpURL);
httpConn.setRequestMethod(HttpConnection.GET);
responseCode = httpConn.getResponseCode();
if (responseCode != HttpConnection.HTTP_OK)
{
throw new IOException("HTTP response code: "+ responseCode);
//System.out.println("\n Internaet problem HttpConnection. = "+responseCode);
}else{
System.out.println("\n elseeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee= ");
System.out.println("\nHttpConnection.HTTP_OK = "+responseCode);
DataOutputStream _outStream = new DataOutputStream(httpConn.openDataOutputStream());
byte[] request_body = httpURL.getBytes();
for (int i = 0; i < request_body.length; i++) {
_outStream.writeByte(request_body[i]);
}
DataInputStream _inputStream = new DataInputStream(
httpConn.openInputStream());
StringBuffer _responseMessage = new StringBuffer();
int ch;
while ((ch = _inputStream.read()) != -1) {
_responseMessage.append((char) ch);
}
String res = (_responseMessage.toString());
String responce = res.trim();
System.out.println("\nresponce= "+responce);
JSONArray jsnarry = new JSONArray(responce);
System.out.println("\n--length----- "+jsnarry.length());
for (int i = 0; i < jsnarry.length(); i++){
JSONArray inerarray = jsnarry.getJSONArray(i);
System.out.println("\n-innerarray-length----- "+inerarray.length());
//for (int i1 = 0; i1 < inerarray.length(); i1++) {
//System.out.println("\n-inerarray-values----- "+inerarray.getString(i1));
String ID = inerarray.getString(0);
String TITTLE = inerarray.getString(1);
String CONTENT = inerarray.getString(2);
String DATE = inerarray.getString(3);
String IMAGE = inerarray.getString(4);
String six = inerarray.getString(5);
System.out.println("................................................");
System.out.println("ID= "+ID);
System.out.println("TITTLE= "+TITTLE);
System.out.println("CONTENT= "+CONTENT);
System.out.println("DATE= "+DATE);
System.out.println("IMAGE= "+IMAGE);
imagename = "http://sanjaytandon.in/admin/image/"+IMAGE.trim();
System.out.println("imagename= "+imagename);
//System.out.println("six "+six);
System.out.println("....................................................=");
// String jsonresponse = ""+inerarray.getString(i1);
//label = new LabelField(jsonresponse,LabelField.FOCUSABLE);
//add(label);
//}
elements.addElement(TITTLE);
contentelements.addElement(CONTENT);
datelements.addElement(DATE);
Imageselements.addElement(imagename);
}
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
try {
_list.setSize(elements.size());
_list.setSize(contentelements.size());
_list.setSize(datelements.size());
_list.setSize(Imageselements.size());
//invalidate();
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("error _list.setSize"+e.toString());
}
}
});
}
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("\nresponce code error "+e.toString());
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
// TODO Auto-generated method stub
Status.show("Check your internet connection!", 2000);
}
});
}
}
}
public void drawListRow (ListField listField, Graphics graphics, int index, int y, int w) {
try {
graphics.setGlobalAlpha(255);
final int margin =5;
String tittle = (String)elements.elementAt(index);
String content = (String)contentelements.elementAt(index);
String date = (String)datelements.elementAt(index);
String imagesurl = (String)Imageselements.elementAt(index);
UrlToImage img = new UrlToImage( imagesurl);
bit =img.getbitmap();
graphics.drawText(tittle.trim(), 3*margin+bit.getWidth(), y+margin);
graphics.drawText(content.trim(), 3*margin+bit.getWidth(), y+ margin+30);
graphics.drawText(date.trim(), 3*margin+bit.getWidth(), y+ margin+60);
graphics.drawBitmap(2, y+margin+5, bit.getWidth(), bit.getHeight(), bit, 0, 0);
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("error drawListRow"+e.toString());
}
}
public Object get(ListField listField, int index) {
// TODO Auto-generated method stub
return null;
}
public int getPreferredWidth(ListField listField) {
// TODO Auto-generated method stub
return 0;
}
public int indexOfList(ListField listField, String prefix, int start) {
// TODO Auto-generated method stub
return 0;
}
and MY UrlToImage.java class whcih downloads the images from URL
public class UrlToImage {
public static Bitmap _bmap;
UrlToImage(final String url){
HttpConnection connection = null;
InputStream inputStream = null;
EncodedImage bitmap;
byte[] dataArray = null;
try {
connection = (HttpConnection) Connector.open(url+ getConnectionString(), Connector.READ, true);
inputStream = connection.openInputStream();
byte[] responseData = new byte[10000];
int length = 0;
StringBuffer rawResponse = new StringBuffer();
while (-1 != (length = inputStream.read(responseData)))
{
rawResponse.append(new String(responseData, 0, length));
}
int responseCode = connection.getResponseCode();
if (responseCode != HttpConnection.HTTP_OK)
{
throw new IOException("HTTP response code: "+ responseCode);
}else{
System.out.println("\nHttpConnection.HTTP_OK = "+responseCode);
}
final String result = rawResponse.toString();
dataArray = result.getBytes();
/*System.out.println("\ndataArray.length = "+dataArray.length);
System.out.println("\ndataArray.length = "+dataArray[0]);
System.out.println("\ndataArray.length = "+dataArray[1]);
System.out.println("\ndataArray.length = "+dataArray[2]);
System.out.println("\ndataArray.length = "+dataArray[3]);*/
} catch (final Exception ex) {
}finally {
try {
inputStream.close();
inputStream = null;
connection.close();
connection = null;
}
catch(Exception e){
}
}
bitmap = EncodedImage.createEncodedImage(dataArray, 0,dataArray.length);
// this will scale your image acc. to your height and width of bitmapfield
int multH;
int multW;
int currHeight = bitmap.getHeight();
int currWidth = bitmap.getWidth();
multH= Fixed32.div(Fixed32.toFP(currHeight),Fixed32.toFP(80));//height
multW = Fixed32.div(Fixed32.toFP(currWidth),Fixed32.toFP(80));//width
bitmap = bitmap.scaleImage32(multW,multH);
_bmap=bitmap.getBitmap();
}
public String getConnectionString() {
String connectionString = null;
// Simulator behaviour is controlled by the USE_MDS_IN_SIMULATOR
// variable.
if (DeviceInfo.isSimulator()) {
connectionString = ";deviceside=true";
System.out.println("11111111111111111");
}
// Wifi is the preferred transmission method
else if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) {
connectionString = ";interface=wifi";
System.out.println("222222222222222222");
}
// Is the carrier network the only way to connect?
else if ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT)
{
String carrierUid = getCarrierBIBSUid();
if (carrierUid == null) {
// Has carrier coverage, but not BIBS. So use the carrier's TCP
// network
System.out.println("33333333333333333");
connectionString = ";deviceside=true";
} else {
// otherwise, use the Uid to construct a valid carrier BIBS
// request
System.out.println("444444444444444444");
connectionString = ";deviceside=false;connectionUID="+carrierUid + ";ConnectionType=mds-public";
}
}
// Check for an MDS connection instead (BlackBerry Enterprise Server)
else if ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS)
{
System.out.println("55555555555555555555");
connectionString = ";deviceside=false";
}
// If there is no connection available abort to avoid hassling the user
// unnecssarily.
else if (CoverageInfo.getCoverageStatus() == CoverageInfo.COVERAGE_NONE)
{
System.out.println("66666666666666666666");
connectionString = "none";
}
// In theory, all bases are covered by now so this shouldn't be reachable.But hey, just in case ...
else {
System.out.println("77777777777777777777777");
connectionString = ";deviceside=true";
}
return connectionString;
}
/**//**
* Looks through the phone's service book for a carrier provided BIBS
* network
*
* #return The uid used to connect to that network.
*//*
*/ private synchronized String getCarrierBIBSUid() {
ServiceRecord[] records = ServiceBook.getSB().getRecords();
int currentRecord;
for (currentRecord = 0; currentRecord < records.length; currentRecord++) {
if (records[currentRecord].getCid().toLowerCase().equals("ippp")) {
if (records[currentRecord].getName().toLowerCase()
.indexOf("bibs") >= 0) {
return records[currentRecord].getUid();
}
}
}
return null;
}
public Bitmap getbitmap()
{
return _bmap;
}
}
For a ListField, this method:
public void drawListRow (ListField listField, Graphics graphics, int index, int y, int w)
will be called on the main (a.k.a. "UI") thread. This method is intended for drawing. However, you have these lines of code in that method:
UrlToImage img = new UrlToImage( imagesurl);
bit =img.getbitmap();
As you have implemented the UrlToImage class, that code is making a network call. Network calls should not be made on the UI thread. If you do so, your UI will hang/freeze.
What you should do is create a background worker (thread) that will go to the network to fetch your images. You can decide whether you need to load all images immediately in the background, or only load images as the user scrolls the ListField. When the images arrive (on the background thread), then you should tell the list field to redraw itself, row by row.
You might want to take a look at this recent answer, which performs lazy loading of imagery in the background. That answer is a little different, in that it uses a custom list, not the standard ListField. For a standard ListField, you can trigger a redraw of a list row with ListField#invalidate(int):
// image for row 'x' download complete. save image as member data
// that can be accessed in drawListRow().
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
_list.invalidate(x);
}
});
i have written a java code to transfer files from one server to another using the concept of socket programming. now in the same code i also want to check for the network connection availability before each file is transferred. i also want that the files transferred should be transferred according to their numerical sequence. And while the files are being transferred the transfer progress of each file i.e the progress percentage bar should also be visible on the screen.
i will really appreciate if someone can help me with the code. i am posting the code i have written for file transfer.
ClientMain.java
import java.io.*;
import java.net.Socket;
import java.net.SocketException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class ClientMain {
private DirectoryTxr transmitter = null;
Socket clientSocket = null;
private boolean connectedStatus = false;
private String ipAddress;
String srcPath = null;
String dstPath = "";
public ClientMain() {
}
public void setIpAddress(String ip) {
this.ipAddress = ip;
}
public void setSrcPath(String path) {
this.srcPath = path;
}
public void setDstPath(String path) {
this.dstPath = path;
}
private void createConnection() {
Runnable connectRunnable = new Runnable() {
public void run() {
while (!connectedStatus) {
try {
clientSocket = new Socket(ipAddress, 3339);
connectedStatus = true;
transmitter = new DirectoryTxr(clientSocket, srcPath, dstPath);
} catch (IOException io) {
io.printStackTrace();
}
}
}
};
Thread connectionThread = new Thread(connectRunnable);
connectionThread.start();
}
public static void main(String[] args) {
ClientMain main = new ClientMain();
main.setIpAddress("10.6.3.92");
main.setSrcPath("E:/temp/movies/");
main.setDstPath("D:/tcp/movies");
main.createConnection();
}
}
(DirectoryTxr.java)
import java.io.*;
import java.net.Socket;
import java.net.SocketException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class DirectoryTxr {
Socket clientSocket = null;
String srcDir = null;
String dstDir = null;
byte[] readBuffer = new byte[1024];
private InputStream inStream = null;
private OutputStream outStream = null;
int state = 0;
final int permissionReqState = 1;
final int initialState = 0;
final int dirHeaderSendState = 2;
final int fileHeaderSendState = 3;
final int fileSendState = 4;
final int fileFinishedState = 5;
private boolean isLive = false;
private int numFiles = 0;
private int filePointer = 0;
String request = "May I send?";
String respServer = "Yes,You can";
String dirResponse = "Directory created...Please send files";
String fileHeaderRecvd = "File header received ...Send File";
String fileReceived = "File Received";
String dirFailedResponse = "Failed";
File[] opFileList = null;
public DirectoryTxr(Socket clientSocket, String srcDir, String dstDir) {
try {
this.clientSocket = clientSocket;
inStream = clientSocket.getInputStream();
outStream = clientSocket.getOutputStream();
isLive = true;
this.srcDir = srcDir;
this.dstDir = dstDir;
state = initialState;
readResponse(); //starting read thread
sendMessage(request);
state = permissionReqState;
} catch (IOException io) {
io.printStackTrace();
}
}
private void sendMessage(String message) {
try {
sendBytes(request.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
/**
* Thread to read response from server
*/
private void readResponse() {
Runnable readRunnable = new Runnable() {
public void run() {
while (isLive) {
try {
int num = inStream.read(readBuffer);
if (num > 0) {
byte[] tempArray = new byte[num];
System.arraycopy(readBuffer, 0, tempArray, 0, num);
processBytes(tempArray);
}
} catch (SocketException se) {
System.exit(0);
} catch (IOException io) {
io.printStackTrace();
isLive = false;
}
}
}
};
Thread readThread = new Thread(readRunnable);
readThread.start();
}
private void sendDirectoryHeader() {
File file = new File(srcDir);
if (file.isDirectory()) {
try {
String[] childFiles = file.list();
numFiles = childFiles.length;
String dirHeader = "$" + dstDir + "#" + numFiles + "&";
sendBytes(dirHeader.getBytes("UTF-8"));
} catch (UnsupportedEncodingException en) {
en.printStackTrace();
}
} else {
System.out.println(srcDir + " is not a valid directory");
}
}
private void sendFile(String dirName) {
File file = new File(dirName);
if (!file.isDirectory()) {
try {
int len = (int) file.length();
int buffSize = len / 8;
//to avoid the heap limitation
RandomAccessFile raf = new RandomAccessFile(file, "rw");
FileChannel channel = raf.getChannel();
int numRead = 0;
while (numRead >= 0) {
ByteBuffer buf = ByteBuffer.allocate(1024 * 100000);
numRead = channel.read(buf);
if (numRead > 0) {
byte[] array = new byte[numRead];
System.arraycopy(buf.array(), 0, array, 0, numRead);
sendBytes(array);
}
}
System.out.println("Finished");
} catch (IOException io) {
io.printStackTrace();
}
}
}
private void sendHeader(String fileName) {
try {
File file = new File(fileName);
if (file.isDirectory())
return;//avoiding child directories to avoid confusion
//if want we can sent them recursively
//with proper state transitions
String header = "&" + fileName + "#" + file.length() + "*";
sendHeader(header);
sendBytes(header.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
private void sendBytes(byte[] dataBytes) {
synchronized (clientSocket) {
if (outStream != null) {
try {
outStream.write(dataBytes);
outStream.flush();
} catch (IOException io) {
io.printStackTrace();
}
}
}
}
private void processBytes(byte[] data) {
try {
String parsedMessage = new String(data, "UTF-8");
System.out.println(parsedMessage);
setResponse(parsedMessage);
} catch (UnsupportedEncodingException u) {
u.printStackTrace();
}
}
private void setResponse(String message) {
if (message.trim().equalsIgnoreCase(respServer) && state == permissionReqState) {
state = dirHeaderSendState;
sendDirectoryHeader();
} else if (message.trim().equalsIgnoreCase(dirResponse) && state == dirHeaderSendState) {
state = fileHeaderSendState;
if (LocateDirectory()) {
createAndSendHeader();
} else {
System.out.println("Vacant or invalid directory");
}
} else if (message.trim().equalsIgnoreCase(fileHeaderRecvd) && state == fileHeaderSendState) {
state = fileSendState;
sendFile(opFileList[filePointer].toString());
state = fileFinishedState;
filePointer++;
} else if (message.trim().equalsIgnoreCase(fileReceived) && state == fileFinishedState) {
if (filePointer < numFiles) {
createAndSendHeader();
}
System.out.println("Successfully sent");
} else if (message.trim().equalsIgnoreCase(dirFailedResponse)) {
System.out.println("Going to exit....Error ");
// System.exit(0);
} else if (message.trim().equalsIgnoreCase("Thanks")) {
System.out.println("All files were copied");
}
}
private void closeSocket() {
try {
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private boolean LocateDirectory() {
boolean status = false;
File file = new File(srcDir);
if (file.isDirectory()) {
opFileList = file.listFiles();
numFiles = opFileList.length;
if (numFiles <= 0) {
System.out.println("No files found");
} else {
status = true;
}
}
return status;
}
private void createAndSendHeader() {
File opFile = opFileList[filePointer];
String header = "&" + opFile.getName() + "#" + opFile.length() + "*";
try {
state = fileHeaderSendState;
sendBytes(header.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
}
}
private void sendListFiles() {
createAndSendHeader();
}
}
(ServerMain.java)
public class ServerMain {
public ServerMain() {
}
public static void main(String[] args) {
DirectoryRcr dirRcr = new DirectoryRcr();
}
}
(DirectoryRcr.java)
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
public class DirectoryRcr {
String request = "May I send?";
String respServer = "Yes,You can";
String dirResponse = "Directory created...Please send files";
String dirFailedResponse = "Failed";
String fileHeaderRecvd = "File header received ...Send File";
String fileReceived = "File Received";
Socket socket = null;
OutputStream ioStream = null;
InputStream inStream = null;
boolean isLive = false;
int state = 0;
final int initialState = 0;
final int dirHeaderWait = 1;
final int dirWaitState = 2;
final int fileHeaderWaitState = 3;
final int fileContentWaitState = 4;
final int fileReceiveState = 5;
final int fileReceivedState = 6;
final int finalState = 7;
byte[] readBuffer = new byte[1024 * 100000];
long fileSize = 0;
String dir = "";
FileOutputStream foStream = null;
int fileCount = 0;
File dstFile = null;
public DirectoryRcr() {
acceptConnection();
}
private void acceptConnection() {
try {
ServerSocket server = new ServerSocket(3339);
socket = server.accept();
isLive = true;
ioStream = socket.getOutputStream();
inStream = socket.getInputStream();
state = initialState;
startReadThread();
} catch (IOException io) {
io.printStackTrace();
}
}
private void startReadThread() {
Thread readRunnable = new Thread() {
public void run() {
while (isLive) {
try {
int num = inStream.read(readBuffer);
if (num > 0) {
byte[] tempArray = new byte[num];
System.arraycopy(readBuffer, 0, tempArray, 0, num);
processBytes(tempArray);
}
sleep(100);
} catch (SocketException s) {
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException i) {
i.printStackTrace();
}
}
}
};
Thread readThread = new Thread(readRunnable);
readThread.start();
}
private void processBytes(byte[] buff) throws InterruptedException {
if (state == fileReceiveState || state == fileContentWaitState) {
//write to file
if (state == fileContentWaitState)
state = fileReceiveState;
fileSize = fileSize - buff.length;
writeToFile(buff);
if (fileSize == 0) {
state = fileReceivedState;
try {
foStream.close();
} catch (IOException io) {
io.printStackTrace();
}
System.out.println("Received " + dstFile.getName());
sendResponse(fileReceived);
fileCount--;
if (fileCount != 0) {
state = fileHeaderWaitState;
} else {
System.out.println("Finished");
state = finalState;
sendResponse("Thanks");
Thread.sleep(2000);
System.exit(0);
}
System.out.println("Received");
}
} else {
parseToUTF(buff);
}
}
private void parseToUTF(byte[] data) {
try {
String parsedMessage = new String(data, "UTF-8");
System.out.println(parsedMessage);
setResponse(parsedMessage);
} catch (UnsupportedEncodingException u) {
u.printStackTrace();
}
}
private void setResponse(String message) {
if (message.trim().equalsIgnoreCase(request) && state == initialState) {
sendResponse(respServer);
state = dirHeaderWait;
} else if (state == dirHeaderWait) {
if (createDirectory(message)) {
sendResponse(dirResponse);
state = fileHeaderWaitState;
} else {
sendResponse(dirFailedResponse);
System.out.println("Error occurred...Going to exit");
System.exit(0);
}
} else if (state == fileHeaderWaitState) {
createFile(message);
state = fileContentWaitState;
sendResponse(fileHeaderRecvd);
} else if (message.trim().equalsIgnoreCase(dirFailedResponse)) {
System.out.println("Error occurred ....");
System.exit(0);
}
}
private void sendResponse(String resp) {
try {
sendBytes(resp.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
private boolean createDirectory(String dirName) {
boolean status = false;
dir = dirName.substring(dirName.indexOf("$") + 1, dirName.indexOf("#"));
fileCount = Integer.parseInt(dirName.substring(dirName.indexOf("#") + 1, dirName.indexOf("&")));
if (new File(dir).mkdir()) {
status = true;
System.out.println("Successfully created directory " + dirName);
} else if (new File(dir).mkdirs()) {
status = true;
System.out.println("Directories were created " + dirName);
} else if (new File(dir).exists()) {
status = true;
System.out.println("Directory exists" + dirName);
} else {
System.out.println("Could not create directory " + dirName);
status = false;
}
return status;
}
private void createFile(String fileName) {
String file = fileName.substring(fileName.indexOf("&") + 1, fileName.indexOf("#"));
String lengthFile = fileName.substring(fileName.indexOf("#") + 1, fileName.indexOf("*"));
fileSize = Integer.parseInt(lengthFile);
dstFile = new File(dir + "/" + file);
try {
foStream = new FileOutputStream(dstFile);
System.out.println("Starting to receive " + dstFile.getName());
} catch (FileNotFoundException fn) {
fn.printStackTrace();
}
}
private void writeToFile(byte[] buff) {
try {
foStream.write(buff);
} catch (IOException io) {
io.printStackTrace();
}
}
private void sendBytes(byte[] dataBytes) {
synchronized (socket) {
if (ioStream != null) {
try {
ioStream.write(dataBytes);
} catch (IOException io) {
io.printStackTrace();
}
}
}
}
}
ClientMain.java and DirectoryTxr.java are the two classes under client application. ServerMain.java and DirectoryRcr.java are the two classes under Server application.
run the ClientMain.java and ServerMain.java
You can sort an array using Arrays.sort(...)
ie
String[] childFiles = file.list();
Arrays.sort(childFiles);
As I understand it, this will sort the files in natural order, based on the file name.
You can modify this by passing a custom Comparator...
Arrays.sort(childFiles, new Comparator<File>() {...}); // Fill out with your requirements...
Progress monitoring will come down to your requirements. Do you want to see only the overall progress of the number of files, the individual files, a combination of both?
What I've done in the past is pre-iterated the file list, calculated the total number of bytes to be copied and used a ProgressMonitor to display the overall bytes been copied...
Otherwise you will need to supply your own dialog. In either case, you will need use SwingUtilities.invokeLater to update them correctly.
Check out Concurrency in Swing for more details.
Network connectivity is subjective. You could simply send a special "message" to there server and wait for a response, but this only suggests that the message was sent and a recipt was received. It could just as easily fail when you start transfering the file again...
i have two threads starting at the same time in my android program for downloading some packets.
ServerIP = WifiServer;
Thread thread22 = new Thread (new CallDownloader(fileToReceive, ServerIP, WifiServer, MobileServer));
thread22.start();
ServerIP = MobileServer;
Thread thread23 = new Thread (new CallDownloader(fileToReceive, ServerIP, WifiServer, MobileServer));
thread23.start();
i want to download some files from two servers at the same time.
if i use under every thread the thread.joim();
ServerIP = WifiServer;
Thread thread22 = new Thread (new CallDownloader(fileToReceive, ServerIP, WifiServer, MobileServer));
thread22.start();
thread22.join();
ServerIP = MobileServer;
Thread thread23 = new Thread (new CallDownloader(fileToReceive, ServerIP, WifiServer, MobileServer));
thread23.start();
thread23.join();
the code is not parallel but serialized. and i don't want this. I know that join is waiting to finish the first and after continue to the other.
when i take out the thread.join(); its downloading whatever it wants and not what i want to download. and is using only the first thread and it isn't going to the second.
how i can avoid the join?
because i need thread to call the connection.
if you need more details tell me to put.
here is the main code.
Queue<String> fileparts = new LinkedList<String>();
Time = SystemClock.currentThreadTimeMillis();
long Time2 = System.currentTimeMillis();
StartTime = Time2;
int intnumbOfChunks = Integer.parseInt(numbOfChunks);
intnumbOfChunks = 250;
for (int i = 0; i < intnumbOfChunks; i++) {
fileToClaim = "";
fileToClaim = "bigbang.mp4";
fileToClaim = String.format("%s.part%06d", fileToClaim, i);
fileparts.add(fileToClaim);
}
int i=0;
while (!fileparts.isEmpty()) {
fileToReceive = fileparts.peek();
ServerIP = WifiServer;
Thread thread22 = new Thread (new CallDownloader(fileToReceive, ServerIP, WifiServer, MobileServer));
thread22.start();
thread22.start();
try {
thread22.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ServerIP = MobileServer;
Thread thread23 = new Thread (new CallDownloader(fileToReceive, ServerIP, WifiServer, MobileServer));
thread23.start();
try {
thread22.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Thread thread3 = new Thread(new Join(fileToReceive));
thread3.start();
try {
thread3.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
fileparts.remove();}
the downloader thread is
public class Downloader {
final String fileToReceive;
boolean fileDownloaded = false;
Socket clientSocket;
public Downloader(String fileToReceive) {
this.fileToReceive = fileToReceive;
}
public void download(String ServerIP) {
//Socket clientSocket = null;
try {
System.out.println("kanw connect me ti porta 3248");
clientSocket = new Socket(ServerIP, 3248);
System.out.println("egine i sundesi me ti porta 3248");
System.out.println("stelnw to " + fileToReceive);
BufferedWriter BuffWriter = new BufferedWriter(
new OutputStreamWriter(clientSocket.getOutputStream()));
BuffWriter.write(fileToReceive + "\n");
BuffWriter.flush();
System.out.println("esteila to " + fileToReceive);
InputStream is = clientSocket.getInputStream();
File root = Environment.getExternalStorageDirectory();
System.out.println("dimiourgo to fakelo vid ");
File dir = new File(root.getAbsolutePath() + "/vid/");
// File dir = new File (root.getAbsolutePath());
dir.mkdirs();
System.out.println("arxizw tin lipsei tou chunk");
byte[] longBytes = new byte[8];
readFully(clientSocket, longBytes);
ByteBuffer bb = ByteBuffer.wrap(longBytes);
long fileLength = bb.getLong();
System.out.println("apothikeusi arxeiou");
// save
#SuppressWarnings("resource")
FileOutputStream f = new FileOutputStream(new File(dir,
fileToReceive));
byte[] buffer = new byte[1024];
int len1 = 0;
int readBytes = 0;
while ((len1 = is.read(buffer)) > 0) {
f.write(buffer, 0, len1);
readBytes += len1;
}
if (readBytes == fileLength) {
this.fileDownloaded = true;
}
System.out.println("Receiving the file " + fileToReceive);
System.out.println("File Recieved");
} catch (IOException ex) {
System.out.println("Client stopped");
ex.printStackTrace();
} finally {
try {
if (clientSocket != null && !clientSocket.isClosed()) {
clientSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void readFully(Socket clientSocket, byte[] buffer)
throws IOException {
System.out.println("readfully");
InputStream inputStream = clientSocket.getInputStream();
int remaining = buffer.length;
int read = 0;
while (remaining > 0) {
read = inputStream.read(buffer, buffer.length - remaining, remaining);
if (read < 0) {
throw new IOException();
}
remaining -= read;
}
System.out.println("exit");
}
public boolean IsDownloaded() {
return this.fileDownloaded;
}
}
CallDownloader class is the thread
public void run() {
System.out.println("connecting tou "+ServerIP);
Downloader d = new Downloader(fileToReceive);
//Downloader1 d1 = new Downloader1(fileToReceive);
d.download(ServerIP);
}