observing for subset of event using jtapi provider - cisco-jtapi

I am using cisco jtapi v7+ and investigating whether I can add a provider to only listen to certain (not all) events. However, the only call I see in the API is the following:
provider.addObserver(ProviderObserver);
I would like to avoid filtering events in my application and have it done through the API. Any thoughts/insight on this would be appreciated!

You must add CallObserver instance to address which you want listen, then filtering events. For example:
Address srcAddr = provider.getAddress(src);
co = new CallObserver() {
public void callChangedEvent(CallEv[] eventList) {
for (int i = 0; i < eventList.length; ++i) {
try {
if (eventList[i].getID() == TermConnRingingEv.ID) {
session.getBasicRemote().sendText("new_call");
}
} catch (Exception ex) {
ex.printStackTrace();
}
if (eventList[i].getID() == ConnDisconnectedEv.ID) {
try {
System.out.println("Disconnected");
session.getBasicRemote().sendText("disconnected");
} catch (Exception ex) {
ex.printStackTrace();
}
}
if (eventList[i] instanceof CallObservationEndedEv) {
System.out.println("Event: Call Observation Ended");
}
if (eventList[i] instanceof CiscoAddrOutOfServiceEv) {
System.out.println("Event: Address Out of service");
}
System.out.println("State: " + eventList[i].getCall().getState());
}
}
};
srcAddr.addCallObserver(co);

Related

How would I delete a subscribed item in Eclipse Milo 0.3.8?

I have looked at the examples for subscribing to a NodeId and im wondering how I could stop/delete a subscription afterward.
Eclipse Milo v0.3.8 Client.
Here's what I've tried.
protected boolean unsubscribe(TransactionDefinition transactionDefinition) {
// Finds the mathing TransactionDefinition from the map where all subscriptions
// are stored, together with the clientHandle.
// private Map<UInteger, TransactionDefinition> subscriptions = new HashMap<>();
try {
UInteger subscriptionClientHandle = null;
for (Map.Entry<UInteger, TransactionDefinition> entry : subscriptions.entrySet()) {
if (entry.getValue().equals(transactionDefinition))
subscriptionClientHandle = entry.getKey();
}
if (subscriptionClientHandle == null) return false;
try {
client.getSubscriptionManager().deleteSubscription(subscriptionClientHandle).get();
return true;
} catch (Exception e) {
log.error("Subscription not found: {}", e.getMessage(), e.getCause());
e.printStackTrace();
}
} catch (ClassCastException e) {
log.error("TransactionDefinition trigger not found. {}", e.getMessage(), e.getCause());
e.printStackTrace();
}
return false;
}
UaSubscription has a deleteMonitoredItems method on it.
UaSubscriptionManager has a deleteSubscription method on it.
You could also call either of these services "manually" by invoking the "raw" service methods on the UaClient instance.

Whether to use Flowable.create or Flowable.generate to make a file observable?

From https://github.com/ReactiveX/RxJava/wiki/Backpressure-(2.0)
the following snippet is provide to indicate correct usage for RXified file reading.
Flowable<Integer> o = Flowable.generate(
() -> new FileInputStream("data.bin"),
(inputstream, output) -> {
try {
int byte = inputstream.read();
if (byte < 0) {
output.onComplete();
} else {
output.onNext(byte);
}
} catch (IOException ex) {
output.onError(ex);
}
return inputstream;
},
inputstream -> {
try {
inputstream.close();
} catch (IOException ex) {
RxJavaHooks.onError(ex);
}
}
);
I am doing the following
public Flowable<byte[]> createFlowable(File file) {
return Flowable.create(source -> {
try (FileInputStream fin = new FileInputStream(file);
BufferedInputStream in = new BufferedInputStream(fin)) {
while (in.available() > 0) {
byte[] data = getMessageRawData(in);
source.onNext(data);
}
source.onComplete();
}
catch (Exception ex) {
source.onError(ex);
}
}, BackpressureStrategy.BUFFER);
}
Does my code (uses try with resource) suffer from resource leakage if dispose is called mid way or what other side effects can be expected or is it just a different way of doing things?

addautomtioneventhandler on web pages

I'm trying to add an automation event handler for a ui button. I've tested on desktop applications and it's working but I've a problem with buttons in web pages.In fact I can invoke them with InvokePattern.Invoke() but I don't see the handler working after that!
this is my code :
if ((wantedElement != null))
{
element = wantedElement;
buttonEvent = new AutomationEventHandler(close_event);
Automation.AddAutomationEventHandler(InvokePattern.InvokedEvent, element,TreeScope.Element,buttonEvent );
object pattern;
if (wantedElement.TryGetCurrentPattern(InvokePattern.Pattern, out pattern))
{
InvokePattern invokePattern = (InvokePattern)pattern;
invokePattern.Invoke();
}
}
...
private static void close_event(object sender, AutomationEventArgs e)
{
AutomationElement sourceElement;
try
{
sourceElement = sender as AutomationElement;
Console.WriteLine("button try");
}
catch (ElementNotAvailableException)
{
return;
}
if (e.EventId == InvokePattern.InvokedEvent)
{
// TODO Add handling code.
Console.WriteLine("button invoked");
}
else
{
// TODO Handle any other events that have been subscribed to.
Console.WriteLine("button event");
}
}

Write to Event Log - The source was not found, but some or all event logs could not be searched. Inaccessible logs: Security."

I'am trying to write some messages to Windows Event log.
The (security) exception will be thrown when calling function "SourceExists()".
private bool CheckIfEventLogSourceExits()
{
try
{
if (!EventLog.SourceExists(this.BaseEventLog))
{
return false;
}
return true;
}
catch (System.Security.SecurityException)
{
return false;
}
}
All answers to this question are explaining how you can MANUALLY resolve issue.
Like here: Stackoverflow Thread. Solution would be changing some registry keys
But you can't expect that everyone who consumes your application is aware of these changes.
So my question is, how can we solve this issue programmatically?
Below my code:
try
{
string sLog = "Application";
if (CheckIfEventLogSourceExits())
{
EventLog.CreateEventSource(this.BaseEventLog, sLog);
}
EventLog.WriteEntry(this.BaseEventLog, message, eventLogEntryType);
}
catch (Exception ex)
{
ex.Source = "WriteToEventLog";
throw ex;
}
I know it's too late for this posting, but the answer, I found from similar experience, is that the service you're running under doesn't have administrative rights to the machine and, thus, can't write to the logs.
It's easy enough to figure out if an app is being run under admin rights. You can add something like this to your code with a message box advising the user to run "admin".
private void GetServicePermissionLevel()
{
bool bAdmin = false;
try {
SecurityIdentifier sidAdmin = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null);
AppDomain myDomain = Thread.GetDomain();
myDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
WindowsPrincipal myPrincipal = (WindowsPrincipal)Thread.CurrentPrincipal;
if (myPrincipal.IsInRole(sidAdmin)) {
bAdmin = true;
} else {
bAdmin = false;
}
} catch (Exception ex) {
throw new Exception("Error in GetServicePermissionlevel(): " + ex.Message + " - " + ex.StackTrace);
} finally {
_ServiceRunAsAdmin = bAdmin;
}
}

Record store in lwuit

Does LWUIT have an implementation for j2me record store?
I have made a lwuit app which uses j2me recordstore. However, I feel that using lwuit api without jumping into j2me api would help make the app more portable. Am I right?
Thanks
Yes, LWUIT has a record store. Check Storage Class of LWUIT IO, but this is only available in LWUIT 1.5.
Yes, it would make it more portable.
You can use J2ME record store for this i.e. RMS record management store.
You no need LWUIT for RS
public static void writeDataToRecordStore(byte[] aData, final String aDir) {
if (aData == null) {
return;
}
RecordStore iRecord = null;
try {
iRecord = RecordStore.openRecordStore(aDir, true);
if (iRecord.getNumRecords() > 0) {
iRecord.setRecord(1, aData, 0, aData.length);
} else {
iRecord.addRecord(aData, 0, aData.length);
}
} catch (Exception ex) {
} finally {
if (iRecord != null) {
try {
iRecord.closeRecordStore();
} catch (Exception ex) {
}
iRecord = null;
}
}
}
/**
* Read data from Record Store
*
* #param aDir
* #return
*/
public static byte[] readDataFromRecordStore(final String aDir) {
RecordStore iReccord = null;
try {
iReccord = RecordStore.openRecordStore(aDir, false);
if (iReccord.getNumRecords() > 0) {
// Has data
return iReccord.getRecord(1);
}
} catch (Exception otherEx) {
} finally {
if (iReccord != null) {
try {
iReccord.closeRecordStore();
} catch (Exception ex1) {
}
}
}
return null;
}
Lwuit using Record Store in HTML Component.
And I think, using record store is not related with LWUIT. They both separate function. LWUIT is for User Interface / View, record store is for data / model.