Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simulate card removal and insertion by raising SIGUSR2 (when using vpcd) #159

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
45 changes: 40 additions & 5 deletions src/main/java/com/licel/jcardsim/remote/VSmartCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import java.security.InvalidParameterException;
import java.util.Enumeration;
import java.util.Properties;
import java.net.SocketException;
import sun.misc.Signal;
import sun.misc.SignalHandler;

/**
* VSmartCard Card Implementation.
Expand All @@ -37,7 +40,7 @@ public class VSmartCard {
public VSmartCard(String host, int port) throws IOException {
VSmartCardTCPProtocol driverProtocol = new VSmartCardTCPProtocol();
driverProtocol.connect(host, port);
startThread(driverProtocol);
startThread(driverProtocol, host, port);
}

static public void main(String args[]) throws Exception {
Expand Down Expand Up @@ -80,9 +83,9 @@ static public void main(String args[]) throws Exception {
new VSmartCard(host, Integer.parseInt(port));
}

private void startThread(VSmartCardTCPProtocol driverProtocol) throws IOException {
private void startThread(VSmartCardTCPProtocol driverProtocol, String host, int port) throws IOException {
sim = new Simulator();
final IOThread ioThread = new IOThread(sim, driverProtocol);
final IOThread ioThread = new IOThread(sim, driverProtocol, host, port);
ShutDownHook hook = new ShutDownHook(ioThread);
Runtime.getRuntime().addShutdownHook(hook);
ioThread.start();
Expand All @@ -102,21 +105,33 @@ public void run() {
}
}

static class IOThread extends Thread {
static class IOThread extends Thread implements SignalHandler {
VSmartCardTCPProtocol driverProtocol;
Simulator sim;
boolean isRunning;
String host;
int port;
boolean inserted;

public IOThread(Simulator sim, VSmartCardTCPProtocol driverProtocol) {
public IOThread(Simulator sim, VSmartCardTCPProtocol driverProtocol, String host, int port) {
this.sim = sim;
this.driverProtocol = driverProtocol;
isRunning = true;
this.host = host;
this.port = port;
inserted = true;
Signal.handle(new Signal("USR2"), this);
}

@Override
public void run() {
while (isRunning) {
try {
if (!inserted) {
synchronized(this) {
wait();
}
}
int cmd = driverProtocol.readCommand();
switch (cmd) {
case VSmartCardTCPProtocol.POWER_ON:
Expand All @@ -132,6 +147,26 @@ public void run() {
driverProtocol.writeData(reply);
break;
}
} catch (SocketException e) {
if (!inserted)
sim.reset();
} catch (Exception e) {}
}
}

public void handle(Signal sig) {
if (inserted) {
inserted = false;
driverProtocol.disconnect();
System.out.println("Smartcard removed");
} else {
try {
driverProtocol.connect(host, port);
inserted = true;
synchronized(this) {
notify();
}
System.out.println("Smartcard inserted");
} catch (Exception e) {}
}
}
Expand Down