// Close connection when things have gone wrong... void close() { // System.out.println("Closing Connection..."); try { sendTime = -1L; if (output != null) { output.close(); output = null; } if (input != null) { input.Close(); input = null; } if (connection != null) { connection.Close(); connection = null; } } catch (Exception e) { if (PrologSession.debugging()) { PrologSession.WriteStackTrace(e, Console.Error); } } }
PBTerm sendAtom(string commandName, string argument) { lock (this) { if (parentSession != null) { return(parentSession.sendAtom(commandName, argument)); } try { initSend(); // Write a fastrw term output.writeCompound(commandName, 1); output.writeAtom(argument); output.commit(); return(parser.parseProlog(input)); } catch (System.IO.IOException e) { if (PrologSession.debugging()) { PrologSession.WriteStackTrace(e, Console.Error); } close(); return(null); } finally { finishSend(); } } }
/// <summary> Returns <code>true</code> if this {@link se.sics.prologbeans.PBTerm} is a /// proper list and all of its elements are character codes or one character /// atoms. Returns <code>false</code> otherwise. /// </summary> /// <returns> whether the argument is a string /// </returns> public virtual bool isString() { if (PrologSession.debugging()) { Console.Error.WriteLine("Entering PBTerm.isString()"); } return(false); }
internal virtual void queryStarted(PrologSession session) { lock (this) { if (activeCount == sessions.Length) { PrologSession[] tmp = new PrologSession[activeCount + 10]; Array.Copy(sessions, 0, tmp, 0, activeCount); // [PM] 4.1.3 SPRM 11845, 11819 sessions = tmp; } sessions [activeCount++] = session; } }
internal virtual void queryFinished(PrologSession session) { lock (this) { for (int i = 0; i < activeCount; i++) { if (sessions [i] == session) { activeCount--; sessions [i] = sessions [activeCount]; sessions [activeCount] = null; break; } } } }
// Note: may only be called within the timeout thread void checkQueries() { long currentTime = (DateTime.Now.Ticks - 621355968000000000) / 10000; lock (this) { if (cancelList.Length < activeCount) { cancelList = new PrologSession[activeCount]; } // [PM] 4.1.3 FIXME: assert cancelCount == 0; for (int i = 0; i < activeCount; i++) { PrologSession sess = sessions [i]; int timeout = sess.Timeout; long startTime = sess.QueryStartTime; if (currentTime > (startTime + timeout)) { activeCount--; sessions [i] = sessions [activeCount]; sessions [activeCount] = null; if (startTime > 0L && timeout > 0) { // The query has taken too long and need to be cancelled cancelList [cancelCount++] = sess; } // Since we might have moved one session to this index it // will need to be rechecked again. i--; } } } if (cancelCount > 0) { // Notify all sessions that need to be cancelled. This should // not be done synchronized in case the cancellation takes time // and we do not want new queries to be blocked. for (int i = 0; i < cancelCount; i++) { Console.Error.WriteLine("PBMonitor: need to interrupt read/write!"); cancelList [i].cancelQuery(); cancelList [i] = null; } cancelCount = 0; } }
public void Run() { do { try { Thread.Sleep(oneSecond); checkQueries(); } catch (Exception e) { if (PrologSession.debugging()) { Console.Error.WriteLine("PBMonitor: monitor caught an exception:"); PrologSession.WriteStackTrace(e, Console.Error); } } } while (true); }
// PrologSession constructor PrologSession(PrologSession parent, string sessionID) { parentSession = parent; prologSessionID = sessionID; }