Example #1
0
        internal string Request(string req, ref object answer)
        {
            string ret = "";

            lock (myLock) {
                if (this.Connection != null && this.Connection.Valid && !this.stopQueued)
                {
                    try {
                        Communication.GenericRequest request = new Communication.GenericRequest {
                            Command = req
                        };
                        Communication.Response res = Connection.Send(request);
                        if (!string.IsNullOrWhiteSpace(res.Error))
                        {
                            ret = res.Error;
                            ParentForm.listBoxLog.Log(Util.Level.Error, string.Format("MegaMol did not accept {0}: {1}", req.ToString(), res.Error));
                            SetProcessState(MegaMolProcessState.MMPS_CONNECTION_BROKEN); // broken
                            //if (!res.Error.StartsWith("NOTFOUND")) {
                            //    MessageBox.Show(string.Format("MegaMol did not accept {0}", req.ToString()), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            //    SetProcessState(MegaMolProcessState.MMPS_CONNECTION_BROKEN); // broken
                            //} else {
                            //    SetProcessState(MegaMolProcessState.MMPS_CONNECTION_BROKEN); // broken
                            //}
                        }
                        else
                        {
                            // problem: slow megamol might re-order answers. so if we continue listening no matter what,
                            // we will get answers from other requests which blows us up
                            answer = res.Answer;
                            SetProcessState(MegaMolProcessState.MMPS_CONNECTION_GOOD); // good
                        }
                    } catch (Exception ex) {
                        if (!stopQueued)
                        {
                            // todo set this to false when message ordering really works (see above)
#if false
                            MessageBox.Show("Failed to send: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            ret = ex.Message;
                            this.StopObserving();
                            this.Connection.Close();
                            this.Connection = null;
#endif
                            ParentForm.listBoxLog.Log(Util.Level.Error, string.Format("Exception with request {0} in flight: {1}", req.ToString(), ex.Message));
                            SetProcessState(MegaMolProcessState.MMPS_CONNECTION_BROKEN); // broken
                            ret             = ex.Message;
                            this.Connection = null;
                            TryConnecting(this.connectionString);
                        }
                    }
                }
            }
            return(ret);
        }