Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

Messaging.cpp

Go to the documentation of this file.
00001 /*
00002  * Messaging.cpp
00003  *
00004  * Part of Fly! Legacy project
00005  *
00006  * Copyright 2003 Chris Wallace
00007  *
00008  * Fly! Legacy is free software; you can redistribute it and/or modify
00009  *   it under the terms of the GNU General Public License as published by
00010  *   the Free Software Foundation; either version 2 of the License, or
00011  *   (at your option) any later version.
00012  *
00013  * Fly! Legacy is distributed in the hope that it will be useful,
00014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  *   GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  *   along with Fly! Legacy; if not, write to the Free Software
00020  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  */
00022 
00035 #include "../Include/FlyLegacy.h"
00036 #include "../Include/Globals.h"
00037 #include "../Include/Situation.h"
00038 #include "../Include/Subsystems.h"
00039 #include "../Include/UserVehicles.h"
00040 
00041 using namespace std;
00042 
00043 static EMessageResult SendMessageToAmpSystems (SMessage *msg, CElectricalSystem *amp)
00044 {
00045   EMessageResult rc = MSG_IGNORED;
00046   
00047   if (amp) {
00048     // Electrical systems are valid, send msg to each subs until handler found
00049     vector<CSubsystem*>::iterator i;
00050     for (i=amp->subs.begin(); i!=amp->subs.end(); i++) {
00051       CSubsystem *s = *i;
00052       rc = s->ReceiveMessage (msg);
00053       if (rc == MSG_PROCESSED) {
00054         // Receiver found. Update msg struct so that subsequent
00055         //   calls to SendMessage do not require lookup
00056         msg->receiver = s;
00057       }
00058     }
00059   }
00060 
00061   return rc;
00062 }
00063 
00064 static EMessageResult SendMessageToGasSystems (SMessage *msg, CFuelSystem *gas)
00065 {
00066   EMessageResult rc = MSG_IGNORED;
00067   
00068   if (gas) {
00069     // Fuel systems are valid, send msg to each subs until handler found
00070     vector<CFuelSubsystem*>::iterator i;
00071     for (i=gas->fsub.begin(); i!=gas->fsub.end(); i++) {
00072       CSubsystem *s = *i;
00073       rc = s->ReceiveMessage (msg);
00074       if (rc == MSG_PROCESSED) {
00075         // Receiver found. Update msg struct so that subsequent
00076         //   calls to SendMessage do not require lookup
00077         msg->receiver = s;
00078         break;
00079       }
00080     }
00081   }
00082 
00083   return rc;
00084 }    
00085 
00086 static EMessageResult SendMessageToAirplane (SMessage *msg, CAirplane *plan)
00087 {
00088   EMessageResult rc = MSG_IGNORED;
00089   
00090   if (plan) {
00091     // Try to find message receiver in list of electrical subsystems
00092     rc = SendMessageToAmpSystems (msg, plan->amp);
00093   }
00094   
00095   // If not found, try to find message receiver in list of fuel subsystems
00096   if (msg->receiver == NULL) {
00097     rc = SendMessageToGasSystems (msg, plan->gas);
00098   }
00099   
00100   return rc;
00101 }
00102 
00103 EMessageResult SendMessage (SMessage *msg)
00104 {
00105   if (msg == NULL) return MSG_IGNORED;
00106 
00107   // Assume message has no handler by default
00108   EMessageResult rc = MSG_IGNORED;
00109 
00110   if (msg->receiver != NULL) {
00111     // Send the message to the known recipient
00112     CSubsystem *subs = (CSubsystem *)msg->receiver;
00113     rc = subs->ReceiveMessage (msg);
00114   } else {
00115     // Search all subsystems until a subsystem returns MSG_PROCESSED.
00117     CVehicleObject *vehi = globals->sit->GetUserVehicle ();
00118     EFlyObjectType userType = globals->sit->GetUserVehicleType ();
00119     switch (userType) {
00120     case TYPE_FLY_AIRPLANE:
00121       {
00122         CAirplaneObject *planObj = (CAirplaneObject *)vehi;
00123         CAirplane *plan = planObj->pAirplane;
00124         rc = SendMessageToAirplane (msg, plan);
00125       }
00126       break;
00127       
00128     default:
00129       // Unsupported user vehicle type; do nothing
00130       ;
00131     }
00132 
00134   }
00135 
00136   // Display warning message if message receiver not found
00137   if (rc == MSG_IGNORED) {
00138     char id_string[8];
00139     char group_string[8];
00140     char dtag_string[8];
00141     TagToString (id_string, msg->id);
00142     TagToString (group_string, msg->group);
00143     TagToString (dtag_string, msg->user.u.datatag);
00144     globals->logWarning->Write ("SendMessage: No rcvr: id=%s group=%s, dtag=%s (0x%08X)",
00145       id_string, group_string, dtag_string, msg->user.u.datatag);
00146   }
00147 
00148   msg->result = rc;
00149 
00150   return rc;
00151 }
SourceForge.net Logo Documentation generated by doxygen