Search in sources :

Example 6 with ConnectHandler

use of com.sun.jna.platform.win32.DdemlUtil.ConnectHandler in project jna by java-native-access.

the class DdemlUtilTest method testQueryConvInfoSetUserHandle.

@Test
public void testQueryConvInfoSetUserHandle() throws InterruptedException {
    final String serviceName = "TestService";
    final String topicName = "TestTopic";
    final String testExecute = "Execute�������";
    final CountDownLatch executesProcessed = new CountDownLatch(1);
    StandaloneDdeClient client = null;
    StandaloneDdeClient server = null;
    try {
        client = new StandaloneDdeClient() {

            private final XactCompleteHandler xactCompleteHandler = new XactCompleteHandler() {

                public void onXactComplete(int transactionType, int dataFormat, HCONV hConv, HSZ topic, HSZ item, HDDEDATA hdata, ULONG_PTR transactionIdentifier, ULONG_PTR statusFlag) {
                    CONVINFO convInfo = wrap(hConv).queryConvInfo(transactionIdentifier.intValue());
                    if (convInfo.hUser.intValue() == 42) {
                        executesProcessed.countDown();
                    }
                }
            };

            {
                registerXactCompleteHandler(xactCompleteHandler);
                this.initialize(Ddeml.APPCMD_CLIENTONLY | Ddeml.CBF_SKIP_REGISTRATIONS | Ddeml.CBF_SKIP_UNREGISTRATIONS);
            }
        };
        server = new StandaloneDdeClient() {

            private final ConnectHandler connectHandler = new ConnectHandler() {

                public boolean onConnect(int transactionType, HSZ topic, HSZ service, Ddeml.CONVCONTEXT convcontext, boolean sameInstance) {
                    return topicName.equals(queryString(topic));
                }
            };

            private final ExecuteHandler executeHandler = new ExecuteHandler() {

                public int onExecute(int transactionType, HCONV hconv, HSZ topic, Ddeml.HDDEDATA commandStringData) {
                    Pointer[] pointer = new Pointer[] { accessData(commandStringData, null) };
                    try {
                        String commandString = pointer[0].getWideString(0);
                        if (testExecute.equals(commandString) && queryString(topic).equals(topicName)) {
                            return Ddeml.DDE_FACK;
                        }
                    } finally {
                        synchronized (pointer) {
                            unaccessData(commandStringData);
                        }
                    }
                    return Ddeml.DDE_FNOTPROCESSED;
                }
            };

            {
                registerConnectHandler(connectHandler);
                registerExecuteHandler(executeHandler);
                this.initialize(Ddeml.APPCMD_FILTERINITS | Ddeml.CBF_SKIP_ALLNOTIFICATIONS);
            }
        };
        server.nameService(serviceName, Ddeml.DNS_REGISTER);
        IDdeConnection con = client.connect(serviceName, topicName, null);
        con.execute(testExecute, Ddeml.TIMEOUT_ASYNC, null, new BaseTSD.DWORD_PTR(42L));
        assertTrue(executesProcessed.await(3, TimeUnit.SECONDS));
    } finally {
        closeQuitely(client);
        closeQuitely(server);
    }
}
Also used : HCONV(com.sun.jna.platform.win32.Ddeml.HCONV) Pointer(com.sun.jna.Pointer) CountDownLatch(java.util.concurrent.CountDownLatch) ULONG_PTR(com.sun.jna.platform.win32.BaseTSD.ULONG_PTR) HSZ(com.sun.jna.platform.win32.Ddeml.HSZ) CONVINFO(com.sun.jna.platform.win32.Ddeml.CONVINFO) ConnectHandler(com.sun.jna.platform.win32.DdemlUtil.ConnectHandler) ExecuteHandler(com.sun.jna.platform.win32.DdemlUtil.ExecuteHandler) HDDEDATA(com.sun.jna.platform.win32.Ddeml.HDDEDATA) StandaloneDdeClient(com.sun.jna.platform.win32.DdemlUtil.StandaloneDdeClient) XactCompleteHandler(com.sun.jna.platform.win32.DdemlUtil.XactCompleteHandler) IDdeConnection(com.sun.jna.platform.win32.DdemlUtil.IDdeConnection) Test(org.junit.Test)

Example 7 with ConnectHandler

use of com.sun.jna.platform.win32.DdemlUtil.ConnectHandler in project jna by java-native-access.

the class DdemlUtilTest method testEnableCallback.

@Test
public void testEnableCallback() throws InterruptedException {
    final String serviceName = "TestService";
    final String topicName = "TestTopic";
    final String testExecute = "Execute�������";
    final CountDownLatch executesProcessed = new CountDownLatch(3);
    StandaloneDdeClient client = null;
    StandaloneDdeClient server = null;
    try {
        client = new StandaloneDdeClient() {

            private final XactCompleteHandler xactCompleteHandler = new XactCompleteHandler() {

                public void onXactComplete(int transactionType, int dataFormat, HCONV hConv, HSZ topic, HSZ item, HDDEDATA hdata, ULONG_PTR transactionIdentifier, ULONG_PTR statusFlag) {
                    executesProcessed.countDown();
                }
            };

            {
                registerXactCompleteHandler(xactCompleteHandler);
                this.initialize(Ddeml.APPCMD_CLIENTONLY | Ddeml.CBF_SKIP_REGISTRATIONS | Ddeml.CBF_SKIP_UNREGISTRATIONS);
            }
        };
        server = new StandaloneDdeClient() {

            private final ConnectHandler connectHandler = new ConnectHandler() {

                public boolean onConnect(int transactionType, HSZ topic, HSZ service, Ddeml.CONVCONTEXT convcontext, boolean sameInstance) {
                    return topicName.equals(queryString(topic));
                }
            };

            private final ExecuteHandler executeHandler = new ExecuteHandler() {

                public int onExecute(int transactionType, HCONV hconv, HSZ topic, Ddeml.HDDEDATA commandStringData) {
                    Pointer[] pointer = new Pointer[] { accessData(commandStringData, null) };
                    try {
                        String commandString = pointer[0].getWideString(0);
                        if (testExecute.equals(commandString) && queryString(topic).equals(topicName)) {
                            return Ddeml.DDE_FACK;
                        }
                    } finally {
                        synchronized (pointer) {
                            unaccessData(commandStringData);
                        }
                    }
                    return Ddeml.DDE_FNOTPROCESSED;
                }
            };

            {
                registerConnectHandler(connectHandler);
                registerExecuteHandler(executeHandler);
                this.initialize(Ddeml.APPCMD_FILTERINITS | Ddeml.CBF_SKIP_ALLNOTIFICATIONS);
            }
        };
        server.nameService(serviceName, Ddeml.DNS_REGISTER);
        server.enableCallback(Ddeml.EC_DISABLE);
        assertThat(server.enableCallback(Ddeml.EC_QUERYWAITING), is(false));
        IDdeConnection con = client.connect(serviceName, topicName, null);
        WinDef.DWORDByReference result = new WinDef.DWORDByReference();
        con.execute(testExecute, Ddeml.TIMEOUT_ASYNC, result, null);
        con.execute(testExecute, Ddeml.TIMEOUT_ASYNC, result, null);
        con.execute(testExecute, Ddeml.TIMEOUT_ASYNC, result, null);
        assertThat(server.enableCallback(Ddeml.EC_QUERYWAITING), is(true));
        server.enableCallback(Ddeml.EC_ENABLEALL);
        assertTrue(executesProcessed.await(3, TimeUnit.SECONDS));
    } finally {
        closeQuitely(client);
        closeQuitely(server);
    }
}
Also used : HCONV(com.sun.jna.platform.win32.Ddeml.HCONV) Pointer(com.sun.jna.Pointer) CountDownLatch(java.util.concurrent.CountDownLatch) ULONG_PTR(com.sun.jna.platform.win32.BaseTSD.ULONG_PTR) HSZ(com.sun.jna.platform.win32.Ddeml.HSZ) ConnectHandler(com.sun.jna.platform.win32.DdemlUtil.ConnectHandler) ExecuteHandler(com.sun.jna.platform.win32.DdemlUtil.ExecuteHandler) HDDEDATA(com.sun.jna.platform.win32.Ddeml.HDDEDATA) StandaloneDdeClient(com.sun.jna.platform.win32.DdemlUtil.StandaloneDdeClient) XactCompleteHandler(com.sun.jna.platform.win32.DdemlUtil.XactCompleteHandler) IDdeConnection(com.sun.jna.platform.win32.DdemlUtil.IDdeConnection) Test(org.junit.Test)

Example 8 with ConnectHandler

use of com.sun.jna.platform.win32.DdemlUtil.ConnectHandler in project jna by java-native-access.

the class DdemlUtilTest method testAdvise.

@Test
public void testAdvise() throws InterruptedException {
    final String serviceName = "TestService";
    final String topicName = "TestTopic";
    final String itemName = "TestItem";
    final String testValue = "Execute�������";
    final CountDownLatch adviseStartReceived = new CountDownLatch(1);
    final CountDownLatch adviseDataRequestReceived = new CountDownLatch(1);
    final CountDownLatch adviseDataReceived = new CountDownLatch(1);
    StandaloneDdeClient client = null;
    StandaloneDdeClient server = null;
    try {
        client = new StandaloneDdeClient() {

            private final AdvdataHandler advdataHandler = new DdemlUtil.AdvdataHandler() {

                public int onAdvdata(int transactionType, int dataFormat, HCONV hconv, HSZ topic, HSZ item, HDDEDATA hdata) {
                    if (dataFormat == WinUser.CF_UNICODETEXT && topicName.equals(queryString(topic)) && itemName.equals(queryString(item))) {
                        Pointer pointer = accessData(hdata, null);
                        try {
                            if (testValue.equals(pointer.getWideString(0))) {
                                adviseDataReceived.countDown();
                            }
                        } finally {
                            unaccessData(hdata);
                        }
                    }
                    return Ddeml.DDE_FACK;
                }
            };

            {
                registerAdvdataHandler(advdataHandler);
                this.initialize(Ddeml.APPCMD_CLIENTONLY | Ddeml.CBF_SKIP_REGISTRATIONS | Ddeml.CBF_SKIP_UNREGISTRATIONS);
            }
        };
        server = new StandaloneDdeClient() {

            private final ConnectHandler connectHandler = new ConnectHandler() {

                public boolean onConnect(int transactionType, HSZ topic, HSZ service, Ddeml.CONVCONTEXT convcontext, boolean sameInstance) {
                    return topicName.equals(queryString(topic));
                }
            };

            private final AdvreqHandler advreqHandler = new AdvreqHandler() {

                public HDDEDATA onAdvreq(int transactionType, int dataFormat, HCONV hconv, HSZ topic, HSZ item, int count) {
                    adviseDataRequestReceived.countDown();
                    Memory mem = new Memory((testValue.length() + 1) * 2);
                    mem.setWideString(0, testValue);
                    return createDataHandle(mem, (int) mem.size(), 0, item, dataFormat, 0);
                }
            };

            private final AdvstartHandler advstartHandler = new AdvstartHandler() {

                public boolean onAdvstart(int transactionType, int dataFormat, HCONV hconv, HSZ topic, HSZ item) {
                    adviseStartReceived.countDown();
                    return dataFormat == WinUser.CF_UNICODETEXT && topicName.equals(queryString(topic)) && itemName.equals(queryString(item));
                }
            };

            {
                registerConnectHandler(connectHandler);
                registerAdvReqHandler(advreqHandler);
                registerAdvstartHandler(advstartHandler);
                this.initialize(Ddeml.APPCMD_FILTERINITS | Ddeml.CBF_SKIP_ALLNOTIFICATIONS);
            }
        };
        server.nameService(serviceName, Ddeml.DNS_REGISTER);
        IDdeConnection con = client.connect(serviceName, topicName, null);
        con.advstart(itemName, WinUser.CF_UNICODETEXT, 5 * 1000, null, null);
        assertTrue(adviseStartReceived.await(5, TimeUnit.SECONDS));
        server.postAdvise(topicName, itemName);
        assertTrue(adviseDataRequestReceived.await(5, TimeUnit.SECONDS));
        assertTrue(adviseDataReceived.await(5, TimeUnit.SECONDS));
    } finally {
        closeQuitely(client);
        closeQuitely(server);
    }
}
Also used : Memory(com.sun.jna.Memory) HCONV(com.sun.jna.platform.win32.Ddeml.HCONV) Pointer(com.sun.jna.Pointer) CountDownLatch(java.util.concurrent.CountDownLatch) HSZ(com.sun.jna.platform.win32.Ddeml.HSZ) AdvdataHandler(com.sun.jna.platform.win32.DdemlUtil.AdvdataHandler) AdvreqHandler(com.sun.jna.platform.win32.DdemlUtil.AdvreqHandler) ConnectHandler(com.sun.jna.platform.win32.DdemlUtil.ConnectHandler) HDDEDATA(com.sun.jna.platform.win32.Ddeml.HDDEDATA) StandaloneDdeClient(com.sun.jna.platform.win32.DdemlUtil.StandaloneDdeClient) IDdeConnection(com.sun.jna.platform.win32.DdemlUtil.IDdeConnection) AdvstartHandler(com.sun.jna.platform.win32.DdemlUtil.AdvstartHandler) Test(org.junit.Test)

Example 9 with ConnectHandler

use of com.sun.jna.platform.win32.DdemlUtil.ConnectHandler in project jna by java-native-access.

the class DdemlUtilTest method testRequest.

@Test
public void testRequest() throws InterruptedException {
    final String serviceName = "TestService";
    final String topicName = "TestTopic";
    final String itemName = "TestItem";
    final String testValue = "Execute�������";
    final CountDownLatch pokeReceived = new CountDownLatch(1);
    StandaloneDdeClient client = null;
    StandaloneDdeClient server = null;
    try {
        client = new StandaloneDdeClient() {

            {
                this.initialize(Ddeml.APPCMD_CLIENTONLY | Ddeml.CBF_SKIP_REGISTRATIONS | Ddeml.CBF_SKIP_UNREGISTRATIONS);
            }
        };
        server = new StandaloneDdeClient() {

            private final ConnectHandler connectHandler = new ConnectHandler() {

                public boolean onConnect(int transactionType, HSZ topic, HSZ service, Ddeml.CONVCONTEXT convcontext, boolean sameInstance) {
                    return topicName.equals(queryString(topic));
                }
            };

            private final RequestHandler requestHandler = new RequestHandler() {

                public HDDEDATA onRequest(int transactionType, int dataFormat, HCONV hconv, HSZ topic, HSZ item) {
                    if (dataFormat == WinUser.CF_UNICODETEXT && queryString(topic).equals(topicName) && queryString(item).equals(itemName)) {
                        Memory mem = new Memory((testValue.length() + 1) * 2);
                        mem.setWideString(0, testValue);
                        HDDEDATA result = createDataHandle(mem, (int) mem.size(), 0, item, dataFormat, 0);
                        pokeReceived.countDown();
                        return result;
                    } else {
                        return null;
                    }
                }
            };

            {
                registerConnectHandler(connectHandler);
                registerRequestHandler(requestHandler);
                this.initialize(Ddeml.APPCMD_FILTERINITS | Ddeml.CBF_SKIP_ALLNOTIFICATIONS);
            }
        };
        server.nameService(serviceName, Ddeml.DNS_REGISTER);
        IDdeConnection con = client.connect(serviceName, topicName, null);
        HDDEDATA data = con.request(itemName, WinUser.CF_UNICODETEXT, 5 * 1000, null, null);
        try {
            try {
                Pointer pointer = server.accessData(data, null);
                assertThat(pointer.getWideString(0), is(testValue));
            } finally {
                server.unaccessData(data);
            }
        } finally {
            server.freeDataHandle(data);
        }
        assertTrue(pokeReceived.await(5, TimeUnit.SECONDS));
    } finally {
        closeQuitely(client);
        closeQuitely(server);
    }
}
Also used : ConnectHandler(com.sun.jna.platform.win32.DdemlUtil.ConnectHandler) RequestHandler(com.sun.jna.platform.win32.DdemlUtil.RequestHandler) HDDEDATA(com.sun.jna.platform.win32.Ddeml.HDDEDATA) StandaloneDdeClient(com.sun.jna.platform.win32.DdemlUtil.StandaloneDdeClient) Memory(com.sun.jna.Memory) HCONV(com.sun.jna.platform.win32.Ddeml.HCONV) Pointer(com.sun.jna.Pointer) IDdeConnection(com.sun.jna.platform.win32.DdemlUtil.IDdeConnection) CountDownLatch(java.util.concurrent.CountDownLatch) HSZ(com.sun.jna.platform.win32.Ddeml.HSZ) Test(org.junit.Test)

Aggregations

HCONV (com.sun.jna.platform.win32.Ddeml.HCONV)9 HSZ (com.sun.jna.platform.win32.Ddeml.HSZ)9 ConnectHandler (com.sun.jna.platform.win32.DdemlUtil.ConnectHandler)9 IDdeConnection (com.sun.jna.platform.win32.DdemlUtil.IDdeConnection)9 StandaloneDdeClient (com.sun.jna.platform.win32.DdemlUtil.StandaloneDdeClient)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 Test (org.junit.Test)9 Pointer (com.sun.jna.Pointer)7 HDDEDATA (com.sun.jna.platform.win32.Ddeml.HDDEDATA)7 ExecuteHandler (com.sun.jna.platform.win32.DdemlUtil.ExecuteHandler)4 Memory (com.sun.jna.Memory)3 ULONG_PTR (com.sun.jna.platform.win32.BaseTSD.ULONG_PTR)3 XactCompleteHandler (com.sun.jna.platform.win32.DdemlUtil.XactCompleteHandler)3 ConnectConfirmHandler (com.sun.jna.platform.win32.DdemlUtil.ConnectConfirmHandler)2 DisconnectHandler (com.sun.jna.platform.win32.DdemlUtil.DisconnectHandler)2 CONVINFO (com.sun.jna.platform.win32.Ddeml.CONVINFO)1 AdvdataHandler (com.sun.jna.platform.win32.DdemlUtil.AdvdataHandler)1 AdvreqHandler (com.sun.jna.platform.win32.DdemlUtil.AdvreqHandler)1 AdvstartHandler (com.sun.jna.platform.win32.DdemlUtil.AdvstartHandler)1 PokeHandler (com.sun.jna.platform.win32.DdemlUtil.PokeHandler)1