You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by jb...@apache.org on 2012/10/08 18:38:57 UTC

git commit: add missing ThriftSessionManager.java

Updated Branches:
  refs/heads/trunk 2f979ed60 -> 5160de591


add missing ThriftSessionManager.java


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/5160de59
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/5160de59
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/5160de59

Branch: refs/heads/trunk
Commit: 5160de59149b61ffb8f63c133c64d899fcf33130
Parents: 2f979ed
Author: Jonathan Ellis <jb...@apache.org>
Authored: Mon Oct 8 11:33:24 2012 -0500
Committer: Jonathan Ellis <jb...@apache.org>
Committed: Mon Oct 8 11:34:05 2012 -0500

----------------------------------------------------------------------
 .../cassandra/service/ThriftSessionManager.java    |   70 +++++++++++++++
 1 files changed, 70 insertions(+), 0 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/5160de59/src/java/org/apache/cassandra/service/ThriftSessionManager.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/service/ThriftSessionManager.java b/src/java/org/apache/cassandra/service/ThriftSessionManager.java
new file mode 100644
index 0000000..d1d3f6e
--- /dev/null
+++ b/src/java/org/apache/cassandra/service/ThriftSessionManager.java
@@ -0,0 +1,70 @@
+/*
+ * 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.cassandra.service;
+
+import java.net.SocketAddress;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Encapsulates the current client state (session).
+ *
+ * We rely on the Thrift server to tell us what socket it is
+ * executing a request for via setCurrentSocket, after which currentSession can do its job anywhere.
+ */
+public class ThriftSessionManager
+{
+    public final static ThriftSessionManager instance = new ThriftSessionManager();
+
+    private final ThreadLocal<SocketAddress> remoteSocket = new ThreadLocal<SocketAddress>();
+    private final Map<SocketAddress, ClientState> activeSocketSessions = new ConcurrentHashMap<SocketAddress, ClientState>();
+
+    /**
+     * @param socket the address on which the current thread will work on requests for until further notice
+     */
+    public void setCurrentSocket(SocketAddress socket)
+    {
+        remoteSocket.set(socket);
+    }
+
+    /**
+     * @return the current session for the most recently given socket on this thread
+     */
+    public ClientState currentSession()
+    {
+        SocketAddress socket = remoteSocket.get();
+        assert socket != null;
+
+        ClientState cState = activeSocketSessions.get(socket);
+        if (cState == null)
+        {
+            cState = new ClientState();
+            activeSocketSessions.put(socket, cState);
+        }
+        return cState;
+    }
+
+    /**
+     * The connection associated with @param socket is permanently finished.
+     */
+    public void connectionComplete(SocketAddress socket)
+    {
+        assert socket != null;
+        activeSocketSessions.remove(socket);
+    }
+}