Skip to content

Commit

Permalink
Merge pull request #90 from matty-r/milestone-v0.6.0
Browse files Browse the repository at this point in the history
Milestone v0.6.0
  • Loading branch information
matty-r authored Feb 2, 2024
2 parents b928b46 + c4f1f1a commit 61cdb17
Show file tree
Hide file tree
Showing 50 changed files with 2,490 additions and 1,980 deletions.
39 changes: 27 additions & 12 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>com.mattyr</groupId>
<artifactId>urchat</artifactId>
<version>0.5.1</version>
<version>0.6.0</version>

<name>urchat</name>
<url>https://github.com/matty-r/urChat</url>
Expand Down Expand Up @@ -41,6 +41,21 @@
<version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.22.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.22.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.22.1</version>
</dependency>
</dependencies>

<build>
Expand All @@ -49,9 +64,9 @@
<resources>
<resource>
<directory>
src/images
src/resources
</directory>
<targetPath>images/</targetPath>
<targetPath>resources/</targetPath>
</resource>
</resources>
<testSourceDirectory>tests/</testSourceDirectory>
Expand All @@ -62,10 +77,10 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
Expand Down Expand Up @@ -103,15 +118,15 @@
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
</execution>
</executions>
</plugin>
</plugins>
</build>
Expand All @@ -130,4 +145,4 @@
</plugin>
</plugins>
</reporting>
</project>
</project>
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
22 changes: 22 additions & 0 deletions src/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info">
<Appenders>
<Console name="ConsoleAppender" target="SYSTEM_OUT">
<NoMarkerFilter onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<File name="FileAppender" fileName="Logs/urchat-${date:yyyyMMdd}.log" immediateFlush="true" append="true">
<NoMarkerFilter onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS}{UTC}UTC [%t] %-5level %logger{36} - %msg%n"/>
</File>
<File name="BaseChannelAppender" fileName="Logs/urchat-${date:yyyyMMdd}.log" immediateFlush="false" append="true">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS}{UTC}UTC %marker %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="info" additivity="false">
<AppenderRef ref="ConsoleAppender" />
<AppenderRef ref="FileAppender"/>
</Root>
</Loggers>
</Configuration>
83 changes: 49 additions & 34 deletions src/urChatBasic/backend/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Date;
import java.util.logging.Level;
import urChatBasic.backend.MessageHandler.Message;
import urChatBasic.base.ConnectionBase;
import urChatBasic.base.Constants;
import urChatBasic.base.IRCServerBase;
import urChatBasic.base.proxy.ProxyTypes;
import urChatBasic.frontend.DriverGUI;
import urChatBasic.frontend.dialogs.MessageDialog;
import java.awt.event.ActionEvent;
Expand All @@ -38,11 +37,13 @@ public class Connection implements ConnectionBase

private MessageHandler messageHandler;

// Connection keep alive stuff
// Starts the timer with an start delay
private Timer keepAliveTimer = new Timer((int) Duration.ofSeconds(30).toMillis(), new PingKeepalive());
private final int INITIAL_PING_RATE_MS = (int) Duration.ofSeconds(30).toMillis();
// Rate in which to send a PING to the server
private final int PING_RATE_MS = (int) Duration.ofMinutes(5).toMillis();
// Connection keep alive stuff
private Timer keepAliveTimer = new Timer(INITIAL_PING_RATE_MS, new PingKeepalive());

private boolean pingReceived = true;
private final int MAX_RESPONSE_FAILURES = 2;
private int currentFailures = 0;
Expand Down Expand Up @@ -86,24 +87,20 @@ private void startUp () throws IOException
localMessage("Attempting to connect to " + server);

// Determine the socket type to be used
InetSocketAddress address = new InetSocketAddress(server.getName(), Integer.parseInt(getServer().getPort()));
InetSocketAddress endPointAddress = new InetSocketAddress(server.getName(), Integer.parseInt(getServer().getPort()));

if (getServer().usingSOCKS())
if (!getServer().usingProxy().equals(ProxyTypes.NONE.getType()))
{
Proxy proxy = new Proxy(Proxy.Type.SOCKS,
new InetSocketAddress(getServer().getProxyHost(), Integer.parseInt(getServer().getProxyPort())));
Socket proxySocket = new Socket(proxy);
getServer().usingProxy().createProxy(getServer().getProxyHost(), Integer.parseInt(getServer().getProxyPort()));
Socket proxySocket = getServer().usingProxy().connectThroughProxy(endPointAddress);

if (getServer().usingTLS())
{
proxySocket.connect(address, 5000);

SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
mySocket = sslsocketfactory.createSocket(proxySocket, address.getHostName(), address.getPort(), true);
mySocket = sslsocketfactory.createSocket(proxySocket, endPointAddress.getHostName(), endPointAddress.getPort(), true);
mySocket.setKeepAlive(true);
} else
{
proxySocket.connect(address, 5000);
mySocket = proxySocket;
}
} else
Expand All @@ -116,14 +113,15 @@ private void startUp () throws IOException
{
mySocket = new Socket();
}
mySocket.connect(address, 500);
mySocket.connect(endPointAddress, 500);
}

writer = new BufferedWriter(new OutputStreamWriter(mySocket.getOutputStream()));
reader = new BufferedReader(new InputStreamReader(mySocket.getInputStream(), StandardCharsets.UTF_8));

// if we got this far, we established a connection to the server
DriverGUI.gui.setupServerTab(server);
DriverGUI.gui.addToCreatedServers(getServer());

localMessage("Initiating authentication...");

Expand All @@ -135,8 +133,7 @@ private void startUp () throws IOException
Thread.sleep(500);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
Constants.LOGGER.warn(e.getLocalizedMessage(), e);
}

// Initiate connection to the server.
Expand All @@ -146,8 +143,9 @@ private void startUp () throws IOException
localMessage("Connecting with nick " + getServer().getNick());
writer.flush();

keepAliveTimer.setInitialDelay(INITIAL_PING_RATE_MS);
keepAliveTimer.setDelay(PING_RATE_MS);
keepAliveTimer.start();
keepAliveTimer.restart();

while ((line = reader.readLine()) != null && !shutdown)
{
Expand All @@ -171,7 +169,7 @@ private void startUp () throws IOException
mySocket.close();
} catch (IOException e)
{
Constants.LOGGER.log(Level.SEVERE, "Error stopping connected.. " + e.getLocalizedMessage());
Constants.LOGGER.error( "Error stopping connected.. " + e.getLocalizedMessage());
}
}
}
Expand All @@ -186,7 +184,13 @@ public void run ()
{
try
{
if (shutdown)
if (shutdown && reconnect)
{
pingReceived = true;

writer.write("PING " + new Date().toInstant().toEpochMilli() + "\r\n");
writer.flush();
} else if (shutdown)
{
keepAliveTimer.stop();
} else
Expand All @@ -213,8 +217,7 @@ public void run ()
}
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
Constants.LOGGER.warn(e.getLocalizedMessage(), e);
}
}
});
Expand All @@ -226,7 +229,9 @@ public void setPingReceived ()
{
// Reset the fails
currentFailures = 0;
reconnectAttempts = 0;
pingReceived = true;
reconnect = false;
}

public MessageHandler getMessageHandler ()
Expand Down Expand Up @@ -254,7 +259,7 @@ public void sendClientText (String clientText, String fromChannel) throws IOExce
}
if (clientText.toLowerCase().startsWith("/join"))
{
outText = "JOIN " + clientText.replace("/join ", "") + "\r\n";
outText = "JOIN " + clientText.replace("/join ", "").toLowerCase() + "\r\n";
} else if (clientText.toLowerCase().startsWith("/nick"))
{
outText = "NICK " + clientText.replace("/nick ", "") + "\r\n";
Expand Down Expand Up @@ -282,7 +287,7 @@ public void sendClientText (String clientText, String fromChannel) throws IOExce
} else if (clientText.toLowerCase().startsWith("/quit"))
{
outText = "QUIT :" + clientText.replace("/quit ", "") + "\r\n";
} else if (clientText.toLowerCase().startsWith("/part"))
} else if (clientText.toLowerCase().startsWith("/part") || clientText.toLowerCase().startsWith("/leave"))
{
outText = "PART " + fromChannel + " :" + clientText.replace("/part ", "") + "\r\n";
} else if (clientText.toLowerCase().startsWith("/me") || clientText.toLowerCase().startsWith("/action"))
Expand Down Expand Up @@ -317,7 +322,7 @@ public void sendClientText (String clientText, String fromChannel) throws IOExce
writer.flush();
} catch (Exception e)
{
Constants.LOGGER.log(Level.SEVERE, "Problem writing to socket: " + e.toString() + outText);
Constants.LOGGER.error( "Problem writing to socket: " + e.toString() + outText);
}

try
Expand All @@ -328,15 +333,15 @@ public void sendClientText (String clientText, String fromChannel) throws IOExce
}
} catch (Exception e)
{
Constants.LOGGER.log(Level.SEVERE,
Constants.LOGGER.error(
"Problem writing out client message: " + e.toString() + clientMessage.getRawMessage());
}

Constants.LOGGER.log(Level.FINE, "Client Text:- " + fromChannel + " " + outText);
Constants.LOGGER.debug( "Client Text:- " + fromChannel + " " + outText.trim());
} else
{

Constants.LOGGER.log(Level.WARNING,
Constants.LOGGER.error(
"Not connected. Unable to send text:- " + fromChannel + " " + clientMessage.getRawMessage());
}
}
Expand All @@ -345,7 +350,7 @@ public void sendClientText (String clientText, String fromChannel) throws IOExce
private void localMessage (String message)
{
server.printServerText(message);
Constants.LOGGER.log(Level.INFO, "Local Text:-" + message);
Constants.LOGGER.info( "Local Text:-" + message);
}

private void serverMessage (Message newMessage)
Expand All @@ -355,10 +360,10 @@ private void serverMessage (Message newMessage)
try
{
messageHandler.parseMessage(newMessage);
Constants.LOGGER.log(Level.FINE, newMessage.toString());
Constants.LOGGER.debug( newMessage.toString());
} catch (Exception e)
{
Constants.LOGGER.log(Level.WARNING, e.toString() + newMessage);
Constants.LOGGER.error(e.toString() + newMessage);
}
}
}
Expand All @@ -378,7 +383,7 @@ public void run ()
startUp();
} else
{
Constants.LOGGER.log(Level.SEVERE, "Incomplete settings: (Port " + getServer().getPort() + ") (Server "
Constants.LOGGER.error( "Incomplete settings: (Port " + getServer().getPort() + ") (Server "
+ getServer() + ") (Nick " + getServer().getNick() + ") ");
}

Expand All @@ -392,18 +397,18 @@ public void run ()
run();
} else if (shutdown)
{
Constants.LOGGER.log(Level.INFO, "Disconnected safely!");
Constants.LOGGER.info( "Disconnected safely!");
} else
{
shutdown = true;
reader.close();
writer.close();
mySocket.close();
Constants.LOGGER.log(Level.WARNING, "Disconnected unsafely!");
Constants.LOGGER.error("Disconnected unsafely!");
}
} catch (IOException e)
{
Constants.LOGGER.log(Level.SEVERE, "startUp() failed! " + e.getLocalizedMessage());
Constants.LOGGER.error( "startUp() failed! " + e.getLocalizedMessage());
MessageDialog dialog = new MessageDialog("startUp() failed! " + e.getLocalizedMessage(), "Error",
JOptionPane.ERROR_MESSAGE);
dialog.setVisible(true);
Expand All @@ -415,4 +420,14 @@ public void disconnect ()
{
shutdown = true;
}

@Override
public void reconnect ()
{
reconnectAttempts = 0;
shutdown = true;
reconnect = true;
keepAliveTimer.setInitialDelay(100);
keepAliveTimer.restart();
}
}
Loading

0 comments on commit 61cdb17

Please sign in to comment.