Skip to content

Commit

Permalink
adds license, fixes handling of connection close, adds timeout to tra…
Browse files Browse the repository at this point in the history
…ce data init, adds null check for parsed itm packages
  • Loading branch information
Mohamad Ramadan committed Apr 20, 2020
1 parent 67e0d76 commit 5f24d52
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 21 deletions.
21 changes: 21 additions & 0 deletions License
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 cosee GmbH

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 0 additions & 1 deletion src/main/java/itmviewer/action/StartClientAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import itmviewer.service.TclService;
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/itmviewer/service/TclService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import itmviewer.state.ITMSettingsState;
import itmviewer.task.BackgroundClientTask;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.concurrent.Future;

public class TclService {
Expand All @@ -23,17 +25,26 @@ public boolean isConnected() {
public boolean openConnection() {
String host = ITMSettingsState.getTclHost();
String port = ITMSettingsState.getTclPort();
task = new BackgroundClientTask(project, host, port);
threadFuture = ApplicationManager.getApplication().executeOnPooledThread(task);
return true;
if(!isConnected()) {
task = new BackgroundClientTask(project, host, port);
threadFuture = ApplicationManager.getApplication().executeOnPooledThread(task);
return true;
}
return false;
}

public boolean closeConnection() {
boolean result = false;
if(threadFuture != null) {
try {
task.closeConnection();
} catch (IOException e) {
return false;
}
result = threadFuture.cancel(true);
threadFuture = null;
task = null;

}
return result;
}
Expand Down
28 changes: 20 additions & 8 deletions src/main/java/itmviewer/task/BackgroundClientTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,19 @@ public boolean clientIsConnected() {
}

public boolean initTclTraceData() throws IOException {
tcpClient.setTimeout(3000);
tcpClient.write(new StartTraceCommand().generateCommand());
return tcpClient.readChar() == TclBase.field_terminator;
boolean receivedTerminator = tcpClient.readChar() == TclBase.field_terminator;
tcpClient.setTimeout(0);
return receivedTerminator;
}

public void closeConnection() throws IOException {
if(tcpClient != null) {
tcpClient.close();
tcpClient = null;
toolWindow.notifyOnServerClose();
}
}

public void runTclServer() throws IOException {
Expand All @@ -62,8 +73,11 @@ public void runTclServer() throws IOException {
continue;
}
List<TclEntity> entities = ITMDecoder.parseITMData(itmLine);
toolWindow.addITMPackages(entities);
if(entities != null) {
toolWindow.addITMPackages(entities);
}
}
closeConnection();
}

@Override
Expand All @@ -73,12 +87,10 @@ public void run() {
} catch (IOException e) {
Thread.currentThread().interrupt();
} finally {
if (tcpClient != null) {
try {
tcpClient.close();
} catch (IOException e) {
System.out.println("Encountered error when closing");
}
try {
closeConnection();
} catch (IOException e) {
System.out.println("Encountered error when closing");
}
}
}
Expand Down
20 changes: 13 additions & 7 deletions src/main/java/itmviewer/ui/ITMViewerToolWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class ITMViewerToolWindow implements Disposable {
private ContentManager contentManager;
ConsoleView consoleView = null;
TclService service = null;
int lastLogPort = 0;
TclEntity lastEntity = null;
private static final String[] ACTIONS = {"ClearConsole", "OpenSettings", "StartClient", "StopClient"};

@Override
Expand All @@ -51,6 +51,11 @@ public void notifyOnTraceInitFail() {
printMessageWithHeader("FAILED to activate Trace Data Output from RPC Server\n", ConsoleViewContentType.ERROR_OUTPUT);
}


public void notifyOnServerClose() {
printMessageWithHeader("Closed Connection to Server\n", ConsoleViewContentType.SYSTEM_OUTPUT);
}

public void notifyOnConnect() {
printMessageWithHeader("Opened Connection to "+ITMSettingsState.getTclHost()+":"+ITMSettingsState.getTclPort() + "\n", ConsoleViewContentType.SYSTEM_OUTPUT);
}
Expand Down Expand Up @@ -140,17 +145,18 @@ private ConsoleViewContentType getLogLevelByITMPort(int itmPort) {
}
}

public void addITMPackages(List<TclEntity> entities) {
public void addITMPackages(@NotNull List<TclEntity> entities) {
for (TclEntity entity : entities) {
ConsoleViewContentType contentType = getLogLevelByITMPort(entity.getChannel());

if (contentType != null) {
if (lastLogPort != entity.getChannel()) {
String timestamp = ZonedDateTime.now().format(DateTimeFormatter.ofPattern("uuuu.MM.dd.HH.mm.ss"));
consoleView.print("\n"+timestamp, contentType);
if (lastEntity == null || (lastEntity.getChannel() != entity.getChannel() || lastEntity.getContent().endsWith("\n"))) {
printMessageWithHeader(entity.getContent(), contentType);
}
else {
printMessageOnly(entity.getContent(), contentType);
}
consoleView.print(entity.getContent(), contentType);
lastLogPort = entity.getChannel();
lastEntity = entity;
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/tcl/client/TclTcpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.io.*;
import java.net.Socket;
import java.net.SocketException;


public class TclTcpClient {
Expand All @@ -20,6 +21,12 @@ public TclTcpClient() {

}

public void setTimeout(int timeout) throws SocketException {
if(client != null) {
client.setSoTimeout(timeout);
}
}

public void open(@NotNull String host, @NotNull int port) throws IOException {
client = new Socket(host, port);
out = client.getOutputStream();
Expand Down Expand Up @@ -59,6 +66,7 @@ public boolean isConnected() {
public void close() throws IOException {
if (client != null) {
client.close();
in.close();
client = null;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<actions>
<action id="ITMViewer.ClearConsole" class="itmviewer.action.ClearConsoleAction" text="Clear Console" description="Clears console but keeps connection if already opened" icon="AllIcons.Actions.Refresh"/>
<action id="ITMViewer.OpenSettings" class="itmviewer.action.OpenSettingsAction" text="Open Settings" description="Open settings" icon="AllIcons.General.Settings"/>
<action id="ITMViewer.StartClient" class="itmviewer.action.StartClientAction" text="Start Client" description="Start Client" icon="AllIcons.RunConfigurations.TestState.Run"/>
<action id="ITMViewer.StopClient" class="itmviewer.action.StopClientAction" text="Start Client" description="Stop Client" icon="AllIcons.Process.Stop"/>
<action id="ITMViewer.StartClient" class="itmviewer.action.StartClientAction" text="Start Client" description="Start client" icon="AllIcons.RunConfigurations.TestState.Run"/>
<action id="ITMViewer.StopClient" class="itmviewer.action.StopClientAction" text="Stop Client" description="Stop client" icon="AllIcons.Process.Stop"/>
</actions>
</idea-plugin>

0 comments on commit 5f24d52

Please sign in to comment.