@Override
	public WriteMsg write(FileContent data) throws RemoteException, IOException {
		String fileName = data.getFileName();

		// check if this is a commit acknowledgment
		if (data.getData() == null) {
			synchronized (this) {
				ReplicaLoc[] locations = locMap.get(fileName);
				metaDataWriter.write(fileName);
				for (int i = 0; i < locations.length; i++) {
					metaDataWriter.write(" " + locations[i].getName());
				}
				metaDataWriter.write("\n");
				metaDataWriter.flush();
			}
			return null;
		} else {
			// This step guarantees that clients who request same file reach out
			// the
			// primary replica in the order which they obtain their transaction
			// id's
			Lock lock = null;
			try {
				if (!fileLock.containsKey(fileName)) {
					lock = new ReentrantLock();
					fileLock.put(fileName, lock);
				} else {
					lock = fileLock.get(fileName);
				}
				lock.lock();
				int tId = txnID.incrementAndGet();
				int ts = timeStamp.incrementAndGet();
				ReplicaLoc[] locations = null;
				if (locMap.containsKey(fileName)) {
					locations = locMap.get(fileName);
				} else {
					locations = selectRandomReplicas();
				}
				locMap.put(fileName, locations);
				ReplicaLoc primary = locations[0];
				ReplicaServerClientInterface primaryServer = null;
				try {
					primaryServer = (ReplicaServerClientInterface) LocateRegistry
							.getRegistry(primary.getHost(), primary.getPort())
							.lookup(primary.getName());
				} catch (Exception e) {
				}
				primaryServer.write(tId, 1, data);
				return new WriteMsg(tId, ts, primary);
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			} finally {
				lock.unlock();
			}
		}
	}