-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4e9815c
commit ae18fd3
Showing
8 changed files
with
200 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (c) 2016 Jae-jun Kang | ||
// See the file LICENSE for details. | ||
|
||
package x2.links; | ||
|
||
import java.util.concurrent.locks.*; | ||
|
||
import x2.*; | ||
|
||
/** Common base class for single-session client links. */ | ||
public abstract class ClientLink extends SessionBasedLink { | ||
protected LinkSession session; // current link session | ||
|
||
protected ClientLink(String name) { | ||
super(name); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
if (closed) { return; } | ||
|
||
LinkSession session = null; | ||
Lock wlock = rwlock.writeLock(); | ||
wlock.lock(); | ||
try { | ||
if (this.session != null) { | ||
session = this.session; | ||
this.session = null; | ||
} | ||
} | ||
finally { | ||
wlock.unlock(); | ||
} | ||
|
||
if (session != null) { | ||
session.close(); | ||
} | ||
|
||
super.close(); | ||
} | ||
|
||
@Override | ||
public void send(Event e) { | ||
LinkSession currentSession = session(); | ||
if (currentSession == null) { | ||
// drop event | ||
} | ||
currentSession.send(e); | ||
} | ||
|
||
public LinkSession session() { | ||
Lock rlock = rwlock.readLock(); | ||
rlock.lock(); | ||
try { | ||
return session; | ||
} | ||
finally { | ||
rlock.unlock(); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright (c) 2016 Jae-jun Kang | ||
// See the file LICENSE for details. | ||
|
||
package x2.links; | ||
|
||
import x2.*; | ||
|
||
public abstract class LinkSession { | ||
protected int handle; | ||
protected SessionBasedLink link; | ||
|
||
public void close() { | ||
} | ||
|
||
/** Sends out the specified event through this link session. */ | ||
public void send(Event e) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright (c) 2016 Jae-jun Kang | ||
// See the file LICENSE for details. | ||
|
||
package x2.links; | ||
|
||
import java.util.*; | ||
import java.util.concurrent.locks.*; | ||
|
||
import x2.*; | ||
|
||
/** Common base class for multi-session server links. */ | ||
public abstract class ServerLink extends SessionBasedLink { | ||
protected HashMap<Integer, LinkSession> sessions; | ||
|
||
protected ServerLink(String name) { | ||
super(name); | ||
sessions = new HashMap<Integer, LinkSession>(); | ||
} | ||
|
||
/** Broadcasts the specified event to all the connected clients. */ | ||
public void broadcast(Event e) { | ||
Lock rlock = rwlock.readLock(); | ||
rlock.lock(); | ||
ArrayList<LinkSession> snapshot; | ||
try { | ||
snapshot = new ArrayList<LinkSession>(sessions.values()); | ||
} | ||
finally { | ||
rlock.unlock(); | ||
} | ||
for (int i = 0, count = snapshot.size(); i < count; ++i) { | ||
snapshot.get(i).send(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() { | ||
if (closed) { return; } | ||
|
||
// Close all the active sessions | ||
Lock rlock = rwlock.readLock(); | ||
rlock.lock(); | ||
ArrayList<LinkSession> snapshot; | ||
try { | ||
snapshot = new ArrayList<LinkSession>(sessions.values()); | ||
} | ||
finally { | ||
rlock.unlock(); | ||
} | ||
for (int i = 0, count = snapshot.size(); i < count; ++i) { | ||
snapshot.get(i).close(); | ||
} | ||
|
||
Lock wlock = rwlock.writeLock(); | ||
wlock.lock(); | ||
try { | ||
sessions.clear(); | ||
} | ||
finally { | ||
wlock.unlock(); | ||
} | ||
|
||
super.close(); | ||
} | ||
|
||
/** Sends out the specified event through this link channel. */ | ||
public void send(Event e) { | ||
Lock rlock = rwlock.readLock(); | ||
rlock.lock(); | ||
try { | ||
LinkSession session = sessions.get(e._getHandle()); | ||
if (session != null) { | ||
session.send(e); | ||
} | ||
} | ||
finally { | ||
rlock.unlock(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters