All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mindoo.domino.jna.utils.ServerConsoleThread Maven / Gradle / Ivy

There is a newer version: 0.9.53
Show newest version
package com.mindoo.domino.jna.utils;

import java.util.concurrent.Callable;
import java.util.concurrent.LinkedBlockingQueue;

import com.mindoo.domino.jna.errors.NotesErrorUtils;
import com.mindoo.domino.jna.gc.NotesGC;
import com.mindoo.domino.jna.gc.NotesGC.IDisposableCustomValue;
import com.mindoo.domino.jna.internal.Mem32;
import com.mindoo.domino.jna.internal.Mem64;
import com.mindoo.domino.jna.internal.NotesNativeAPI32;
import com.mindoo.domino.jna.internal.NotesNativeAPI64;
import com.mindoo.domino.jna.internal.structs.ConsoleEntry32;
import com.mindoo.domino.jna.internal.structs.ConsoleEntry64;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.LongByReference;

public class ServerConsoleThread extends Thread implements IDisposableCustomValue {
	private long m_hQueue64;
	private int m_hQueue32;
	private boolean m_queueDisposed;
	private LinkedBlockingQueue m_msgQueue = new LinkedBlockingQueue<>();
	
	ServerConsoleThread(long hQueue64) {
		m_hQueue64 = hQueue64;
	}

	ServerConsoleThread(int hQueue32) {
		m_hQueue32 = hQueue32;
	}

	@Override
	public void dispose() {
		if (m_queueDisposed) {
			return;
		}
		
		short result;
		if (PlatformUtils.is64Bit()) {
			result = NotesNativeAPI64.get().QueueDelete(m_hQueue64);
		}
		else {
			result = NotesNativeAPI32.get().QueueDelete(m_hQueue32);
		}
		NotesErrorUtils.checkResult(result);
		m_queueDisposed = true;
	}
	
	public LinkedBlockingQueue getQueue() {
		return m_msgQueue;
	}
	
	@Override
	public void run() {
		NotesInitUtils.notesInitThread();
		try {
			NotesGC.runWithAutoGC(new Callable() {

				@Override
				public Object call() throws Exception {
					while (!m_queueDisposed) {
						short result;
						if (PlatformUtils.is64Bit()) {
							LongByReference retDataHandle = new LongByReference();
							result = NotesNativeAPI64.get().QueueGet(m_hQueue64, retDataHandle);
							NotesErrorUtils.checkResult(result);
							
							long hData = retDataHandle.getValue();
							if (hData!=0) {
								Pointer ptr = Mem64.OSLockObject(hData);
								try {
									ConsoleEntry64 consoleEntry = ConsoleEntry64.newInstance(ptr);
									Pointer txtPtr = ptr.share(consoleEntry.size());
									String txt = NotesStringUtils.fromLMBCS(txtPtr, consoleEntry.Length);
									m_msgQueue.add(txt);
								}
								finally {
									Mem64.OSUnlockObject(hData);
									Mem64.OSMemFree(hData);
								}
							}
						}
						else {
							IntByReference retDataHandle = new IntByReference();
							result = NotesNativeAPI32.get().QueueGet(m_hQueue32, retDataHandle);
							NotesErrorUtils.checkResult(result);
							
							int hData = retDataHandle.getValue();
							if (hData!=0) {
								Pointer ptr = Mem32.OSLockObject(hData);
								try {
									ConsoleEntry32 consoleEntry = ConsoleEntry32.newInstance(ptr);
									Pointer txtPtr = ptr.share(consoleEntry.size());
									String txt = NotesStringUtils.fromLMBCS(txtPtr, consoleEntry.Length);
									m_msgQueue.add(txt);
								}
								finally {
									Mem32.OSUnlockObject(hData);
									Mem32.OSMemFree(hData);
								}
							}
						}
					}
					
					return null;
				}});
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			NotesInitUtils.notesTermThread();
		}
	}

}