diff options
Diffstat (limited to 'src/main/java/org/gnunet/util/Helper.java')
-rw-r--r-- | src/main/java/org/gnunet/util/Helper.java | 35 |
1 files changed, 30 insertions, 5 deletions
diff --git a/src/main/java/org/gnunet/util/Helper.java b/src/main/java/org/gnunet/util/Helper.java index e8c1723..46451b0 100644 --- a/src/main/java/org/gnunet/util/Helper.java +++ b/src/main/java/org/gnunet/util/Helper.java | |||
@@ -6,7 +6,10 @@ import org.gnunet.construct.MessageLoader; | |||
6 | import org.gnunet.construct.ProtocolViolationException; | 6 | import org.gnunet.construct.ProtocolViolationException; |
7 | import org.gnunet.mq.Envelope; | 7 | import org.gnunet.mq.Envelope; |
8 | import org.gnunet.mq.MessageQueue; | 8 | import org.gnunet.mq.MessageQueue; |
9 | import org.slf4j.Logger; | ||
10 | import org.slf4j.LoggerFactory; | ||
9 | 11 | ||
12 | import java.io.File; | ||
10 | import java.io.IOError; | 13 | import java.io.IOError; |
11 | import java.io.IOException; | 14 | import java.io.IOException; |
12 | import java.nio.Buffer; | 15 | import java.nio.Buffer; |
@@ -18,17 +21,20 @@ import java.util.LinkedList; | |||
18 | import java.util.List; | 21 | import java.util.List; |
19 | 22 | ||
20 | /** | 23 | /** |
21 | * Process that we can communicate with standard GNUnet messages over stdin/stdout. | 24 | * Process that we can communicate to with standard GNUnet messages over stdin/stdout. |
22 | */ | 25 | */ |
23 | public class Helper extends MessageQueue { | 26 | public class Helper extends MessageQueue { |
27 | private static final Logger logger = LoggerFactory | ||
28 | .getLogger(Helper.class); | ||
24 | 29 | ||
25 | private final ProcessBuilder processBuilder; | 30 | private final ProcessBuilder processBuilder; |
26 | private final RunaboutMessageReceiver receiver; | 31 | private final RunaboutMessageReceiver receiver; |
27 | private Process process; | 32 | private Process process; |
28 | |||
29 | private volatile GnunetMessage.Body writeMessage; | 33 | private volatile GnunetMessage.Body writeMessage; |
34 | private WriteThread writeThread; | ||
35 | private ReadThread readThread; | ||
30 | 36 | ||
31 | private final class WriteThread implements Runnable { | 37 | private final class WriteThread extends Thread { |
32 | @Override | 38 | @Override |
33 | public void run() { | 39 | public void run() { |
34 | GnunetMessage.Body msg; | 40 | GnunetMessage.Body msg; |
@@ -62,7 +68,7 @@ public class Helper extends MessageQueue { | |||
62 | } | 68 | } |
63 | } | 69 | } |
64 | 70 | ||
65 | private final class ReadThread implements Runnable { | 71 | private final class ReadThread extends Thread { |
66 | private ByteBuffer buffer; | 72 | private ByteBuffer buffer; |
67 | ReadableByteChannel channel; | 73 | ReadableByteChannel channel; |
68 | 74 | ||
@@ -133,6 +139,15 @@ public class Helper extends MessageQueue { | |||
133 | } | 139 | } |
134 | } | 140 | } |
135 | 141 | ||
142 | private String getBinaryPath(String binaryName) { | ||
143 | if (!binaryName.contains("gnunet")) | ||
144 | return binaryName; | ||
145 | String prefix = System.getenv("GNUNET_PREFIX"); | ||
146 | if (prefix == null || prefix.isEmpty()) | ||
147 | return binaryName; | ||
148 | // FIXME: this is not portable at all | ||
149 | return prefix + "/lib/" + "gnunet/" + "libexec/" + binaryName; | ||
150 | } | ||
136 | 151 | ||
137 | public Helper(boolean withControlPipe, String binaryName, List<String> argv, | 152 | public Helper(boolean withControlPipe, String binaryName, List<String> argv, |
138 | RunaboutMessageReceiver receiver) { | 153 | RunaboutMessageReceiver receiver) { |
@@ -141,7 +156,7 @@ public class Helper extends MessageQueue { | |||
141 | if (binaryName == null) { | 156 | if (binaryName == null) { |
142 | throw new AssertionError(); | 157 | throw new AssertionError(); |
143 | } | 158 | } |
144 | command.add(binaryName); | 159 | command.add(getBinaryPath(binaryName)); |
145 | if (argv != null) | 160 | if (argv != null) |
146 | command.addAll(argv); | 161 | command.addAll(argv); |
147 | processBuilder = new ProcessBuilder(command); | 162 | processBuilder = new ProcessBuilder(command); |
@@ -150,6 +165,14 @@ public class Helper extends MessageQueue { | |||
150 | } catch (IOException e) { | 165 | } catch (IOException e) { |
151 | throw new IOError(e); | 166 | throw new IOError(e); |
152 | } | 167 | } |
168 | |||
169 | writeThread = new WriteThread(); | ||
170 | readThread = new ReadThread(); | ||
171 | |||
172 | writeThread.start(); | ||
173 | readThread.start(); | ||
174 | |||
175 | reportReadyForSubmit(); | ||
153 | } | 176 | } |
154 | 177 | ||
155 | /** | 178 | /** |
@@ -191,12 +214,14 @@ public class Helper extends MessageQueue { | |||
191 | 214 | ||
192 | @Override | 215 | @Override |
193 | protected void submit(Envelope ev) { | 216 | protected void submit(Envelope ev) { |
217 | logger.debug("submitting envelope to helper thread"); | ||
194 | synchronized (this) { | 218 | synchronized (this) { |
195 | if (writeMessage != null) | 219 | if (writeMessage != null) |
196 | throw new AssertionError("message queue not implemented correctly"); | 220 | throw new AssertionError("message queue not implemented correctly"); |
197 | writeMessage = ev.message; | 221 | writeMessage = ev.message; |
198 | notifyAll(); | 222 | notifyAll(); |
199 | } | 223 | } |
224 | reportReadyForSubmit(); | ||
200 | } | 225 | } |
201 | 226 | ||
202 | @Override | 227 | @Override |