mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-12-22 02:27:58 +01:00
[smack-debug] Factor duplicate code into its own method
This commit is contained in:
parent
ae2394ea83
commit
45a4987818
1 changed files with 48 additions and 88 deletions
|
@ -428,50 +428,8 @@ public class EnhancedDebugger extends SmackDebugger {
|
||||||
private final PriorityBlockingQueue<String> buffer = new PriorityBlockingQueue<>();
|
private final PriorityBlockingQueue<String> buffer = new PriorityBlockingQueue<>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void read(final String line) {
|
public void read(final String string) {
|
||||||
|
addBatched(string, buffer, receivedText);
|
||||||
buffer.add(line);
|
|
||||||
|
|
||||||
SwingUtilities.invokeLater(() -> {
|
|
||||||
List<String> linesToAdd = new ArrayList<>();
|
|
||||||
String data;
|
|
||||||
Instant start = Instant.now();
|
|
||||||
try {
|
|
||||||
// To reduce overhead/increase performance, try to process up to a certain amount of lines at the
|
|
||||||
// same time, when they arrive in rapid succession.
|
|
||||||
while (linesToAdd.size() < 50
|
|
||||||
&& Duration.between(start, Instant.now()).compareTo(Duration.ofMillis(100)) < 0
|
|
||||||
&& (data = buffer.poll(10, TimeUnit.MILLISECONDS)) != null) {
|
|
||||||
linesToAdd.add(data);
|
|
||||||
}
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
// Interrupted wait-for-poll. Process all data now.
|
|
||||||
}
|
|
||||||
|
|
||||||
if (linesToAdd.isEmpty()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (EnhancedDebuggerWindow.PERSISTED_DEBUGGER &&
|
|
||||||
!EnhancedDebuggerWindow.getInstance().isVisible()) {
|
|
||||||
// Do not add content if the parent is not visible
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete lines from the top, if lines to be added will exceed the maximum.
|
|
||||||
int linesToDelete = receivedText.getLineCount() + linesToAdd.size() - EnhancedDebuggerWindow.MAX_TABLE_ROWS;
|
|
||||||
if (linesToDelete > 0) {
|
|
||||||
try {
|
|
||||||
receivedText.replaceRange("", 0, receivedText.getLineEndOffset(linesToDelete - 1));
|
|
||||||
}
|
|
||||||
catch (BadLocationException e) {
|
|
||||||
LOGGER.log(Level.SEVERE, "Error with line offset, MAX_TABLE_ROWS is set too low: " + EnhancedDebuggerWindow.MAX_TABLE_ROWS, e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add the new content.
|
|
||||||
receivedText.append(String.join(NEWLINE, linesToAdd));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
debugReader.addReaderListener(readerListener);
|
debugReader.addReaderListener(readerListener);
|
||||||
|
@ -482,9 +440,21 @@ public class EnhancedDebugger extends SmackDebugger {
|
||||||
private final PriorityBlockingQueue<String> buffer = new PriorityBlockingQueue<>();
|
private final PriorityBlockingQueue<String> buffer = new PriorityBlockingQueue<>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(final String line) {
|
public void write(final String string) {
|
||||||
|
addBatched(string, buffer, sentText);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
debugWriter.addWriterListener(writerListener);
|
||||||
|
|
||||||
buffer.add(line);
|
// Assign the reader/writer objects to use the debug versions. The packet reader
|
||||||
|
// and writer will use the debug versions when they are created.
|
||||||
|
reader = debugReader;
|
||||||
|
writer = debugWriter;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void addBatched(String string, PriorityBlockingQueue<String> buffer, JTextArea jTextArea) {
|
||||||
|
buffer.add(string);
|
||||||
|
|
||||||
SwingUtilities.invokeLater(() -> {
|
SwingUtilities.invokeLater(() -> {
|
||||||
List<String> linesToAdd = new ArrayList<>();
|
List<String> linesToAdd = new ArrayList<>();
|
||||||
|
@ -499,43 +469,33 @@ public class EnhancedDebugger extends SmackDebugger {
|
||||||
linesToAdd.add(data);
|
linesToAdd.add(data);
|
||||||
}
|
}
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
// Interrupted wait-for-poll. Process all data now.
|
LOGGER.log(Level.FINER, "Interrupted wait-for-poll in addBatched(). Process all data now.", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (linesToAdd.isEmpty()) {
|
if (linesToAdd.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (EnhancedDebuggerWindow.PERSISTED_DEBUGGER &&
|
if (EnhancedDebuggerWindow.PERSISTED_DEBUGGER && !EnhancedDebuggerWindow.getInstance().isVisible()) {
|
||||||
!EnhancedDebuggerWindow.getInstance().isVisible()) {
|
|
||||||
// Do not add content if the parent is not visible
|
// Do not add content if the parent is not visible
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete lines from the top, if lines to be added will exceed the maximum.
|
// Delete lines from the top, if lines to be added will exceed the maximum.
|
||||||
int linesToDelete = sentText.getLineCount() + linesToAdd.size() - EnhancedDebuggerWindow.MAX_TABLE_ROWS;
|
int linesToDelete = jTextArea.getLineCount() + linesToAdd.size() - EnhancedDebuggerWindow.MAX_TABLE_ROWS;
|
||||||
if (linesToDelete > 0) {
|
if (linesToDelete > 0) {
|
||||||
try {
|
try {
|
||||||
sentText.replaceRange("", 0, sentText.getLineEndOffset(linesToDelete - 1));
|
jTextArea.replaceRange("", 0, jTextArea.getLineEndOffset(linesToDelete - 1));
|
||||||
}
|
} catch (BadLocationException e) {
|
||||||
catch (BadLocationException e) {
|
LOGGER.log(Level.SEVERE, "Error with line offset, MAX_TABLE_ROWS is set too low: "
|
||||||
LOGGER.log(Level.SEVERE, "Error with line offset, MAX_TABLE_ROWS is set too low: " + EnhancedDebuggerWindow.MAX_TABLE_ROWS, e);
|
+ EnhancedDebuggerWindow.MAX_TABLE_ROWS, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the new content.
|
// Add the new content.
|
||||||
sentText.append(String.join(NEWLINE, linesToAdd));
|
jTextArea.append(String.join(NEWLINE, linesToAdd));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
|
||||||
debugWriter.addWriterListener(writerListener);
|
|
||||||
|
|
||||||
// Assign the reader/writer objects to use the debug versions. The packet reader
|
|
||||||
// and writer will use the debug versions when they are created.
|
|
||||||
reader = debugReader;
|
|
||||||
writer = debugWriter;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void addAdhocPacketPanel() {
|
private void addAdhocPacketPanel() {
|
||||||
// Create UI elements for sending ad-hoc messages.
|
// Create UI elements for sending ad-hoc messages.
|
||||||
|
|
Loading…
Reference in a new issue