Skip to content

Commit

Permalink
Windows client-server fixes (#1230)
Browse files Browse the repository at this point in the history
Only one thing in ClientServerTests doesn't work currently.
Maybe someone will find a fix in the future... smile

I tried many things, even reimplementing the whole named pipes thing with JNA...
But it boils down to read/write throwing an exception instead of returning -1...
Since the read is done infinitely in a separate thread, it doesn't know when the output from server is done.
So when a connection breaks, we should just ignore it, and not System.exit(1); as currently.

Commits:

* Try to fix client-server on windows

* Use Files.readAllLines, bcoz java8

* Fix most of client-server tests on windows

* Remove comments..

* Revert clientSocket.close() code

Pull request: #1230
  • Loading branch information
sake92 committed Mar 24, 2021
1 parent f4af127 commit 3e067ab
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 144 deletions.
13 changes: 8 additions & 5 deletions main/client/src/mill/main/client/Lock.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package mill.main.client;
public abstract class Lock implements AutoCloseable{
abstract public Locked lock() throws Exception;
abstract public Locked tryLock() throws Exception;

public void await() throws Exception{
public abstract class Lock implements AutoCloseable {

public abstract Locked lock() throws Exception;

public abstract Locked tryLock() throws Exception;

public void await() throws Exception {
lock().release();
}

/**
* Returns `true` if the lock is *available for taking*
*/
abstract public boolean probe() throws Exception;
public abstract boolean probe() throws Exception;
}
6 changes: 1 addition & 5 deletions main/client/src/mill/main/client/Locked.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package mill.main.client;

import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.util.concurrent.locks.ReentrantLock;
public interface Locked {


public interface Locked{
public void release() throws Exception;
}
66 changes: 40 additions & 26 deletions main/client/src/mill/main/client/Locks.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@
import java.nio.channels.FileChannel;
import java.util.concurrent.locks.ReentrantLock;

public class Locks implements AutoCloseable{
public class Locks implements AutoCloseable {

public Lock processLock;
public Lock serverLock;
public Lock clientLock;
public static Locks files(String lockBase) throws Exception{

public static Locks files(String lockBase) throws Exception {
return new Locks(){{
processLock = new FileLock(lockBase + "/pid");

serverLock = new FileLock(lockBase + "/serverLock");

clientLock = new FileLock(lockBase + "/clientLock");
}};
}
public static Locks memory(){

public static Locks memory() {
return new Locks(){{
this.processLock = new MemoryLock();
this.serverLock = new MemoryLock();
Expand All @@ -32,35 +33,41 @@ public void close() throws Exception {
clientLock.close();
}
}
class FileLocked implements Locked{

class FileLocked implements Locked {

private java.nio.channels.FileLock lock;
public FileLocked(java.nio.channels.FileLock lock){

public FileLocked(java.nio.channels.FileLock lock) {
this.lock = lock;
}
public void release() throws Exception{

public void release() throws Exception {
this.lock.release();
}
}

class FileLock extends Lock{
String path;
RandomAccessFile raf;
FileChannel chan;
public FileLock(String path) throws Exception{
this.path = path;
class FileLock extends Lock {

private RandomAccessFile raf;
private FileChannel chan;

public FileLock(String path) throws Exception {
raf = new RandomAccessFile(path, "rw");
chan = raf.getChannel();
}

public Locked lock() throws Exception{
public Locked lock() throws Exception {
return new FileLocked(chan.lock());
}
public Locked tryLock() throws Exception{

public Locked tryLock() throws Exception {
java.nio.channels.FileLock l = chan.tryLock();
if (l == null) return null;
else return new FileLocked(l);
}
public boolean probe()throws Exception{

public boolean probe() throws Exception {
java.nio.channels.FileLock l = chan.tryLock();
if (l == null) return false;
else {
Expand All @@ -71,30 +78,37 @@ public boolean probe()throws Exception{

@Override
public void close() throws Exception {
raf.close();
chan.close();
raf.close();
}
}
class MemoryLocked implements Locked{
java.util.concurrent.locks.Lock l;
public MemoryLocked(java.util.concurrent.locks.Lock l){

class MemoryLocked implements Locked {

private java.util.concurrent.locks.Lock l;

public MemoryLocked(java.util.concurrent.locks.Lock l) {
this.l = l;
}
public void release() throws Exception{

public void release() throws Exception {
l.unlock();
}
}

class MemoryLock extends Lock{
ReentrantLock innerLock = new ReentrantLock(true);
class MemoryLock extends Lock {

private ReentrantLock innerLock = new ReentrantLock();

public boolean probe(){
return !innerLock.isLocked();
public boolean probe() {
return !innerLock.isLocked();
}

public Locked lock() {
innerLock.lock();
return new MemoryLocked(innerLock);
}

public Locked tryLock() {
if (innerLock.tryLock()) return new MemoryLocked(innerLock);
else return null;
Expand Down
Loading

0 comments on commit 3e067ab

Please sign in to comment.