3535 *
3636 */
3737
38+ #ifdef WIN32
39+ #include < winsock2.h>
40+ #else
41+ #include < sys/socket.h>
42+ #include < netinet/in.h>
43+ #include < arpa/inet.h>
44+ #include < unistd.h>
45+ #endif
46+
3847#include " TLMCoSimulationDialog.h"
3948#include " TLMCoSimulationOutputWidget.h"
4049#include " VariablesWidget.h"
@@ -60,7 +69,7 @@ TLMCoSimulationDialog::TLMCoSimulationDialog(MainWindow *pMainWindow)
6069 // manager monitor port
6170 mpMonitorPortLabel = new Label (tr (" Monitor Port:" ));
6271 mpMonitorPortLabel->setToolTip (tr (" Set the port for monitoring connections" ));
63- mpMonitorPortTextBox = new QLineEdit (" 5010 " );
72+ mpMonitorPortTextBox = new QLineEdit (" 12111 " );
6473 // interface request mode
6574 mpInterfaceRequestModeCheckBox = new QCheckBox (tr (" Interface Request Mode" ));
6675 mpInterfaceRequestModeCheckBox->setToolTip (tr (" Run manager in interface request mode, get information about interface locations" ));
@@ -177,6 +186,10 @@ bool TLMCoSimulationDialog::validate()
177186 return true ;
178187}
179188
189+ /* !
190+ * \brief TLMCoSimulationDialog::createTLMCoSimulationOptions
191+ * \return
192+ */
180193TLMCoSimulationOptions TLMCoSimulationDialog::createTLMCoSimulationOptions ()
181194{
182195 TLMCoSimulationOptions tlmCoSimulationOptions;
@@ -200,8 +213,26 @@ TLMCoSimulationOptions TLMCoSimulationDialog::createTLMCoSimulationOptions()
200213 managerArgs.append (" -m" );
201214 managerArgs.append (mpMonitorPortTextBox->text ());
202215 // set monitor server:port for monitor process
203- QString monitorPort = " 130.236.182.49:" ;
204- monitorPort.append (mpMonitorPortTextBox->text ());
216+ #define MAXHOSTNAME 1024
217+ char myname[MAXHOSTNAME+1 ];
218+ struct hostent *hp;
219+ #ifdef WIN32
220+ WSADATA ws;
221+ int d;
222+ d = WSAStartup (0x0101 ,&ws);
223+ #endif
224+ gethostname (myname, MAXHOSTNAME);
225+ hp = gethostbyname ((const char *) myname);
226+ if (hp == NULL ) {
227+ MessageItem messageItem (MessageItem::TLM, " " , false , 0 , 0 , 0 , 0 ,
228+ tr (" Failed to get my hostname, check that name resolves, e.g. /etc/hosts has %1" )
229+ .arg (QString (myname)), Helper::scriptingKind, Helper::errorLevel);
230+ mpMainWindow->getMessagesWidget ()->addGUIMessage (messageItem);
231+ tlmCoSimulationOptions.setIsValid (false );
232+ return tlmCoSimulationOptions;
233+ }
234+ char * localIP = inet_ntoa (*(struct in_addr *)*hp->h_addr_list );
235+ QString monitorPort = QString (localIP) + " :" + mpMonitorPortTextBox->text ();
205236 monitorArgs.append (monitorPort);
206237 }
207238 if (!mpServerPortTextBox->text ().isEmpty ()) {
@@ -227,15 +258,21 @@ TLMCoSimulationOptions TLMCoSimulationDialog::createTLMCoSimulationOptions()
227258 return tlmCoSimulationOptions;
228259}
229260
261+ /* !
262+ * \brief TLMCoSimulationDialog::simulate
263+ * Starts the TLM co-simulation
264+ */
230265void TLMCoSimulationDialog::simulate ()
231266{
232267 if (validate ()) {
233268 TLMCoSimulationOptions tlmCoSimulationOptions = createTLMCoSimulationOptions ();
234- TLMCoSimulationOutputWidget *pTLMCoSimulationOutputWidget = new TLMCoSimulationOutputWidget (tlmCoSimulationOptions, mpMainWindow);
235- int xPos = QApplication::desktop ()->availableGeometry ().width () - pTLMCoSimulationOutputWidget->frameSize ().width () - 20 ;
236- int yPos = QApplication::desktop ()->availableGeometry ().height () - pTLMCoSimulationOutputWidget->frameSize ().height () - 20 ;
237- pTLMCoSimulationOutputWidget->setGeometry (xPos, yPos, pTLMCoSimulationOutputWidget->width (), pTLMCoSimulationOutputWidget->height ());
238- pTLMCoSimulationOutputWidget->show ();
239- accept ();
269+ if (tlmCoSimulationOptions.isValid ()) {
270+ TLMCoSimulationOutputWidget *pTLMCoSimulationOutputWidget = new TLMCoSimulationOutputWidget (tlmCoSimulationOptions, mpMainWindow);
271+ int xPos = QApplication::desktop ()->availableGeometry ().width () - pTLMCoSimulationOutputWidget->frameSize ().width () - 20 ;
272+ int yPos = QApplication::desktop ()->availableGeometry ().height () - pTLMCoSimulationOutputWidget->frameSize ().height () - 20 ;
273+ pTLMCoSimulationOutputWidget->setGeometry (xPos, yPos, pTLMCoSimulationOutputWidget->width (), pTLMCoSimulationOutputWidget->height ());
274+ pTLMCoSimulationOutputWidget->show ();
275+ accept ();
276+ }
240277 }
241278}
0 commit comments