Directory watch, locked files - java

I´m try to build a application which, by threads, are listening to directory for changes. Everything works fine, but there is a little bug. I´m very new to the whole threads-thing... So, this problem probably based on my ignorance...
The program can se all the changes in the picked directory, BUT, when the threads are running, i cant modify the files inside the directory... Those are locked in the process...
I will be very happy if someone perhaps can give me some tips in how i can solve this.
Thank you in advance
DirWatch
public class DirWatch implements Runnable{
Path dirPath;
private boolean run = true;
private boolean created = false;
private boolean modified = false;
private boolean compressed = false;
private boolean isJSON = false;
/**
*
* #param path
* #throws IOException
*/
public void setWatchPath(String path) throws IOException {
dirPath = Paths.get(path);
try {
Boolean isFolder = (Boolean) Files.getAttribute(dirPath, "basic:isDirectory", new LinkOption[]{NOFOLLOW_LINKS});
if (!isFolder) {
throw new IllegalArgumentException("Path: " + dirPath + " is not a folder");
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
System.out.println("Watching path: " + path);
}
public void setDirPathWatchRun(boolean run){
this.run = run;
}
public boolean isCreated() {
return created;
}
public void setCreated(boolean created) {
this.created = created;
}
public boolean isModified() {
return modified;
}
public void setModified(boolean modified) {
this.modified = modified;
}
public boolean isCompressed() {
return compressed;
}
public void setCompressed(boolean compressed) {
this.compressed = compressed;
}
public boolean isJSON() {
return isJSON;
}
public void setJSON(boolean JSON) {
isJSON = JSON;
}
private void checkFileType(String fileName){
String extension = fileName.substring(fileName.length() - 4);
if(extension.equalsIgnoreCase(FILE_TYPE.TAR.getFileType())){
setCompressed(true);
System.out.println(extension);
}
if(extension.equalsIgnoreCase(".json")){
setJSON(true);
}
}
#Override
public void run() {
FileSystem fs = dirPath.getFileSystem ();
try(WatchService service = fs.newWatchService()) {
dirPath.register(service, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
WatchKey key = null;
while(run) {
key = service.take();
WatchEvent.Kind<?> kind = null;
for(WatchEvent<?> watchEvent : key.pollEvents()) {
kind = watchEvent.kind();
if (OVERFLOW == kind) {
System.out.println("OVERFLOW");
continue;
} else if (ENTRY_CREATE == kind) {
System.out.println("New path created: " + watchEvent.context().toString());
setCreated(true);
checkFileType(watchEvent.context().toString());
} else if (ENTRY_DELETE == kind){
System.out.println("Path deleted: " + watchEvent.context().toString());
setModified(true);
checkFileType(watchEvent.context().toString());
} else if (ENTRY_MODIFY == kind) {
System.out.println("Path modified: " + watchEvent.context().toString());
setModified(true);
checkFileType(watchEvent.context().toString());
}
}
if(!key.reset()) {
break;
}
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Method Watch in another class
private void watch() throws IOException {
watchJSON = new DirWatch();
watchTAR = new DirWatch();
watchTAR.setWatchPath(serverArgs.getCompressedPath());
watchJSON.setWatchPath(serverArgs.getExtractedPath());
Runnable checkSourceActions = new Runnable() {
#Override
public void run() {
while(true) {
if (watchJSON.isCreated() || (watchJSON.isModified())) {
server();
}
if(watchTAR.isCreated() || (watchTAR.isModified())) {
extractFiles(fileHandler);
createManifest(fileHandler);
server();
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread thread1 = new Thread(watchJSON);
Thread thread3 = new Thread(watchTAR);
Thread thread2 = new Thread(checkSourceActions);
thread1.start();
thread2.start();
thread3.start();
}
When I try to change the file while the program is running

Related

Why isn't my client socket inputstream receiving message sent from server socket outputstream

This is the SocketServer code that generates a server thread
public class ProcessorCorresponder {
protected final static Logger logger = LogManager.getLogger( ProcessorCorresponder.class );
private static int port = Integer.parseInt(PropertiesLoader.getProperty("appserver.port") == null ? "666" : PropertiesLoader.getProperty("appserver.port"));
private static int maxConnections = Integer.parseInt(PropertiesLoader.getProperty("appserver.maxconnections") == null ? "666" : PropertiesLoader.getProperty("appserver.maxconnections"));
public static void main(String[] args) {
logger.info("Starting server .. "
+ "[port->" + port
+ ",databaseName->" + databaseName + "]");
try (ServerSocket listener = new ServerSocket();) {
listener.setReuseAddress(true);
listener.bind(new InetSocketAddress(port));
Socket server;
int i = 0;
while((i++ < maxConnections) || (maxConnections == 0)) {
server = listener.accept();
logger.debug(
"New Thread listening on " + server.getLocalAddress().toString() + ":" + server.getLocalPort()
+ ", initiated from IP => " + server.getInetAddress().toString() + ":" + server.getPort()
);
MySocketServer socSrv = new MySocketServer (server);
Thread t = new Thread( socSrv );
t.start();
}
} catch (Exception ex) {
logger.error("Error in ProcessorInterface", ex);
}
}
}
Server code: This is a thread to handle one connection, there is a program that monitors a serversocket and spins off request threads as needed.
public class MySocketServer implements Runnable {
protected final static Logger logger = LogManager.getLogger(MySocketServer.class);
private final Socket server;
// because we are using threads, we must make this volatile, or the class will
// never exit.
private volatile boolean shouldContinue = true;
private StringBuffer buffHeartbeatMessage = new StringBuffer().append((char) 0).append((char) 0).append((char) 0)
.append((char) 0).append((char) 0).append((char) 0);
private Heartbeat heartbeat = new Heartbeat(/* 60 */3000, buffHeartbeatMessage.toString());
public MySocketServer(Socket server) {
this.server = server;
}
#Override
public void run() {
try (BufferedReader in = new BufferedReader(new InputStreamReader(this.server.getInputStream()));
BufferedOutputStream out = new HeartbeatBufferedOutputStream(this.server.getOutputStream(),
heartbeat)) {
final StreamListener listener = new StreamListener(in);
listener.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
if (event.getID() == ActionEvent.ACTION_PERFORMED) {
if (event.getActionCommand().equals(StreamListener.ERROR)) {
logger.error("Problem listening to stream.");
listener.setShouldContinue(false);
stopRunning();
} else {
String messageIn = event.getActionCommand();
if (messageIn == null) { // End of Stream;
stopRunning();
} else { // hey, we can do what we were meant for
logger.debug("Request received from client");
// doing stuff here
...
// done doing stuff
logger.debug("Sending Client Response");
try {
sendResponse(opResponse, out);
} catch (Exception ex) {
logger.error("Error sending response to OP.", ex);
}
}
}
}
}
});
listener.start();
while (shouldContinue) {
// loop here until shouldContinue = false;
// this should be set to false in the ActionListener above
}
heartbeat.setShouldStop(true);
return;
} catch (Exception ex) {
logger.error("Error in ESPSocketServer", ex);
return;
}
}
private void stopRunning() {
shouldContinue = false;
}
private void sendResponse(ClientResponse opResponse, BufferedOutputStream out) throws Exception {
logger.debug("Before write");
out.write(opResponse.getResponse().getBytes());
logger.debug("After write. Before flush");
out.flush();
logger.debug("After flush");
// this log message is in my logs, so I know the message was sent
}
}
My StreamListener class.
public class StreamListener extends Thread {
protected final static Logger logger = LogManager.getLogger(StreamListener.class);
public final static String ERROR = "ERROR";
private BufferedReader reader = null;
private List<ActionListener> actionListeners = new ArrayList<>();
private boolean shouldContinue = true;
public StreamListener(BufferedReader reader) {
this.reader = reader;
}
#Override
public void run() {
while (shouldContinue) {
String message;
try {
// client blocks here and never receives message
message = reader.readLine();
ActionEvent event = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, message);
fireActionPerformed(event);
} catch (IOException e) {
e.printStackTrace();
ActionEvent event = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, ERROR);
fireActionPerformed(event);
}
}
}
public void setShouldContinue(boolean shouldContinue) {
this.shouldContinue = shouldContinue;
}
public boolean getShouldContinue() {
return shouldContinue;
}
public boolean addActionListener(ActionListener listener) {
return actionListeners.add(listener);
}
public boolean removeActionListener(ActionListener listener) {
return actionListeners.remove(listener);
}
private void fireActionPerformed(ActionEvent event) {
for (ActionListener listener : actionListeners) {
listener.actionPerformed(event);
}
}
}
My Heartbeat class
public class Heartbeat extends Thread {
private BufferedOutputStream bos = null;
private int beatDelayMS = 0;
private String message = null;
private boolean shouldStop = false;
public Heartbeat(int beatDelayMS, String message) {
this.beatDelayMS = beatDelayMS;
this.message = message;
setDaemon(true);
}
#Override
public void run() {
if (bos == null) { return; }
while(!shouldStop) {
try {
sleep(beatDelayMS);
try {
bos.write(message.getBytes());
bos.flush();
} catch (IOException ex) {
// fall thru
}
} catch (InterruptedException ex) {
if (shouldStop) {
return;
}
}
}
}
public void setBufferedOutputStream(BufferedOutputStream bos) {
this.bos = bos;
}
public BufferedOutputStream getBufferedOutputStream() {
return bos;
}
public void setShouldStop(boolean shouldStop) {
this.shouldStop = shouldStop;
}
public boolean getShouldStop() {
return shouldStop;
}
}
My HeartbeatBufferedOutputStream
public class HeartbeatBufferedOutputStream extends BufferedOutputStream {
private Heartbeat heartbeat = null;
public HeartbeatBufferedOutputStream(OutputStream out, Heartbeat heartbeat) {
super(out);
this.heartbeat = heartbeat;
this.heartbeat.setBufferedOutputStream(this);
heartbeat.start();
}
#Override
public synchronized void flush() throws IOException {
super.flush();
heartbeat.interrupt();
}
}
And finally here is the "Client" class
public class Mockup extends Thread {
protected final static Logger logger = LogManager.getLogger(Mockup.class);
// because we are using threads, we must make this volatile, or the class will
// never exit.
private volatile boolean shouldContinue = true;
public static void main(String[] args) {
new Mockup().start();
}
#Override
public void run() {
try (Socket socket = new Socket("localhost", 16100);
BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));) {
final StreamListener listener = new StreamListener(in);
listener.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
if (event.getID() == ActionEvent.ACTION_PERFORMED) {
if (event.getActionCommand().equals(StreamListener.ERROR)) {
logger.error("Problem listening to stream.");
listener.setShouldContinue(false);
stopRunning();
} else {
String messageIn = event.getActionCommand();
if (messageIn == null) { // End of Stream;
stopRunning();
} else { // hey, we can do what we were meant for
// convert the messageIn to an OrderPower request, this parses the information
logger.info("Received message from server. [" + messageIn + "].");
}
}
}
}
});
listener.start();
StringBuffer buff = new StringBuffer("Some message to send to server");
logger.info("Sending message to server [" + buff.toString() + "]");
out.write(buff.toString().getBytes());
out.flush();
boolean started = false;
while (shouldContinue) {
if (!started) {
logger.debug("In loop");
started = true;
}
// loop here until shouldContinue = false;
// this should be set to false in the ActionListener above
}
logger.info("Exiting Mockup");
return;
} catch (Exception ex) {
logger.error("Error running MockupRunner", ex);
}
}
private void stopRunning() {
shouldContinue = false;
}
}
I have confirmed from logging messages that the Server sends a message to the BufferedOutputStream, and is flushed, but the Client logs indicate that it is blocked on the reader.readLine() and never gets the message.
You are reading lines but you are never writing lines. Add a line terminator to what you send.

Multiple Thread Writer with Single Thread Reader using PipedOutputStream and PipedInputStream

I have a Multiple Writer Threads with Single Reader Thread model.
The ThreadMultipleDateReceiver class is designed to read from multiple Threads.
public class ThreadMultipleDateReceiver extends Thread {
private static final int MAX_CLIENT_THREADS = 4;
private byte[] incomingBytes;
private volatile boolean isRunning;
private volatile List<ThreadStreamDateWriter> lThrdDate;
private static PipedInputStream pipedInputStream;
public ThreadMultipleDateReceiver() {
lThrdDate = Collections.synchronizedList(new ArrayList<>(MAX_CLIENT_THREADS));
pipedInputStream = new PipedInputStream();
System.out.println("ThreadMultipleDateReceiver Created");
}
#Override public void run() {
isRunning = true;
while (isRunning) {
if (!lThrdDate.isEmpty()) {
System.out.println("ThreadMultipleDateReceiver has:" + lThrdDate.size());
for (int i = lThrdDate.size(); i > 0; i--) {
if (lThrdDate.get(i - 1).getState() == Thread.State.TERMINATED) {
lThrdDate.remove(i - 1);
} else {
System.out.println("I ThreadMultipleDateReceiver have:" + lThrdDate.get(i - 1).getNameDateWriter());
}
}
incomingBytes = new byte[1024];
try {
String str = "";
int iRd;
System.out.println("ThreadMultipleDateReceiver waiting:" + str);
while ((iRd = pipedInputStream.read(incomingBytes)) != -1) {
if (iRd > 0) {
str += new String(incomingBytes);
}
}
System.out.println("ThreadMultipleDateReceiver Received:\n\t:" + str);
} catch (IOException e) { }
} else {
System.out.println("ThreadMultipleDateReceiver Empty");
}
}
emptyDateWriters();
}
public void addDateWriter(ThreadStreamDateWriter threadDateWriter) {
if (lThrdDate.size() < MAX_CLIENT_THREADS) {
lThrdDate.add(threadDateWriter);
}
}
private void emptyDateWriters() {
if (!lThrdDate.isEmpty()) {
for (int i = lThrdDate.size(); i > 0; i--) {
ThreadStreamDateWriter threadDateWriter = lThrdDate.get(i - 1);
threadDateWriter.stopThread();
lThrdDate.remove(i - 1);
}
}
}
public PipedInputStream getPipedInputStream() {
return pipedInputStream;
}
public void stopThread() {
isRunning = false;
}
}
And the single Writer Thread
public class ThreadStreamDateWriter extends Thread {
String Self;
private byte[] outgoingBytes;
private volatile boolean isRunning;
private static PipedOutputStream pipedOutputStream;
ThreadStreamDateWriter(String name, PipedInputStream snk) {
Self = name;
pipedOutputStream = new PipedOutputStream();
try {
pipedOutputStream.connect(snk);
} catch (IOException e) { }
}
#Override public void run() {
isRunning = true;
while (isRunning) {
try {
outgoingBytes = getInfo().getBytes();
System.out.println("ThreadStreamDateWriter -> write to pipedOutputStream:" + new String(outgoingBytes));
pipedOutputStream.write(outgoingBytes);
System.out.println("ThreadStreamDateWriter -> wrote:" + new String(outgoingBytes));
try { Thread.sleep(4000); } catch (InterruptedException ex) { }
} catch (IOException | NegativeArraySizeException | IndexOutOfBoundsException e) {
isRunning = false;
}
}
}
String getInfo() {
String sDtTm = new SimpleDateFormat("yyyyMMdd-hhmmss").format(Calendar.getInstance().getTime());
return Self + " -> " + sDtTm;
}
public void stopThread() {
isRunning = false;
}
public String getNameDateWriter() {
return Self;
}
}
How launch (I'm using Netbeans)?
ThreadMultipleDateReceiver thrdMDateReceiver = null;
ThreadStreamDateWriter thrdSDateWriter0 = null;
ThreadStreamDateWriter thrdSDateWriter1 = null;
private void jtbDateExchangerActionPerformed(java.awt.event.ActionEvent evt) {
if (jtbDateExchanger.isSelected()) {
if (thrdMDateReceiver == null) {
thrdMDateReceiver = new ThreadMultipleDateReceiver();
thrdMDateReceiver.start();
}
if (thrdSDateWriter0 == null) {
thrdSDateWriter0 = new ThreadStreamDateWriter("-0-", thrdMDateReceiver.getPipedInputStream());
thrdSDateWriter0.start();
thrdMDateReceiver.addDateWriter(thrdSDateWriter0);
}
if (thrdSDateWriter1 == null) {
thrdSDateWriter1 = new ThreadStreamDateWriter("-1-", thrdMDateReceiver.getPipedInputStream());
thrdSDateWriter1.start();
thrdMDateReceiver.addDateWriter(thrdSDateWriter1);
}
} else {
if (thrdMDateReceiver != null) {
thrdMDateReceiver.stopThread();
}
}
}
The OUTPUT
run:
ThreadMultipleDateReceiver Created
ThreadMultipleDateReceiver Empty
ThreadMultipleDateReceiver Empty
ThreadMultipleDateReceiver Empty
.....
ThreadMultipleDateReceiver Empty
ThreadMultipleDateReceiver Empty
ThreadMultipleDateReceiver Empty
ThreadMultipleDateReceiver has:1
I ThreadMultipleDateReceiver have:-0-
ThreadMultipleDateReceiver waiting:
ThreadStreamDateWriter -> write to pipedOutputStream:-0- -> 20170608-090003
ThreadStreamDateWriter -> write to pipedOutputStream:-1- -> 20170608-090003
BUILD SUCCESSFUL (total time: 1 minute 3 seconds)
The ThreadMultipleDateReceiver is blocked, and is not printing:
ThreadMultipleDateReceiver Received:
-1- -> 20170608-090003
or
ThreadMultipleDateReceiver Received:
-0- -> 20170608-090003
How solve it?
looks like your piped output stream is static, so every time you construct a ThreadStreamDateWriter, you are stepping on the old value of piped output stream.
try making this an instance variable and pass it into the constructor. so you only have one of them.
edit 1: i made the pipes instance variables and added some printouts. seems to be running longer now (see below):
edit 2: you second pipedOutputStream.connect(snk); is throwing. you can only connect one thing at a time.
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
public class So44438086 {
public static class ThreadMultipleDateReceiver extends Thread {
private static final int MAX_CLIENT_THREADS=4;
private byte[] incomingBytes;
private volatile boolean isRunning;
private volatile List<ThreadStreamDateWriter> lThrdDate;
private /*static*/ PipedInputStream pipedInputStream;
public ThreadMultipleDateReceiver() {
lThrdDate=Collections.synchronizedList(new ArrayList<>(MAX_CLIENT_THREADS));
pipedInputStream=new PipedInputStream();
System.out.println("ctor setting pipedInputStream to: "+pipedInputStream);
System.out.println("ThreadMultipleDateReceiver Created");
}
#Override public void run() {
isRunning=true;
while(isRunning) {
if(!lThrdDate.isEmpty()) {
System.out.println("ThreadMultipleDateReceiver has:"+lThrdDate.size());
for(int i=lThrdDate.size();i>0;i--) {
if(lThrdDate.get(i-1).getState()==Thread.State.TERMINATED) {
lThrdDate.remove(i-1);
} else {
System.out.println("I ThreadMultipleDateReceiver have:"+lThrdDate.get(i-1).getNameDateWriter());
}
}
incomingBytes=new byte[1024];
try {
String str="";
int iRd;
System.out.println("ThreadMultipleDateReceiver waiting:"+str);
System.out.println("reading: "+pipedInputStream);
while((iRd=pipedInputStream.read(incomingBytes))!=-1) {
if(iRd>0) {
str+=new String(incomingBytes);
}
}
System.out.println("ThreadMultipleDateReceiver Received:\n\t:"+str);
} catch(IOException e) {}
} else {
System.out.println("ThreadMultipleDateReceiver Empty");
}
}
emptyDateWriters();
}
public void addDateWriter(ThreadStreamDateWriter threadDateWriter) {
if(lThrdDate.size()<MAX_CLIENT_THREADS) {
lThrdDate.add(threadDateWriter);
}
}
private void emptyDateWriters() {
if(!lThrdDate.isEmpty()) {
for(int i=lThrdDate.size();i>0;i--) {
ThreadStreamDateWriter threadDateWriter=lThrdDate.get(i-1);
threadDateWriter.stopThread();
lThrdDate.remove(i-1);
}
}
}
public PipedInputStream getPipedInputStream() {
return pipedInputStream;
}
public void stopThread() {
isRunning=false;
}
}
public static class ThreadStreamDateWriter extends Thread {
String Self;
private byte[] outgoingBytes;
private volatile boolean isRunning;
private /*static*/ PipedOutputStream pipedOutputStream;
ThreadStreamDateWriter(String name,PipedInputStream snk) {
Self=name;
pipedOutputStream=new PipedOutputStream();
System.out.println("ctor setting pipedOutputStream to: "+pipedOutputStream);
try {
pipedOutputStream.connect(snk);
System.out.println(pipedOutputStream+" connectd to: "+snk);
} catch(IOException e) {}
}
#Override public void run() {
isRunning=true;
while(isRunning) {
try {
outgoingBytes=getInfo().getBytes();
System.out.println("ThreadStreamDateWriter -> write to pipedOutputStream:"+new String(outgoingBytes));
System.out.println("writing to: "+pipedOutputStream);
pipedOutputStream.write(outgoingBytes);
System.out.println("ThreadStreamDateWriter -> wrote:"+new String(outgoingBytes));
try {
Thread.sleep(4000);
} catch(InterruptedException ex) {}
} catch(IOException|NegativeArraySizeException|IndexOutOfBoundsException e) {
isRunning=false;
}
}
}
String getInfo() {
String sDtTm=new SimpleDateFormat("yyyyMMdd-hhmmss").format(Calendar.getInstance().getTime());
return Self+" -> "+sDtTm;
}
public void stopThread() {
isRunning=false;
}
public String getNameDateWriter() {
return Self;
}
}
private void foo() {
if(thrdMDateReceiver==null) {
thrdMDateReceiver=new ThreadMultipleDateReceiver();
thrdMDateReceiver.start();
}
if(thrdSDateWriter0==null) {
thrdSDateWriter0=new ThreadStreamDateWriter("-0-",thrdMDateReceiver.getPipedInputStream());
thrdSDateWriter0.start();
thrdMDateReceiver.addDateWriter(thrdSDateWriter0);
}
if(thrdSDateWriter1==null) {
thrdSDateWriter1=new ThreadStreamDateWriter("-1-",thrdMDateReceiver.getPipedInputStream());
thrdSDateWriter1.start();
thrdMDateReceiver.addDateWriter(thrdSDateWriter1);
}
}
void run() throws InterruptedException {
System.out.println(("running"));
foo();
System.out.println(("sleeping"));
Thread.sleep(10000);
System.out.println(("stopping"));
if(thrdMDateReceiver!=null) {
thrdMDateReceiver.stopThread();
}
}
public static void main(String[] args) throws InterruptedException {
new So44438086().run();
}
ThreadMultipleDateReceiver thrdMDateReceiver=null;
ThreadStreamDateWriter thrdSDateWriter0=null;
ThreadStreamDateWriter thrdSDateWriter1=null;
}

How to return a boolean from a runnable or similar?

This is my code by now:
public class discoverRunnable implements Runnable{
InetAddress address = null;
boolean discovered;
public discoverRunnable(InetAddress address){
this.address = address;
boolean discovered = false;
}
#Override
public void run(){
//some crazy stuff
//may set discovered = true
}
}
How can I return the Value of "discovered" to use it within a Thread now?
It should work on a PC without using Android archives.
You can use Callable instead of Runnable
public class DiscoverRunnable implements Callable<Boolean> {
InetAddress address = null;
boolean discovered;
public DiscoverRunnable(InetAddress address){
this.address = address;
boolean discovered = false;
}
#Override
public Boolean call(){
//some crazy stuff
//may set discovered = true
return discovered;
}
}
I couldn't get a member variable to be accessible after a thread finishes a Runnable. I can get the Callable to return future with value, without a problem. So, I would agree, always use a Callable in these cases where you need a value from a finished runnable.
import java.util.*;
import java.util.concurrent.*;
class Main
{
public static void main(String[] args) {
ExecutorService ex = Executors.newFixedThreadPool(4);
Runnable r1 = new Runnable() {
private boolean flag = false;
#Override
public void run() {
try {
System.out.println("Thread: " + Thread.currentThread().getName());
Thread.sleep((long)(Math.random() * 1000));
flag = true;
} catch (InterruptedException ie) {
// do nothing
}
}
public boolean getFlag() {
return flag;
}
};
Callable<Boolean> c1 = new Callable<Boolean>() {
private boolean flag = false;
#Override
public Boolean call() {
try {
System.out.println("Thread: " + Thread.currentThread().getName());
Thread.sleep((long)(Math.random() * 1000));
flag = true;
} catch (InterruptedException ie) {
// do nothing
}
return getFlag();
}
public boolean getFlag() {
return flag;
}
};
ex.submit(r1);
Future<Boolean> f = ex.submit(c1);
ex.shutdown();
if (c1 != null) {
try {
System.out.println("Callable future-get: "
+ f.get()); //WORKS!: shows boolean value returned from future
System.out.println("Callable direct-var: "
+ ((Callable<Boolean>) c1).flag); //FAIL
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
if (r1 != null) System.out.println("Runnable result: "
+ ((Runnable) r1).flag); //FAIL
}
}
In Kotlin
fun checkWebUrl(): Boolean {
val futureResult = FutureTask<Boolean>(Callable<Boolean> {
webview_entry.url.startsWith("https://developer.android.com/reference/java/util/concurrent/FutureTask")
})
return futureResult.get()
}

Communicate with Process in Java through different Threads

I have a programm which communicates with MPlayer through Input and Outputstream of the Process.
It works quite good, but I can only communicate with the Process in the Main Thread, and not in any other. Is there a posibility to get arround that?
synchronized-Methods or blocks didn't brought a solution
private String getProperty(String property) {
System.out.println("CurThread: " + Thread.currentThread().getName());
String cmd = "";
cmd = String.format("pausing_keep_force get_property %s", property);
mplayerIn.println(cmd);
mplayerIn.flush();
String rightAnswer = String.format("ANS_%s=", property);
String errorAnswer = "ANS_ERROR";
String answer;
String value = "";
try {
while ((answer = mplayerOut.readLine()) != null) {
if (answer.startsWith(rightAnswer)) {
value = answer.substring(rightAnswer.length());
break;
}
if (answer.startsWith(errorAnswer)) {
value = "";
break;
}
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("Property Error:" + property);
}
return value;
}
edit: ProcessCommunicator
public class ProcessCommunicator extends Thread {
private String cmd;
private BufferedReader is;
private PrintStream os;
List<MessageReceivedListener> listeners;
public boolean addListener(MessageReceivedListener listener) {
return listeners.add(listener);
}
public boolean removeListener(MessageReceivedListener listener) {
if (listeners.contains(listener)) {
return listeners.remove(listener);
}
return false;
}
public ProcessCommunicator(String cmd) {
this.cmd = cmd;
listeners = new ArrayList<>();
}
public void write(String cmd) {
System.out.println(cmd);
os.println(cmd);
os.flush();
}
public void fireEvent(String msg) {
for (MessageReceivedListener listener : listeners) {
listener.received(msg);
}
}
Process p;
#Override
public void run() {
try {
p = Runtime.getRuntime().exec(cmd);
is = new BufferedReader(new InputStreamReader(p.getInputStream()));
os = new PrintStream(p.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
String line;
try {
while ((line = is.readLine()) != null) {
System.err.println(line);
fireEvent(line.trim());
}
} catch (IOException e) {
e.printStackTrace();
}
}
boolean waitForProperty;
String propertyResult;
public String readProperty(String property) {
final String rightAnswer = String.format("ANS_%s=", property);
propertyResult = "";
MessageReceivedListener messageListener = new MessageReceivedListener() {
#Override
public void received(String s) {
if (s.startsWith(rightAnswer)) {
waitForProperty = false;
propertyResult = s.substring(rightAnswer.length());
} else if (s.startsWith("ANS_ERROR")) {
waitForProperty = false;
propertyResult = "";
}
}
};
addListener(messageListener);
waitForProperty = true;
write(String.format("pausing_keep_force get_property %s", property));
int i = 5;
while (waitForProperty && i > 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.print(".");
i--;
}
System.out.println("END");
removeListener(messageListener);
return propertyResult;
}
}

Bluetooth OBEX FTP server on Android 2.X

I'm using the Bluecove library to devolep an Obex ftp client/server on android 2.X.
So far i managed to connect as a client from android to a pc.
I tried the bluecove example code for a server but doesn't work.
Is it possible to develop a server on android 2.X. Does any one have the code to do it.
Thank you.
public class OBEXServer implements Runnable {
private SessionNotifier serverConnection;
private boolean isStoped = false;
private boolean isRunning = false;
public final UUID OBEX_OBJECT_PUSH = new UUID(0x1105);
public static final String SERVER_NAME = "OBEX Object Push";
private UserInteraction interaction;
private OBEXServer(UserInteraction interaction) {
this.interaction = interaction;
}
public static OBEXServer startServer(UserInteraction interaction) {
OBEXServer srv = new OBEXServer(interaction);
Thread thread = new Thread(srv);
thread.start();
while (!srv.isRunning && !srv.isStoped) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new Error(e);
}
}
if (!srv.isRunning) {
throw new Error("Can't start server");
}
return srv;
}
/*
* (non-Javadoc)
*
* #see java.lang.Runnable#run()
*/
public void run() {
isStoped = false;
LocalDevice localDevice;
try {
localDevice = LocalDevice.getLocalDevice();
if (!localDevice.setDiscoverable(DiscoveryAgent.GIAC)) {
Logger.error("Fail to set LocalDevice Discoverable");
}
serverConnection = (SessionNotifier) Connector.open("btgoep://localhost:" + OBEX_OBJECT_PUSH + ";name="
+ SERVER_NAME);
} catch (Throwable e) {
Logger.error("OBEX Server start error", e);
isStoped = true;
return;
}
try {
ServiceRecord record = localDevice.getRecord(serverConnection);
String url = record.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
Logger.debug("BT server url: " + url);
final int OBJECT_TRANSFER_SERVICE = 0x100000;
try {
record.setDeviceServiceClasses(OBJECT_TRANSFER_SERVICE);
} catch (Throwable e) {
Logger.debug("setDeviceServiceClasses", e);
}
DataElement bluetoothProfileDescriptorList = new DataElement(DataElement.DATSEQ);
DataElement obbexPushProfileDescriptor = new DataElement(DataElement.DATSEQ);
obbexPushProfileDescriptor.addElement(new DataElement(DataElement.UUID, OBEX_OBJECT_PUSH));
obbexPushProfileDescriptor.addElement(new DataElement(DataElement.U_INT_2, 0x100));
bluetoothProfileDescriptorList.addElement(obbexPushProfileDescriptor);
record.setAttributeValue(0x0009, bluetoothProfileDescriptorList);
final short ATTR_SUPPORTED_FORMAT_LIST_LIST = 0x0303;
DataElement supportedFormatList = new DataElement(DataElement.DATSEQ);
// any type of object.
supportedFormatList.addElement(new DataElement(DataElement.U_INT_1, 0xFF));
record.setAttributeValue(ATTR_SUPPORTED_FORMAT_LIST_LIST, supportedFormatList);
final short UUID_PUBLICBROWSE_GROUP = 0x1002;
final short ATTR_BROWSE_GRP_LIST = 0x0005;
DataElement browseClassIDList = new DataElement(DataElement.DATSEQ);
UUID browseClassUUID = new UUID(UUID_PUBLICBROWSE_GROUP);
browseClassIDList.addElement(new DataElement(DataElement.UUID, browseClassUUID));
record.setAttributeValue(ATTR_BROWSE_GRP_LIST, browseClassIDList);
localDevice.updateRecord(record);
} catch (Throwable e) {
Logger.error("Updating SDP", e);
}
try {
int errorCount = 0;
int count = 0;
isRunning = true;
while (!isStoped) {
RequestHandler handler = new RequestHandler();
try {
count++;
Logger.debug("Accepting OBEX connections");
handler.connectionAccepted(serverConnection.acceptAndOpen(handler));
} catch (InterruptedIOException e) {
isStoped = true;
break;
} catch (Throwable e) {
if ("Stack closed".equals(e.getMessage())) {
isStoped = true;
}
if (isStoped) {
return;
}
errorCount++;
Logger.error("acceptAndOpen ", e);
continue;
}
errorCount = 0;
}
} finally {
close();
Logger.debug("OBEX Server finished!");
isRunning = false;
}
}
public void close() {
isStoped = true;
try {
if (serverConnection != null) {
serverConnection.close();
}
Logger.debug("OBEX ServerConnection closed");
} catch (Throwable e) {
Logger.error("OBEX Server stop error", e);
}
}
private static File homePath() {
String path = "bluetooth";
boolean isWindows = false;
String sysName = System.getProperty("os.name");
if (sysName != null) {
sysName = sysName.toLowerCase();
if (sysName.indexOf("windows") != -1) {
isWindows = true;
path = "My Documents";
}
}
File dir;
try {
dir = new File(System.getProperty("user.home"), path);
if (!dir.exists()) {
if (!dir.mkdirs()) {
throw new SecurityException();
}
}
} catch (SecurityException e) {
dir = new File(new File(System.getProperty("java.io.tmpdir"), System.getProperty("user.name")), path);
}
if (isWindows) {
dir = new File(dir, "Bluetooth Exchange Folder");
}
if (!dir.exists()) {
if (!dir.mkdirs()) {
return null;
}
} else if (!dir.isDirectory()) {
dir.delete();
if (!dir.mkdirs()) {
return null;
}
}
return dir;
}
private void showStatus(final String message) {
interaction.showStatus(message);
}
private class RequestHandler extends ServerRequestHandler {
Timer notConnectedTimer = new Timer();
boolean isConnected = false;
boolean receivedOk = false;
Connection cconn;
void connectionAccepted(Connection cconn) {
Logger.debug("Received OBEX connection");
showStatus("Client connected");
this.cconn = cconn;
if (!isConnected) {
notConnectedTimer.schedule(new TimerTask() {
public void run() {
notConnectedClose();
}
}, 1000 * 30);
}
}
void notConnectedClose() {
if (!isConnected) {
Logger.debug("OBEX connection timeout");
try {
cconn.close();
} catch (IOException e) {
}
if (!receivedOk) {
showStatus("Disconnected");
}
}
}
public int onConnect(HeaderSet request, HeaderSet reply) {
isConnected = true;
notConnectedTimer.cancel();
Logger.debug("OBEX onConnect");
return ResponseCodes.OBEX_HTTP_OK;
}
public void onDisconnect(HeaderSet request, HeaderSet reply) {
Logger.debug("OBEX onDisconnect");
if (!receivedOk) {
showStatus("Disconnected");
}
}
public int onSetPath(HeaderSet request, HeaderSet reply, boolean backup, boolean create) {
Logger.debug("OBEX onSetPath");
return super.onSetPath(request, reply, backup, create);
}
public int onDelete(HeaderSet request, HeaderSet reply) {
Logger.debug("OBEX onDelete");
return super.onDelete(request, reply);
}
public int onPut(Operation op) {
Logger.debug("OBEX onPut");
try {
HeaderSet hs = op.getReceivedHeaders();
String name = (String) hs.getHeader(HeaderSet.NAME);
if (name != null) {
Logger.debug("name:" + name);
showStatus("Receiving " + name);
} else {
name = "xxx.xx";
showStatus("Receiving file");
}
Long len = (Long) hs.getHeader(HeaderSet.LENGTH);
if (len != null) {
Logger.debug("file lenght:" + len);
interaction.setProgressValue(0);
interaction.setProgressMaximum(len.intValue());
}
File f = new File(homePath(), name);
FileOutputStream out = new FileOutputStream(f);
InputStream is = op.openInputStream();
int received = 0;
while (!isStoped) {
int data = is.read();
if (data == -1) {
Logger.debug("EOS received");
break;
}
out.write(data);
received++;
if ((len != null) && (received % 100 == 0)) {
interaction.setProgressValue(received);
}
}
op.close();
out.close();
Logger.debug("file saved:" + f.getAbsolutePath());
showStatus("Received " + name);
receivedOk = true;
return ResponseCodes.OBEX_HTTP_OK;
} catch (IOException e) {
Logger.error("OBEX Server onPut error", e);
return ResponseCodes.OBEX_HTTP_UNAVAILABLE;
} finally {
Logger.debug("OBEX onPut ends");
interaction.setProgressDone();
}
}
public int onGet(Operation op) {
Logger.debug("OBEX onGet");
try {
HeaderSet hs = op.getReceivedHeaders();
String name = (String) hs.getHeader(HeaderSet.NAME);
return ResponseCodes.OBEX_HTTP_NOT_IMPLEMENTED;
} catch (IOException e) {
Logger.error("OBEX Server onGet error", e);
return ResponseCodes.OBEX_HTTP_UNAVAILABLE;
} finally {
Logger.debug("OBEX onGet ends");
}
}
public void onAuthenticationFailure(byte[] userName) {
Logger.debug("OBEX AuthFailure " + new String(userName));
}
}
}

Categories

Resources