Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
android:minSdkVersion="19"
android:targetSdkVersion="21" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Expand Down Expand Up @@ -113,4 +112,4 @@

</application>

</manifest>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.att.arotcpcollector;

import android.support.annotation.Nullable;
import android.util.Log;

import com.att.arotcpcollector.ip.IPv4Header;
Expand All @@ -36,7 +37,6 @@
import java.nio.channels.SocketChannel;
import java.nio.channels.UnresolvedAddressException;
import java.nio.channels.UnsupportedAddressTypeException;
import java.util.Hashtable;
import java.util.Iterator;

/**
Expand All @@ -50,7 +50,6 @@ public class SessionManager {
private static Object syncObj = new Object();
private static volatile SessionManager instance = null;
private SessionTable table = null;
public static Object syncTable = new Object();
private SocketProtector protector = null;
Selector selector;

Expand Down Expand Up @@ -90,9 +89,7 @@ public void keepSessionAlive(Session session) {
if (sessionKey == null) {
sessionKey = this.createKey(session.getDestAddress(), session.getDestPort(), session.getSourceIp(), session.getSourcePort());
}
synchronized (syncTable) {
table.put(sessionKey, session);
}
table.put(sessionKey, session);
}
}

Expand Down Expand Up @@ -163,82 +160,52 @@ public Session getSession(int ipAddress, int destPort, int srcIpAddress, int src
String sessionKey = createKey(ipAddress, destPort, srcIpAddress, srcPort);
Session session = null;

synchronized (syncTable) {
if (table.containsKey(sessionKey)) {
session = table.get(sessionKey);
}
if (table.containsKey(sessionKey)) {
session = table.get(sessionKey);
}
return session;
}

public Session getSessionByKey(String sessionKey) {
Session session = null;
synchronized (syncTable) {
if (table.containsKey(sessionKey)) {
session = table.get(sessionKey);
}

if (table.containsKey(sessionKey)) {
session = table.get(sessionKey);
}
return session;
}

@Nullable
public Session getSessionByDatagramChannel(DatagramChannel channel) {
Session session = null;
synchronized (syncTable) {
Iterator<Session> it = table.values().iterator();
while (it.hasNext()) {
Session sess = it.next();
if (sess.getUdpChannel() == channel) {
session = sess;
break;
}
for (final Session session: table.values()) {
if (session.getUdpChannel() == channel) {
return session;
}
}
return session;
return null;
}

@Nullable
public Session getSessionByChannel(SocketChannel channel) {
Session session = null;
synchronized (syncTable) {
Iterator<Session> it = table.values().iterator();
while (it.hasNext()) {
Session sess = it.next();
if (sess.getSocketchannel() == channel) {
session = sess;
break;
}
for (final Session session: table.values()) {
if (session.getSocketchannel() == channel) {
return session;
}
}
return session;
return null;
}

public void removeSessionByChannel(SocketChannel channel) {
String key = null;
String tmp = null;
Session session = null;
synchronized (syncTable) {
Iterator<String> it = table.keySet().iterator();
while (it.hasNext()) {
tmp = it.next();
Session sess = table.get(tmp);
if (sess != null && sess.getSocketchannel() == channel) {
key = tmp;
session = sess;
break;
}

}
}
if (key != null) {
synchronized (syncTable) {
for (final String key: table.keySet()) {
Session session = table.get(key);
if (session != null && session.getSocketchannel() == channel) {
table.remove(key);
Log.d(TAG,
"closed session -> " + PacketUtil.intToIPAddress(session.getDestAddress()) + ":" + session.getDestPort() + "-"
+ PacketUtil.intToIPAddress(session.getSourceIp()) + ":" + session.getSourcePort());
break;
}
}
if (session != null) {
Log.d(TAG,
"closed session -> " + PacketUtil.intToIPAddress(session.getDestAddress()) + ":" + session.getDestPort() + "-"
+ PacketUtil.intToIPAddress(session.getSourceIp()) + ":" + session.getSourcePort());
session = null;
}
}

/**
Expand All @@ -251,10 +218,8 @@ public void removeSessionByChannel(SocketChannel channel) {
*/
public void closeSession(int ip, int port, int srcIp, int srcPort) {
String keys = createKey(ip, port, srcIp, srcPort);
Session session = null; //getSession(ip, port, srcIp, srcPort);
synchronized (syncTable) {
session = table.remove(keys);
}
Session session = table.remove(keys); //getSession(ip, port, srcIp, srcPort);

if (session != null) {
try {
SocketChannel chan = session.getSocketchannel();
Expand All @@ -267,7 +232,6 @@ public void closeSession(int ip, int port, int srcIp, int srcPort) {
Log.d(TAG,
"closed session -> " + PacketUtil.intToIPAddress(session.getDestAddress()) + ":" + session.getDestPort() + "-"
+ PacketUtil.intToIPAddress(session.getSourceIp()) + ":" + session.getSourcePort());
session = null;
}
}

Expand All @@ -286,23 +250,19 @@ public void closeSession(Session session) {
sessionKey = this.createKey(session.getDestAddress(), session.getDestPort(), session.getSourceIp(), session.getSourcePort());
}

synchronized (syncTable) {
table.remove(sessionKey);
}
if (session != null) {
try {
SocketChannel chan = session.getSocketchannel();
if (chan != null) {
chan.close();
}
} catch (IOException e) {
e.printStackTrace();
table.remove(sessionKey);

try {
SocketChannel chan = session.getSocketchannel();
if (chan != null) {
chan.close();
}
Log.d(TAG,
"closed session -> " + PacketUtil.intToIPAddress(session.getDestAddress()) + ":" + session.getDestPort() + "-"
+ PacketUtil.intToIPAddress(session.getSourceIp()) + ":" + session.getSourcePort());
session = null;
} catch (IOException e) {
e.printStackTrace();
}
Log.d(TAG,
"closed session -> " + PacketUtil.intToIPAddress(session.getDestAddress()) + ":" + session.getDestPort() + "-"
+ PacketUtil.intToIPAddress(session.getSourceIp()) + ":" + session.getSourcePort());
}

/**
Expand All @@ -316,11 +276,8 @@ public void closeSession(Session session) {
*/
public Session createNewUDPSession(int ip, int port, int srcIp, int srcPort) {
String sessionKey = createKey(ip, port, srcIp, srcPort);
boolean found = false;
synchronized (syncTable) {
found = table.containsKey(sessionKey);
}
if (found) {

if (table.containsKey(sessionKey)) {
return null;
}
Session ses = new Session();
Expand Down Expand Up @@ -386,20 +343,16 @@ public Session createNewUDPSession(int ip, int port, int srcIp, int srcPort) {

ses.setUdpChannel(channel);

synchronized (syncTable) {
if (!table.containsKey(sessionKey)) {
table.put(sessionKey, ses);
} else {
found = true;
}
}
if (found) {
if (table.containsKey(sessionKey)) {
try {
channel.close();
} catch (IOException e) {
e.printStackTrace();
}
} else {
table.put(sessionKey, ses);
}

if (ses != null) {
Log.d(TAG, "new UDP session successfully created.");
}
Expand All @@ -418,11 +371,8 @@ public Session createNewUDPSession(int ip, int port, int srcIp, int srcPort) {
*/
public Session createNewSession(int ip, int port, int srcIp, int srcPort) throws SessionCreateException {
String sessionKey = createKey(ip, port, srcIp, srcPort);
boolean found = false;
synchronized (syncTable) {
found = table.containsKey(sessionKey);
}
if (found) {

if (table.containsKey(sessionKey)) {
Session session = table.get(sessionKey);
session.setAbortingConnection(true);
throw new SessionCreateException("Session already exist");
Expand Down Expand Up @@ -496,20 +446,15 @@ public Session createNewSession(int ip, int port, int srcIp, int srcPort) throws

session.setSocketchannel(channel);

synchronized (syncTable) {
if (!table.containsKey(sessionKey)) {
table.put(sessionKey, session);
} else {
found = true;
}
}
if (found) {
if (table.containsKey(sessionKey)) {
try {
channel.close();
} catch (IOException e) {
e.printStackTrace();
}
session = null;
} else {
table.put(sessionKey, session);
}

return session;
Expand Down
Loading