Skip to content

Commit

Permalink
add missing ThriftSessionManager.java
Browse files Browse the repository at this point in the history
  • Loading branch information
jbellis committed Oct 8, 2012
1 parent 2f979ed commit 5160de5
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/java/org/apache/cassandra/service/ThriftSessionManager.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 5160de5

Please sign in to comment.