Skip to content

Instantly share code, notes, and snippets.

@ecelis
Created March 26, 2015 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ecelis/cc35d37943b273a9b509 to your computer and use it in GitHub Desktop.
Save ecelis/cc35d37943b273a9b509 to your computer and use it in GitHub Desktop.
Java FileLock & IPC server example
package server.ipc;
import java.io.*;
import java.net.*;
//import server.model.*;
public class RepositoryServer {
ServerSocket serverSocket = null;
int state = 0;
//IProtocolHandler protocolHandler;
//Repository repository;
/*public RepositoryServer(Repository rep, IProtocolHandler ph) {
protocolHandler = ph;
repository = rep;
}*/
public void bind() throws IOException {
serverSocket = new ServerSocket(9172);
state = 1;
}
public void process() throws IOException {
while (state == 1) {
Socket client = serverSocket.accept();
//new RepositoryThread(client, protocolHandler).start();
new RepositoryThread(client, null).start();
}
shutdown();
}
void shutdown() throws IOException {
if (serverSocket != null) {
serverSocket.close();
serverSocket = null;
state = 0;
}
}
}
package server.ipc;
import java.io.*;
import java.net.*;
public class RepositoryThread extends Thread {
Socket client;
BufferedReader fromClient;
PrintWriter toClient;
//IProtocolHandler handler;
RepositoryThread (Socket s, Object h) throws IOException {
fromClient = new BufferedReader(new InputStreamReader(s.getInputStream()));
toClient = new PrintWriter (s.getOutputStream(), true);
client = s;
//handler = h;
}
public void run() {
// have handler manage the protocol until it decides it is done.
/*while (handler.process(fromClient, toClient)) {
}*/
try {
fromClient.close();
toClient.close();
client.close();
} catch (IOException e) {
System.err.println("Unable to close connection:" + e.getMessage());
}
}
}
package server;
import java.io.*;
import java.nio.channels.FileLock;
import server.ipc.*;
public class ServerLauncher {
public static final String defaultLocation = "/home/ecelis/Repository";//this is the directory that will hold the lockFile file
public static void create(FileLock lock) throws Exception {
create(new File(defaultLocation), lock);
}
public static void create(File dir, FileLock lock) throws Exception {
if(lock != null) {
System.out.println("Server started only once");
RepositoryServer server = new RepositoryServer();
server.bind();
} else {
// If locked exit! Don't leave java processes hangin out there
System.exit(0);
}
}
public static void main(String[] args) throws Exception {
ServerManager manager = ServerManager.getInstance(new File(defaultLocation));
System.out
.println("Server awaiting client connections. Press Enter to terminate");
manager.start();
manager.process();
}
}
package server.ipc;
import java.util.*;
import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
import server.*;
/*import server.model.Repository;
import util.Fingerprint;*/
public class ServerManager extends Thread {
RepositoryServer server;
File directory;
public final String lockFileName = "lockFile";
File f;
private FileChannel channel;
private FileLock lock;
private static ServerManager instance = null;
protected ServerManager()
{
// Prevent instances
}
protected ServerManager(File dir) throws Exception {
directory = new File("/home/ecelis/Repository/"); //dir;
f = new File(directory, lockFileName);
channel = new RandomAccessFile(f, "rw").getChannel();
lock = channel.tryLock();
ServerLauncher.create(directory, lock);
}
public synchronized static ServerManager getInstance() {
if(instance == null) {
instance = new ServerManager();
}
return instance;
}
public synchronized static ServerManager getInstance(File dir) throws Exception {
if(instance == null) {
instance = new ServerManager(dir);
}
return instance;
}
/*
public boolean isLocked(File dir) throws FileNotFoundException {
FileInputStream stream = new FileInputStream(
new File(dir, "lockFile"));
FileChannel ch = stream.getChannel();
try{
ch.tryLock();
}catch(Exception e){
return true;
}
return false;
}
*/
boolean lockRepository(File dir) {
try {
if (!lock.isValid())
lock = channel.lock();
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
boolean unlockRepository(File dir) {
try {
lock.release();
channel.close();
f.delete();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public void run() {
lockRepository(directory);
new Scanner(System.in).nextLine();
unlockRepository(directory);
System.exit(0);
}
public void process() throws Exception {
server.process();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment