📄 isdnchannel.java
字号:
lineDev = new ISDNLine(this, dtiName);
voiceDev = new Voice(this, voiceName);
// link
lineDev.listen(voiceDev);
voiceDev.listen(lineDev);
linestate = RESET;
dnisPref = dnisPreffix;
lineDev.restart();
this.voiceName = voiceName;
this.dtiName = dtiName;
group = new ThreadGroup(dtiName + "/" + voiceName + " group");
serviceThread = new Thread(group, this, dtiName + "/" + voiceName + " service");
serviceThread.start();
}
public void close()
{
super.clear();
if (serviceThread != null) {
Channel.stopGroup(this);
serviceThread.interrupt();
try {
serviceThread.join(500);
}
catch (InterruptedException ie) {};
serviceThread = null;
}
if (lineDev != null) {
lineDev.close();
lineDev = null;
}
if (voiceDev != null) {
voiceDev.close();
voiceDev = null;
}
}
public Voice getVoice() {
return voiceDev;
}
public Device getNetwork() {
return lineDev;
}
public void run() {
while(true) try {
clear();
beginService();
while(true) {
EVT evt = serviceWaitEvent();
if (evt.type == EVT.CCEV_OFFERED) {
linestate = ICALL;
setState(INCOMING);
crn = evt.crn;
if ((Dialogic.debug & Dialogic.DEBUG_ISDN) != 0)
System.out.println(new java.util.Date().toString().substring(11,19) +": Incoming CRN: " + crn);
call = new Call(ISDNChannel.this);
byte dnis[] = new byte[32];
Dialogic.cc_GetDNIS(crn, dnis);
// ASCIIZ, find end and convert to string
int dnisl = 0; while(dnis[dnisl] != 0 && dnisl < (dnis.length-1)) dnisl++;
call.dnis = dnisPref + new String(dnis, 0, dnisl);
if ((Dialogic.debug & Dialogic.DEBUG_ISDN) != 0)
System.out.println(new java.util.Date().toString().substring(11,19) +":Call to " + call.dnis);
if (handler != null) {
endService();
handler.handleCall(call);
}
} else {
if ((Dialogic.debug & Dialogic.DEBUG_ISDN) != 0)
System.out.println(new java.util.Date().toString().substring(11,19) +":Event " + evt);
}
}
}
catch (Exception e) {
if (linestate == DIAL)
// Someone asking me to leave...
return;
System.err.println(this.toString() + " service loop: " + e);
}
finally {
endService();
}
}
public String toString() {
return "ISDNChannel on " + lineDev + "/" + voiceDev;
}
static TNGEN ringLead = new TNGEN(200, -40, 20) ;
static TNGEN ring = new TNGEN(440, -10, 100) ;
static TNGEN ringSilence = new TNGEN(200, -40, 200);
static TPT stdTPT = new TPT();
void accept() {
if (linestate != ICALL)
throw new ChannelException("accept(): wrong state");
try {
if ((Dialogic.debug & Dialogic.DEBUG_ISDN) != 0)
System.out.println(new java.util.Date().toString().substring(11,19) +": ISDN accept()");
beginService();
Dialogic.cc_AcceptCall(crn, 0, Dialogic.EV_ASYNC);
EVT evt = serviceWaitEvent();
if (evt.type != EVT.CCEV_ACCEPT)
throw new ChannelException("Accept error:" + evt);
linestate = RINGS;
ringsSent = 0;
voiceDev.playtone(ringLead, stdTPT);
ringPhase = true;
} finally {
endService();
}
}
void answer(int rings) {
// It is ok to answer w/o accept...
if (!(linestate == RINGS || linestate == ICALL))
throw new ChannelException("answer(): wrong state");
if ((Dialogic.debug & Dialogic.DEBUG_ISDN) != 0)
System.out.println(new java.util.Date().toString().substring(11,19) +": ISDN answer(" + rings + ")");
if (linestate == ICALL)
accept();
// Ring cycle
EVT evt = null;
long now = System.currentTimeMillis();
long conn = call.startTime() + rings * 3000; // 3 secs each
try {
beginService();
if (conn > now)
evt = serviceWaitEvent(conn - now);
if (evt == null) {
Dialogic.cc_AnswerCall(crn, 0, Dialogic.EV_ASYNC);
evt = serviceWaitEvent();
if (evt.type != EVT.CCEV_ANSWERED)
throw new ChannelException("Answer error:" + evt);
linestate = IN;
voiceDev.stop();
} else {
linestate = RESET;
setState(OOS);
throw new ChannelException("answer():" + evt);
}
} finally {
endService();
}
}
void reject(int reason) {
if (linestate != ICALL)
throw new ChannelException("reject(): wrong state");
throw new ChannelException("reject: not implemented!");
}
void dial(Call call, String number) {
if (linestate != IDLE)
throw new ChannelException("dial(): wrong state");
if (blocked)
throw new ChannelException("dial(): blocked");
linestate = DIAL;
if (serviceThread != null) {
Channel.stopGroup(this);
serviceThread.interrupt();
/* Service thread will normally die,
* but simultaneous seizure may prevent that from happening... */
try {
serviceThread.join(500);
}
catch(InterruptedException ie) {
Thread.currentThread().interrupt();
}
// Give up if it did not quit...
if (serviceThread.isAlive()) {
System.err.println("Seizure failure: service not quitting");
throw new ChannelException("dial(): seizure failure");
}
serviceThread = null;
}
this.call = call;
setState(OUTGOING);
crn = lineDev.dial(number, 5);
return;
}
String ani() {
String ani = "";
if (crn == 0)
return "";
byte anis[] = new byte[32];
try {
Dialogic.cc_GetANI(crn,anis);
// ASCIIZ, find end and convert to string
int anisl = 0; while(anis[anisl] != 0 && anisl < (anis.length-1)) anisl++;
ani = new String(anis, 0, anisl);
} catch (Exception e) {
// Runtime exception thrown by CC layer if ani not available
ani = "";
}
return ani;
}
int pulses() {
throw new ChannelException("pulses: not implemented!");
}
synchronized void clear() {
if (linestate == RESET)
return;
linestate = RESET;
super.clear();
if (voiceDev != null) {
voiceDev.stop();
voiceDev.clear();
}
lineDev.listen(voiceDev);
voiceDev.listen(lineDev);
if (crn != 0) {
Dialogic.cc_DropCall(crn, NORMAL_CLEARING, Dialogic.EV_ASYNC);
}
flush();
if (serviceThread == null) {
serviceThread = new Thread(group, this, dtiName + "/" + voiceName + " service");
serviceThread.start();
}
}
// Our line state service fn
protected EVT service(EVT evt) {
if (lineDev.source(evt)) {
switch (evt.type) {
case EVT.CCEV_RESTARTFAIL:
case EVT.CCEV_BLOCKED:
blocked = true;
linestate = RESET;
evt = null;
setState(OOS);
break;
case EVT.CCEV_RESTART:
case EVT.CCEV_UNBLOCKED:
blocked = false;
evt = null;
lineDev.waitCall();
if (linestate == RESET) {
linestate = IDLE;
setState(FREE);
}
break;
case EVT.CCEV_DISCONNECTED:
if (call != null) call.drop();
if (linestate != RESET) {
clear();
setState(OOS);
throw new HangUpException();
}
break;
case EVT.CCEV_CONNECTED:
// Remote answer
if (call != null) call.connect();
break; // Let the event go up
case EVT.CCEV_DROPCALL:
Dialogic.cc_ReleaseCallEx(crn, Dialogic.EV_ASYNC);
if (call != null) call.drop();
crn = 0;
evt = null;
linestate = RESET;
setState(OOS);
case EVT.CCEV_RELEASECALL:
evt = null;
linestate = IDLE;
setState(FREE);
break;
}
} else if (voiceDev.source(evt)) {
if (evt.type == EVT.TDX_PLAYTONE) {
if (linestate == RINGS) {
if (ringPhase) {
voiceDev.playtone(ring, stdTPT);
ringPhase = false;
ringsSent++;
} else {
voiceDev.playtone(ringSilence, stdTPT);
ringPhase = true;
}
return null;
}
}
}
return evt;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -