Example #1
0
/** Cancel an ASIM from the SDM system.
 * 
 * Cancel cancels an ASIM from the SDM system by sending an SDMCancelxTEDS 
 * message to the DataManager.
 *
 * INPUTS:
 *  None.
 * RETURNS:
 *  @return void
 */
void Sensor::Cancel()
{
  if(debug_level>=2)
    printf("Canceling xTEDS for %s\n",device_name);

  SDMCancelxTEDS cancel;
  cancel.source = sensor_id;
  cancel.Send();
  MessageSent(&cancel);

  pthread_mutex_lock(&sensor_mutex);
  connected = false;
  subscriptions.ClearAllSubscriptions();
  pthread_mutex_unlock(&sensor_mutex);
}
Example #2
0
/** ReRegister the ASIM.
 * ReRegister re-registers an ASIM with the SDM by resending its xTEDS.  A 
 * device is reregistered when the DataManager fails and a new DM has been 
 * elected.
 *
 * INPUTS:
 *  None.
 * RETURNS:
 *  @return void
 */
void Sensor::ReRegister()
{
  //Reregister the xTEDS of the current device
  SDMxTEDS xteds_msg;
  if (m_Ready)
  {
    strcpy(xteds_msg.SPA_node, device.USBLocation());
    xteds_msg.source = sensor_id;
    strcpy(xteds_msg.xTEDS, device_xTEDS);
    xteds_msg.Send();
    MessageSent(&xteds_msg);
  }
  //Cancel all subscriptions, which are no longer applicable
  pthread_mutex_lock(&sensor_mutex);
  subscriptions.ClearAllSubscriptions();
  pthread_mutex_unlock(&sensor_mutex);
}
Example #3
0
status_t
Jabber::Process( BMessage * msg )
{
	switch ( msg->what )
	{
		case IM::MESSAGE:
		{
			int32 im_what = 0;
			
			msg->FindInt32("im_what", &im_what );
						
			switch ( im_what )
			{
				case IM::SET_STATUS:
				{
					const char *status = msg->FindString("status");
					LOG(kProtocolName, liMedium, "Set status to %s", status);
					
					if (strcmp(status, OFFLINE_TEXT) == 0) 
					{
							
						SetStatus(S_OFFLINE,OFFLINE_TEXT); //do the log-out?
					} 
					else 
					if (strcmp(status, AWAY_TEXT) == 0) 
					{
						if(IsAuthorized()){
						
						 //const char *away_msg;	
						 BString away_msg;					 
						 if(msg->FindString("away_msg",&away_msg) == B_OK)
						 {
						 	// quick and dirty way to use advanced away status:
						 	// add 'DND: ' for Do not Disturb
						 	// or  'XA: ' for Extended Away
						 	
						 	if(away_msg.Compare("DND: ",4) == 0)
						 		SetStatus(S_DND,away_msg); 
						 	else
						 	if(away_msg.Compare("XA: ",4) == 0)
						 		SetStatus(S_XA,away_msg); 
						 	else	
						 		SetStatus(S_AWAY,away_msg); 
						 }
						 	 else
						 		 SetStatus(S_AWAY,AWAY_TEXT); 
						 
						 			
						 SetAway(true);
						} 
					} 
					else 
					if (strcmp(status, ONLINE_TEXT) == 0) 
					{
							if(!IsAuthorized())
							{
								if(fUsername == "")
									Error("Empty Username!",NULL);
								if(fServer == "")
									Error("Empty Server!",NULL);
								if(fPassword == "")
									Error("Empty Password!",NULL);
								
								Progress("Jabber Login", "Jabber: Connecting..", 0.0);
										
							}
							
							SetStatus(S_ONLINE,ONLINE_TEXT); //do the login!
							if(IsAuthorized()) SetAway(false); 
					} 
					else
					{
						Error("Invalid",NULL);
						LOG(kProtocolName, liHigh, "Invalid status when setting status: '%s'", status);
					}
				}	break;
				
				case IM::SEND_MESSAGE:
				{
						const char * buddy=msg->FindString("id");
						const char * sms=msg->FindString("message");
						JabberMessage jm;
						jm.SetTo(buddy);
						jm.SetFrom(GetJid());
						jm.SetBody(sms);
						TimeStamp(jm);
						
						//not the right place.. see Jabber::Message
						JabberContact *contact=getContact(buddy);
						
						//tmp: new mess id!
						BString messid("imkit_");
						messid << idsms;
						idsms++;
						
						if(contact)
							jm.SetID(messid);
												
						SendMessage(jm);
						
						MessageSent(buddy,sms);
				} 
				break;
				case IM::REGISTER_CONTACTS:
					
					{
					
					//debugger("REGISTER");
					type_code garbage;
					int32 count = 0;
					msg->GetInfo("id", &garbage, &count);
					
										
					if (count > 0 ) {
						
						for ( int i=0; msg->FindString("id",i); i++ )
						{
							const char * id = msg->FindString("id",i);
							JabberContact* contact=getContact(id);
							if(contact)
								  BuddyStatusChanged(contact);
							else
							{
							 
								//Are we on-line?
								// send auth req?
								if(fFullLogged)
								{ 			
									AddContact(id,id,"");
									BuddyStatusChanged(id,OFFLINE_TEXT);
								}
								
								else
								{
								 // we add to a temp list.
								 // when logged in we will register the new buddy..
									 fLaterBuddyList->push_back(BString(id));
								}							 
							} 
						};
						
					} 
					else 
						return B_ERROR;
					} 	
					break;
				case IM::UNREGISTER_CONTACTS:
				
				{
						
					
						const char * buddy=NULL;
						
						for ( int i=0; msg->FindString("id", i, &buddy) == B_OK; i++ )
						{
							LOG(kProtocolName, liDebug, "Unregister Contact: '%s'", buddy);
							
							if(!fFullLogged)
							BuddyStatusChanged(buddy,OFFLINE_TEXT);
							else
							{
								LOG(kProtocolName, liDebug, "Unregister Contact DOING IT");
							 	JabberContact* contact=getContact(buddy);
							 	if(contact)
									RemoveContact(contact);
							}
						}
				} 
					
					break;
				case IM::USER_STARTED_TYPING: 
				{
						const char * id=NULL;
						
						if( msg->FindString("id", &id) == B_OK )
						{
						 JabberContact* contact=getContact(id);
						 if(contact)
							StartComposingMessage(contact);
						}
				} 
				break;
				case IM::USER_STOPPED_TYPING: 
				{
						const char * id=NULL;
						
						if( msg->FindString("id", &id) == B_OK )
						{
						 JabberContact* contact=getContact(id);
						 if(contact && (contact->GetLastMessageID().ICompare("")!=0)){
							StopComposingMessage(contact);
							contact->SetLastMessageID("");
							
							}
						}
				} 
				break;
				
				case IM::GET_CONTACT_INFO:
					//debugger("Get Contact Info! ;)");
					SendContactInfo(msg->FindString("id"));
				break;
								
				case IM::SEND_AUTH_ACK:
				{
					if(!IsAuthorized())
						return B_ERROR;
					
					const char * id = msg->FindString("id");
					int32 button = msg->FindInt32("which");
					
					if (button == 0) {
						
						//Authorization granted
						AcceptSubscription(id);
						BMessage im_msg(IM::MESSAGE);
						im_msg.AddInt32("im_what", IM::CONTACT_AUTHORIZED);
						im_msg.AddString("protocol", kProtocolName);
						im_msg.AddString("id", id);
						im_msg.AddString("message", "");
						fServerMsgr.SendMessage(&im_msg);
						
						//now we want to see you! ;)
						AddContact(id,id,"");
														
					} 
					else 
					{
						//Authorization rejected
						Error("Authorization rejected!",id);
					}
						
					
				}
				break;
				default:
					// we don't handle this im_what code
					msg->PrintToStream();	
					return B_ERROR;
			}
		}	break;
		
		default:
			// we don't handle this what code
			return B_ERROR;
	}
	
	return B_OK;
}
Example #4
0
/** Read data from connected ASIM.
 *
 *  Read attempts to read data from the connected ASIM.  If the message is data,
 *  it is published to all interested subscribers, otherwise
 *  the message received is printed to the screen (if debug is high enough).
 *
 * INPUTS:
 * @param buf    [output] - The buffer in which to store the message.
 * @param length [output] - The number of bytes stored in buf.
 *
 * RETURNS:
 * @return char - The type of ASIM message received.
 */
char Sensor::Read(unsigned short& length, unsigned char* buf, int bufsize)
{
  char msg_type;
  int error_code;
  char error_str[80];
  int i;

  if(!connected)
    return ASIM_ERROR;

  memset(buf, 0, bufsize);	//Clear out the message buffer
  msg_type = device.Read(length,buf,bufsize);
  error_code = errno;

  switch(msg_type)
  {
  case ASIM_STATUS:
    if (buf[0]&0x80) //first bit is set for an error
    {
      if(debug_level>=0)
      {
	printf("ASIM %s status ERROR: %hhd\n",device_name,buf[0]);
      }
      if(debug_level>=2)
      {
	if(buf[0]&0x40) //illegal command bit
	{
	  printf("\tIllegal or unrecognized command\n");
	}
	if(buf[0]&0x20) //self test failure bit
	{
	  printf("\tSelf Test failed\n");
	}
      }
    }
    else
    {
      if(debug_level>=1)
      {
	printf("ASIM %s status OK: %hhd\n",device_name,buf[0]);
      }
    }
    if(debug_level>=2)
    {
      if(buf[0]&0x10) //mode bit
      {
	printf("\tASIM mode: operational\n");
      }
      else
      {
	printf("\tASIM mode: idle\n");
      }
    }
    break;

  case ASIM_XTEDS:
    if(debug_level>=1)
    {
      printf("%s xTEDS (%d)\n",device_name,length);
      fflush(NULL);
      if(debug_level>=2)
      {
	for(i=0;i<length;++i)
	{
	  if(isprint(buf[i])||isspace(buf[i]))
	  {
	    printf("%c",buf[i]);
	  }
	  else
	  {
	    printf("\nERROR %s xTEDS contains invalid character 0x%x\n",
		   device_name,buf[i]);
	  }
	}
      }
    }
    break;

  case ASIM_XTEDS_ID_PAIR:
    if(debug_level>=1)
    {
      printf("%s xTEDS_PID (%d)\n",device_name,length);
      fflush(NULL);
      if(debug_level>=2)
      {
	printf(" (ID=0x");
	for(i = 0; i < (length > 4 ? 4 : length);++i)
	  printf("%x",buf[i]);
	printf(")\n");
	for( ;i<length;++i)
	{
	  if(isprint(buf[i])||isspace(buf[i]))
	  {
	    printf("%c",buf[i]);
	  }
	  else
	  {
	    printf("\nWARNING: %s xTEDS contains invalid character 0x%x\n",
		   device_name,buf[i]);
	  }
	}
      }
    }
    break;

  case ASIM_DATA:
    if(debug_level>=2)
    {
      printf("%s produced message %hhd\n",device_name,buf[0]);
    }
    pthread_mutex_lock(&sensor_mutex);
    if(!subscriptions.Publish(buf[0],buf[1],(char*)(buf+2),length-2))
    {
      if(debug_level>=1)
      {
	printf("\t%s:%hhd had no subscribers\n",device_name,buf[0]);
	printf("\tCanceling %s:%hhd\n",device_name,buf[0]);
      }
      device.Cancel(buf[0], buf[1]);
    }
    else
    {
      MessageSent(subscriptions.GetLastPublished());
    }
    pthread_mutex_unlock(&sensor_mutex);
    break;

  case ASIM_TIME_AT_TONE:
  {
    // A modification for Fronterhouse.  The ASI protocol doesn't define an 
    // ASIM producing a Time-At-Tone, however, the GPS SPA-U ASIM will.  This 
    // will send it to the SM, which will in turn send it to all ASIMs connected
    // to the SM.

    SDMTat TATMsg;
    char SendBuffer[BUFSIZE];

    TATMsg.destination.setSensorID(0);	// Send to all ASIMs
    TATMsg.seconds = GET_UINT(buf);
    TATMsg.useconds = GET_UINT(buf+4);

    int TATLength = TATMsg.Marshal(SendBuffer);
    if (TATLength < 0)
    {
      printf("Error marshalling TAT message.\n");
      return ASIM_ERROR;
    }

    // Send to the SensorManager listen thread
    if (UDPsendto("127.0.0.1", PORT_SM, SendBuffer, TATLength) < 0)
    {
      printf("Error sending TAT message.\n");
      return ASIM_ERROR;
    }
  }
  break;

  case ASIM_VERSION:
    if(debug_level>=1)
      printf("%s running ASIM version %hhd\n",device_name,buf[0]);
    break;

  case ASIM_ERROR:
    if(debug_level>=0)	//display errors
    {
      printf("%s read error: %s\n",device_name,strerror_r(error_code,error_str,80));
    }
    break;

  case ASIM_TIMEOUT:
    if(debug_level>=3)	//only display timeout errors on debug 3
    {
      printf("%s read timeout: %s\n",
	     device_name,strerror_r(error_code,error_str,80));
    }
    break;

#ifdef WIN32
  case NULL:	/*Default return for no message available in Win32*/
    usleep(10000);		/*Give time for a message to be produced*/
    break;
#endif
  default:
    if(debug_level>=0)
      printf("%s produced an undefined message 0x%x\n",device_name,msg_type);
    break;
  }
  return msg_type;
}
Example #5
0
void
SMTPMessage::ReadReturnValue()
{
	JString line;
	JBoolean ok = itsLink->GetNextMessage(&line);
	assert(ok);

	if (itsCurrentMode != kDataHeader)
		{
		GMGetSMTPDebugDir()->AddText(line);
		}

	if ((itsCurrentMode != kStartUp) && (itsCurrentMode != kDataHeader))
		{
		JInteger value;
		JIndex findex;
		ok = line.LocateSubstring(" ", &findex);
		if (ok && (findex > 1))
			{
			JString number = line.GetSubstring(1, findex - 1);
			if (number.IsInteger())
				{
				number.ConvertToInteger(&value);
				}
			else
				{
				ok	= kJFalse;
				}
			}
		else
			{
			ok	= kJFalse;
			}

		if (!ok)
			{
			const JIndex kDashIndex	= 4;
			if (line.GetCharacter(kDashIndex) == '-')
				{
				// this is a multiline response.
				return;
				}
			}

		if (!ok || (value != kOKValue))
			{
			if (!itsIsTryingToQuit)
				{
				const JCharacter* map[] =
					{
					"err", line
					};
				const JString msg = JGetString("SMTPERROR", map, sizeof(map));

				JGetUserNotification()->ReportError(msg);
				}
			itsIsFinished = kJTrue;
			Broadcast(SendFailure());
			if (!itsIsTryingToQuit)
				{
				itsDeleteTask = new JXTimerTask(1000,kJTrue);
				assert( itsDeleteTask != NULL );
				itsDeleteTask->Start();
				ListenTo(itsDeleteTask);
				}
			return;
			}
		}

	if (itsCurrentMode < kTo)
		{
		itsCurrentMode++;
		}
	else if (itsCurrentMode == kTo)
		{
		if (itsCurrentIndex < itsToNames->GetElementCount())
			{
			itsCurrentIndex++;
			}
		else if (itsCcNames->GetElementCount() != 0)
			{
			itsCurrentMode = kCc;
			itsCurrentIndex = 1;
			}
		else if (itsBccNames->GetElementCount() != 0)
			{
			itsCurrentMode = kBcc;
			itsCurrentIndex = 1;
			}
		else
			{
			itsCurrentMode = kDataHeader;
			}
		}
	else if (itsCurrentMode == kCc)
		{
		if (itsCurrentIndex < itsCcNames->GetElementCount())
			{
			itsCurrentIndex++;
			}
		else if (itsBccNames->GetElementCount() != 0)
			{
			itsCurrentMode = kBcc;
			itsCurrentIndex = 1;
			}
		else
			{
			itsCurrentMode = kDataHeader;
			}
		}
	else if (itsCurrentMode == kBcc)
		{
		if (itsCurrentIndex < itsBccNames->GetElementCount())
			{
			itsCurrentIndex++;
			}
		else
			{
			itsCurrentMode = kDataHeader;
			}
		}
	else if (itsCurrentMode == kData)
		{
		itsIsFinished = kJTrue;
		Broadcast(MessageSent());
		GMGetSMTPDebugDir()->AddText("\n-------------------------------------\n");
		if (!itsIsTryingToQuit)
			{
			itsDeleteTask = new JXTimerTask(1000,kJTrue);
			assert( itsDeleteTask != NULL );
			itsDeleteTask->Start();
			ListenTo(itsDeleteTask);
			}
		return;
		}
	else
		{
		itsCurrentMode++;
		}

	SendNextData();
}