You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by jv...@apache.org on 2012/12/12 13:11:39 UTC
svn commit: r1420633 -
/mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/
Author: jvermillard
Date: Wed Dec 12 12:11:37 2012
New Revision: 1420633
URL: http://svn.apache.org/viewvc?rev=1420633&view=rev
Log:
start of IoHandler executor support
Added:
mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/
mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/CloseEvent.java
mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/Event.java
mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/EventVisitor.java
mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/FixedThreadPoolHandlerExecutor.java
mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/HandlerExecutor.java
mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/IdleEvent.java
mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/OpenEvent.java
mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/ReceiveEvent.java
mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/SentEvent.java
Added: mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/CloseEvent.java
URL: http://svn.apache.org/viewvc/mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/CloseEvent.java?rev=1420633&view=auto
==============================================================================
--- mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/CloseEvent.java (added)
+++ mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/CloseEvent.java Wed Dec 12 12:11:37 2012
@@ -0,0 +1,43 @@
+/*
+ * 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.mina.service.executor;
+
+import org.apache.mina.api.IoSession;
+
+public class CloseEvent implements Event {
+ private final IoSession session;
+
+ public CloseEvent(final IoSession session) {
+ this.session = session;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public IoSession getSession() {
+ return session;
+ }
+
+ @Override
+ public void visit(EventVisitor visitor) {
+ visitor.visit(this);
+ }
+}
Added: mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/Event.java
URL: http://svn.apache.org/viewvc/mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/Event.java?rev=1420633&view=auto
==============================================================================
--- mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/Event.java (added)
+++ mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/Event.java Wed Dec 12 12:11:37 2012
@@ -0,0 +1,29 @@
+/*
+ * 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.mina.service.executor;
+
+import org.apache.mina.api.IoSession;
+
+public interface Event {
+
+ IoSession getSession();
+
+ void visit(EventVisitor visitor);
+}
Added: mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/EventVisitor.java
URL: http://svn.apache.org/viewvc/mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/EventVisitor.java?rev=1420633&view=auto
==============================================================================
--- mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/EventVisitor.java (added)
+++ mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/EventVisitor.java Wed Dec 12 12:11:37 2012
@@ -0,0 +1,32 @@
+/*
+ * 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.mina.service.executor;
+
+public interface EventVisitor {
+ void visit(OpenEvent event);
+
+ void visit(CloseEvent event);
+
+ void visit(ReceiveEvent event);
+
+ void visit(SentEvent event);
+
+ void visit(IdleEvent event);
+}
Added: mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/FixedThreadPoolHandlerExecutor.java
URL: http://svn.apache.org/viewvc/mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/FixedThreadPoolHandlerExecutor.java?rev=1420633&view=auto
==============================================================================
--- mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/FixedThreadPoolHandlerExecutor.java (added)
+++ mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/FixedThreadPoolHandlerExecutor.java Wed Dec 12 12:11:37 2012
@@ -0,0 +1,142 @@
+/*
+ * 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.mina.service.executor;
+
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.LinkedBlockingDeque;
+
+import org.apache.mina.api.IoSession;
+
+public class FixedThreadPoolHandlerExecutor implements HandlerExecutor {
+
+ private Worker[] workers;
+
+ public FixedThreadPoolHandlerExecutor(int workerThreadCount, int queueSize) {
+ workers = new Worker[workerThreadCount];
+
+ for (int i = 0; i < workerThreadCount; i++) {
+ workers[i] = new Worker(queueSize);
+ }
+ for (int i = 0; i < workerThreadCount; i++) {
+ workers[i].start();
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void execute(Event event) {
+ try {
+ workers[(int) (event.getSession().getId() % workers.length)].enqueue(event);
+ } catch (InterruptedException e) {
+ // interrupt the world
+ return;
+ }
+ }
+
+ private static class Worker extends Thread {
+
+ private static CallHandler caller = new CallHandler();
+
+ private BlockingQueue<Event> queue;
+
+ public Worker(int queueSize) {
+ queue = new LinkedBlockingDeque<Event>(queueSize);
+ }
+
+ public void enqueue(Event event) throws InterruptedException {
+ queue.put(event);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void run() {
+ for (;;) {
+ try {
+
+ Event e = queue.take();
+ e.visit(caller);
+
+ } catch (InterruptedException e) {
+ // end this thread
+ return;
+ }
+ }
+ }
+ }
+
+ private static class CallHandler implements EventVisitor {
+
+ @Override
+ public void visit(CloseEvent event) {
+ IoSession session = event.getSession();
+ try {
+ session.getService().getIoHandler().sessionClosed(session);
+ } catch (Exception e) {
+ session.getService().getIoHandler().exceptionCaught(session, e);
+ }
+ }
+
+ @Override
+ public void visit(IdleEvent event) {
+ IoSession session = event.getSession();
+ try {
+ session.getService().getIoHandler().sessionIdle(session, event.getIdleStatus());
+ } catch (Exception e) {
+ session.getService().getIoHandler().exceptionCaught(session, e);
+ }
+
+ }
+
+ @Override
+ public void visit(OpenEvent event) {
+ IoSession session = event.getSession();
+ try {
+ session.getService().getIoHandler().sessionOpened(session);
+ } catch (Exception e) {
+ session.getService().getIoHandler().exceptionCaught(session, e);
+ }
+
+ }
+
+ @Override
+ public void visit(ReceiveEvent event) {
+ IoSession session = event.getSession();
+ try {
+ session.getService().getIoHandler().messageReceived(session, event.getMessage());
+ } catch (Exception e) {
+ session.getService().getIoHandler().exceptionCaught(session, e);
+ }
+ }
+
+ @Override
+ public void visit(SentEvent event) {
+ IoSession session = event.getSession();
+ try {
+ session.getService().getIoHandler().messageSent(session, event.getMessage());
+ } catch (Exception e) {
+ session.getService().getIoHandler().exceptionCaught(session, e);
+ }
+ }
+ }
+}
Added: mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/HandlerExecutor.java
URL: http://svn.apache.org/viewvc/mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/HandlerExecutor.java?rev=1420633&view=auto
==============================================================================
--- mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/HandlerExecutor.java (added)
+++ mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/HandlerExecutor.java Wed Dec 12 12:11:37 2012
@@ -0,0 +1,25 @@
+/*
+ * 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.mina.service.executor;
+
+public interface HandlerExecutor {
+
+ void execute(Event command);
+}
Added: mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/IdleEvent.java
URL: http://svn.apache.org/viewvc/mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/IdleEvent.java?rev=1420633&view=auto
==============================================================================
--- mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/IdleEvent.java (added)
+++ mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/IdleEvent.java Wed Dec 12 12:11:37 2012
@@ -0,0 +1,51 @@
+/*
+ * 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.mina.service.executor;
+
+import org.apache.mina.api.IdleStatus;
+import org.apache.mina.api.IoSession;
+
+public class IdleEvent implements Event {
+
+ private final IoSession session;
+ private final IdleStatus status;
+
+ public IdleEvent(final IoSession session, final IdleStatus status) {
+ this.session = session;
+ this.status = status;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public IoSession getSession() {
+ return session;
+ }
+
+ public IdleStatus getIdleStatus() {
+ return status;
+ }
+
+ @Override
+ public void visit(EventVisitor visitor) {
+ visitor.visit(this);
+ }
+}
Added: mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/OpenEvent.java
URL: http://svn.apache.org/viewvc/mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/OpenEvent.java?rev=1420633&view=auto
==============================================================================
--- mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/OpenEvent.java (added)
+++ mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/OpenEvent.java Wed Dec 12 12:11:37 2012
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Founimport org.apache.mina.api.IoSession;
+tributor 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.mina.service.executor;
+
+import org.apache.mina.api.IoSession;
+
+public class OpenEvent implements Event {
+ private final IoSession session;
+
+ public OpenEvent(final IoSession session) {
+ this.session = session;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public IoSession getSession() {
+ return session;
+ }
+
+ @Override
+ public void visit(EventVisitor visitor) {
+ visitor.visit(this);
+ }
+}
Added: mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/ReceiveEvent.java
URL: http://svn.apache.org/viewvc/mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/ReceiveEvent.java?rev=1420633&view=auto
==============================================================================
--- mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/ReceiveEvent.java (added)
+++ mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/ReceiveEvent.java Wed Dec 12 12:11:37 2012
@@ -0,0 +1,49 @@
+/*
+ * 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.mina.service.executor;
+
+import org.apache.mina.api.IoSession;
+
+public class ReceiveEvent implements Event {
+ private final IoSession session;
+ private final Object message;
+
+ public ReceiveEvent(final IoSession session, final Object message) {
+ this.session = session;
+ this.message = message;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public IoSession getSession() {
+ return session;
+ }
+
+ public Object getMessage() {
+ return message;
+ }
+
+ @Override
+ public void visit(EventVisitor visitor) {
+ visitor.visit(this);
+ }
+}
Added: mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/SentEvent.java
URL: http://svn.apache.org/viewvc/mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/SentEvent.java?rev=1420633&view=auto
==============================================================================
--- mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/SentEvent.java (added)
+++ mina/mina/trunk/core/src/main/java/org/apache/mina/service/executor/SentEvent.java Wed Dec 12 12:11:37 2012
@@ -0,0 +1,49 @@
+/*
+ * 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.mina.service.executor;
+
+import org.apache.mina.api.IoSession;
+
+public class SentEvent implements Event {
+ private final IoSession session;
+ private final Object message;
+
+ public SentEvent(final IoSession session, final Object message) {
+ this.session = session;
+ this.message = message;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public IoSession getSession() {
+ return session;
+ }
+
+ public Object getMessage() {
+ return message;
+ }
+
+ @Override
+ public void visit(EventVisitor visitor) {
+ visitor.visit(this);
+ }
+}