Hector Miuler Malpica Gallegos avatar Hector Miuler Malpica Gallegos committed cca6cd9

Se agrego un hilo nuevo usando la misma libreria de qt para poder mandar mensaejes entre hilos (netty->gui)

Comments (0)

Files changed (3)

src/main/scala/com/miuler/mocksmtp/core/MockSMTP.scala

 import io.netty.handler.codec.string.{StringEncoder, StringDecoder}
 import io.netty.handler.codec.{DelimiterBasedFrameDecoder, Delimiters}
 import java.net.InetAddress
+import com.miuler.mocksmtp.gui.QConnectionThreadNetty
 
 
-class MockSMTP {
+class MockSMTP (connectionTheadNetty: QConnectionThreadNetty) {
 
   private val log = LoggerFactory.getLogger(classOf[MockSMTP])
 
           def initChannel(ch: SocketChannel) {
             ch.pipeline()
               .addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()(0)))
-              .addLast("decoder", new StringDecoder())
-              .addLast("encoder", new StringEncoder())
-              .addLast("handlerIn", new ChannelInboundHandlerBasic)
+              .addLast("decoder", new StringDecoder)
+              .addLast("encoder", new StringEncoder)
+              .addLast("handlerIn", new ChannelInboundHandlerBasic(connectionTheadNetty))
           }
         })
     server.bind().sync()
 }
 
 
-class ChannelInboundHandlerBasic extends ChannelInboundMessageHandlerAdapter[String] {
+class ChannelInboundHandlerBasic(connectionTheadNetty: QConnectionThreadNetty) extends ChannelInboundMessageHandlerAdapter[String] {
 
   private val log = LoggerFactory.getLogger(classOf[MockSMTP])
 
 
   override def messageReceived(ctx: ChannelHandlerContext, msg: String) {
     log.debug("mensage: \n{}", msg)
-
-    val hello = "(HELLO)".r
+    connectionTheadNetty.msg = msg
+    connectionTheadNetty.mutex.unlock
 
     msg match {
       case msg:String if msg.toUpperCase matches "QUIT" =>
   }
 }
 
+
+
+
+
+

src/main/scala/com/miuler/mocksmtp/gui/MainGUI.scala

 import com.trolltech.qt.gui._
 import com.miuler.mocksmtp.core.MockSMTP
 import org.slf4j.LoggerFactory
-import com.miuler.mocksmtp.utils.Utils.{stack2String, showMessageE}
-import java.net.SocketException
+import com.miuler.mocksmtp.utils.Utils.showMessageE
+import com.trolltech.qt.QThread
+import com.trolltech.qt.core.Qt.ConnectionType
 
 
 /**
 class MainWindowSMTP(id: String = null, serverId: String = null) extends QWidget {
 
   private val log = LoggerFactory.getLogger(classOf[MainGUI])
+  private val label = new QLabel()
 
-  var btnInitSMTP: QPushButton = _
-  var btnStopSMTP: QPushButton = _
-  val mockSMTP = new MockSMTP()
+  private var btnInitSMTP: QPushButton = _
+  private var btnStopSMTP: QPushButton = _
+  private var threadConnectionThreadNetty: QThread = _
+  private var mockSMTP: MockSMTP = _
+  private val connectionTheadNetty = new QConnectionThreadNetty
 
   initGUI
+  initConnectionThread
 
   private def initGUI {
     btnInitSMTP = new QPushButton()
     btnStopSMTP.toggled.connect(this, "stopSMTP()")
 
     val layout = new QVBoxLayout()
+    layout.addWidget(label)
     layout.addWidget(btnInitSMTP)
     layout.addWidget(btnStopSMTP)
 
     show()
   }
 
+  def initConnectionThread {
+    threadConnectionThreadNetty = new QThread (connectionTheadNetty)
+    connectionTheadNetty.moveToThread(threadConnectionThreadNetty)
+    connectionTheadNetty.messageSignal.connect(this, "addEmail(String)", ConnectionType.QueuedConnection)
+    threadConnectionThreadNetty.start
+    mockSMTP = new MockSMTP(connectionTheadNetty)
+  }
+
   def startSMTP() {
     try {
       if (btnInitSMTP.isChecked()) {
 
   override def closeEvent(evn: QCloseEvent) {
     log.debug("salir")
+    super.closeEvent(evn)
     if (btnInitSMTP.isChecked()) {
       log.debug("Apagando el servidor")
       mockSMTP.stop
     }
+    if (threadConnectionThreadNetty.isAlive) {
+      log.debug("threadConnectionThreadNetty isAlive")
+      //threadConnectionThreadNetty.
+      log.debug("threadConnectionThreadNetty isAlive: {}", threadConnectionThreadNetty.isAlive)
+    }
   }
+
+  def addEmail(msg: String) {
+    log.debug("Agregar email a la lista")
+    log.debug("msg: {}", msg)
+    label.setText(msg)
+  }
+
 }

src/main/scala/com/miuler/mocksmtp/gui/QConnectionThreadNetty.scala

+package com.miuler.mocksmtp.gui
+
+import com.trolltech.qt.core.{QMutex, QObject}
+import org.slf4j.{LoggerFactory, Logger}
+
+
+class QConnectionThreadNetty extends QObject with Runnable {
+
+  val log = LoggerFactory.getLogger(classOf[QConnectionThreadNetty])
+  val messageSignal = new Signal1[String]
+  val mutex = new QMutex()
+  var loopConnectionNetty = true
+  var msg = ""
+  mutex.lock()
+
+  def run() {
+    msg.synchronized {
+      while (loopConnectionNetty) {
+        mutex.lock()
+        messageSignal.emit(msg)
+      }
+    }
+  }
+
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.