Author: mturk Date: Mon Aug 8 11:26:12 2011 New Revision: 1154920 URL: http://svn.apache.org/viewvc?rev=1154920&view=rev Log: Add simple IPC test case and some fixes to make it work
Added: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestIpc.java (with props) Modified: commons/sandbox/runtime/trunk/build.xml commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/IpcDescriptor.java commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/IpcServerEndpoint.java commons/sandbox/runtime/trunk/src/main/native/os/win32/arch_ipcs.h commons/sandbox/runtime/trunk/src/main/native/os/win32/init.c commons/sandbox/runtime/trunk/src/main/native/os/win32/ipcsock.c commons/sandbox/runtime/trunk/src/main/native/os/win32/semaphore.c Modified: commons/sandbox/runtime/trunk/build.xml URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/build.xml?rev=1154920&r1=1154919&r2=1154920&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/build.xml (original) +++ commons/sandbox/runtime/trunk/build.xml Mon Aug 8 11:26:12 2011 @@ -430,6 +430,17 @@ The Apache Software Foundation (http://w </sequential> </parallel> </target> + <target name="testipcs" depends="tests"> + <parallel> + <sequential> + <runtest groups="init,ipcs.parent" name="ipcs.parent"/> + </sequential> + <sequential> + <sleep milliseconds="100" /> + <runtest groups="init,ipcs.child" name="ipcs.child"/> + </sequential> + </parallel> + </target> <!-- =================================================================== --> <!-- Run Example --> Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/IpcDescriptor.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/IpcDescriptor.java?rev=1154920&r1=1154919&r2=1154920&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/IpcDescriptor.java (original) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/IpcDescriptor.java Mon Aug 8 11:26:12 2011 @@ -34,7 +34,7 @@ final class IpcDescriptor extends Descri { private static native int close0(long fd); - private static native long create0(int flags) + private static native long create0() throws IOException; private static native long socket0(int type, boolean blocking) throws IOException; @@ -60,31 +60,25 @@ final class IpcDescriptor extends Descri closed = false; } - public void create(AddressFamily af, SocketType type) - throws IOException - { - create(af, type, true); - } - - public void create(AddressFamily af, SocketType type, boolean blocking) + public void create() throws IOException { if (valid()) close0(fd); - fd = socket0(type.valueOf(), blocking); + fd = create0(); closed = false; } public void create(SocketType type) throws IOException { - create(type, false); + create(type, true); } public void create(SocketType type, boolean blocking) throws IOException { - this.fd = create0(0); + this.fd = socket0(type.valueOf(), blocking); closed = false; } Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/IpcServerEndpoint.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/IpcServerEndpoint.java?rev=1154920&r1=1154919&r2=1154920&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/IpcServerEndpoint.java (original) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/IpcServerEndpoint.java Mon Aug 8 11:26:12 2011 @@ -164,7 +164,7 @@ public class IpcServerEndpoint extends S if (bound) throw new IOException(Local.sm.get("endpoint.EBOUND")); if (sd.closed()) - sd.create(SocketType.STREAM, blocking); + sd.create(); if (backlog == 0) backlog = LISTEN_BACKLOG; int rc = bind0(sd.fd(), endpoint.sockaddr(), backlog); Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/arch_ipcs.h URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/arch_ipcs.h?rev=1154920&r1=1154919&r2=1154920&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/os/win32/arch_ipcs.h (original) +++ commons/sandbox/runtime/trunk/src/main/native/os/win32/arch_ipcs.h Mon Aug 8 11:26:12 2011 @@ -382,8 +382,6 @@ struct IPCSERVER * evet so writter can continue with writting. */ -#define ACR_DESC_PTR(D, T) ((D) == 0) ? 0 : (T)((J2P(D, acr_sd_t *))->p) - #if defined(__cplusplus) extern "C" { #endif Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/init.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/init.c?rev=1154920&r1=1154919&r2=1154920&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/os/win32/init.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/os/win32/init.c Mon Aug 8 11:26:12 2011 @@ -33,6 +33,7 @@ LPSYSTEM_INFO acr_osinf = &osi LPOSVERSIONINFOEXA acr_osver = &osver; HANDLE acr_raised_event = 0; HANDLE acr_alived_mutex = 0; +extern int AcrIpcInit(void); typedef struct tlsd_t { @@ -167,6 +168,8 @@ JNI_OnLoad(JavaVM *vm, void *reserved) return JNI_ERR; if (AcrInitCoreClasses(env) == JNI_FALSE) return JNI_ERR; + if (AcrIpcInit() != 0) + return JNI_ERR; return JNI_VERSION_1_4; } Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/ipcsock.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/ipcsock.c?rev=1154920&r1=1154919&r2=1154920&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/os/win32/ipcsock.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/os/win32/ipcsock.c Mon Aug 8 11:26:12 2011 @@ -143,7 +143,7 @@ AcquireMutex(HANDLE hMutex) WaitForSingleObject(hMutex, INFINITE); } -ACR_NET_EXPORT(jint, IpcEndpoint, init0)(JNI_STDARGS) +int AcrIpcInit() { static int inited = 0; @@ -203,7 +203,6 @@ AcrIpcRemoteOpen(LPCSTR szAddress) goto failed; if ((pr->s = MapViewOfFile(pr->hServerMeta, FILE_MAP_ALL_ACCESS, 0, 0, 0)) == 0) goto failed; - printf("[client] Opened server mapping for process %d of size %d\n", pr->s->dwProcessId, pr->s->Queue.dwSize); if ((pr->hServerProc = OpenProcess(STANDARD_RIGHTS_ALL | PROCESS_DUP_HANDLE, FALSE, pr->s->dwProcessId)) == 0) goto failed; @@ -349,8 +348,14 @@ ACR_NET_EXPORT(jint, IpcServerEndpoint, DWORD dwShareLen; DWORD dwShareSiz = backlog * sizeof(IPCSOCK_ACCEPT) + sizeof(IPCSOCK_SERVER); acr_sockaddr_t *ca = SOCKADDR_CAST(cb); - LPIPCSERVER sp = ACR_DESC_PTR(fp, LPIPCSERVER); + acr_sd_t *ss = J2P(fp, acr_sd_t *); + LPIPCSERVER sp; + if (ss == 0 || ss->p == 0) { + SOCKADDR_RELEASE(cb, ca); + return ACR_EBADF; + } + sp = (LPIPCSERVER)ss->p; if (sp->hServerMap != 0) { SOCKADDR_RELEASE(cb, ca); return WSAEISCONN; @@ -374,8 +379,8 @@ ACR_NET_EXPORT(jint, IpcServerEndpoint, sp->s->nAcceptSema = H2DW(sp->hAcceptSema); sp->s->nAcceptSync = H2DW(sp->hAcceptSync); sp->s->nAcceptLock = H2DW(sp->hAcceptLock); - SOCKADDR_RELEASE(cb, ca); + return 0; failed: rc = GetLastError(); @@ -2032,12 +2037,13 @@ ACR_NET_EXPORT(jint, IpcEndpoint, connec return ACR_EBADF; ca = SOCKADDR_CAST(cb); rc = AcrIpcConnect(AcrIpcSdRetain(sd), ca->hostname, timeout); + fflush(stdout); SOCKADDR_RELEASE(cb, ca); AcrSdRelease(sd); return rc; } -ACR_NET_EXPORT(jlong, IpcDescriptor, create0)(JNI_STDARGS, jint flags) +ACR_NET_EXPORT(jlong, IpcDescriptor, create0)(JNI_STDARGS) { acr_sd_t *sd; IPCSERVER *sp; @@ -2053,7 +2059,6 @@ ACR_NET_EXPORT(jlong, IpcDescriptor, cre } ACR_RING_INIT(&sp->rConnections, IPCSOCK, rLink); sd->p = sp; - sd->flags = flags; sd->refs = 1; sd->type = ACR_DT_IPCSERVER; sp->nReferences = 1; @@ -2092,10 +2097,11 @@ ACR_NET_EXPORT(jlong, IpcDescriptor, soc ACR_NET_EXPORT(jint, IpcDescriptor, close0)(JNI_STDARGS, jlong fp) { - int rc = WSAENOTSOCK; + int rc = WSAENOTSOCK; acr_sd_t *sd = J2P(fp, acr_sd_t *); LPVOID sock; int type; + if (sd == 0) return ACR_EBADF; sock = InterlockedExchangePointer(&sd->p, 0); @@ -2103,7 +2109,7 @@ ACR_NET_EXPORT(jint, IpcDescriptor, clos AcrSdRelease(sd); if (sock == 0) return ACR_EBADF; - if (sd->type == ACR_DT_IPCSERVER) { + if (type == ACR_DT_IPCSERVER) { LPIPCSERVER sp = sock; sp->bClosed = TRUE; SetEvent(sp->hAcceptSync); @@ -2112,7 +2118,7 @@ ACR_NET_EXPORT(jint, IpcDescriptor, clos else rc = 0; } - else if (sd->type == ACR_DT_IPCSOCK) { + else if (type == ACR_DT_IPCSOCK) { rc = AcrIpcSocketClose(sock); } return rc; Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/semaphore.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/semaphore.c?rev=1154920&r1=1154919&r2=1154920&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/os/win32/semaphore.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/os/win32/semaphore.c Mon Aug 8 11:26:12 2011 @@ -83,7 +83,7 @@ ACR_WIN_EXPORT(jint, WindowsSemaphore, o HANDLE h = 0; WITH_WSTR(name) { - h = OpenSemaphoreW(READ_CONTROL | SEMAPHORE_MODIFY_STATE, FALSE, J2S(name)); + h = OpenSemaphoreW(SYNCHRONIZE | READ_CONTROL | SEMAPHORE_MODIFY_STATE, FALSE, J2S(name)); if (h == 0) { ACR_THROW_SYS_ERRNO(); } Added: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestIpc.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestIpc.java?rev=1154920&view=auto ============================================================================== --- commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestIpc.java (added) +++ commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestIpc.java Mon Aug 8 11:26:12 2011 @@ -0,0 +1,106 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.runtime.net; + +import java.io.IOException; +import java.io.File; +import org.testng.annotations.*; +import org.testng.Assert; + +import org.apache.commons.runtime.io.Descriptor; +import org.apache.commons.runtime.Semaphore; + +public class TestIpc extends Assert +{ + + private static final String semname = "acrSemp23"; + private static final String ipcname = "acrIpcs23"; + + + private void parentInit() + throws Exception + { + try { + Semaphore.remove(semname); + } catch (Exception x) { + // Ignore + } + Semaphore s = Semaphore.create(semname, 1); + assertNotNull(s); + } + + private void childInit() + throws Exception + { + Semaphore s = null; + int step = 125; + while (step <= 2000) { + try { + s = Semaphore.open(semname); + break; + } catch (Exception x) { + + } + Thread.sleep(step); + step *= 2; + } + assertNotNull(s); + s.acquire(); + } + + @Test(groups = { "ipcs.parent" }) + public void ipcSimpleAccept() + throws Exception + { + System.out.println("[parent] Creating ipc server" ); + System.out.flush(); + parentInit(); + IpcServerEndpoint ss = new IpcServerEndpoint(); + IpcEndpointAddress sa = new IpcEndpointAddress(ipcname); + ss.configureBlocking(false); + ss.bind(sa); + System.out.println("[parent] Waiting for a child to connect"); + System.out.flush(); + IpcEndpoint ec = ss.accept(); + assertNotNull(ec); + System.out.println("[parent] Accepted."); + System.out.flush(); + ss.close(); + ec.close(); + System.out.println("[parent] Done."); + System.out.flush(); + } + + @Test(groups = { "ipcs.child" }) + public void ipcSimpleConnect() + throws Exception + { + childInit(); + + System.out.println("[child] Connecting"); + System.out.flush(); + IpcEndpointAddress sa = new IpcEndpointAddress(ipcname); + + IpcEndpoint cs = new IpcEndpoint(); + cs.connect(sa); + assertTrue(cs.isBlocking()); + cs.close(); + System.out.println("[child] Done."); + System.out.flush(); + } + +} Propchange: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestIpc.java ------------------------------------------------------------------------------ svn:eol-style = native