Anderes Programm über eine TCP/IP Verbindung fernsteuern

Programmierung unter AOO/LO (StarBasic, Python, Java, ...)

Moderator: Moderatoren

RaKa
*
Beiträge: 12
Registriert: Di, 05.08.2008 18:50

Anderes Programm über eine TCP/IP Verbindung fernsteuern

Beitrag von RaKa »

Hallo!

Ich möchte mit OOBasic folgende Problemstellung lösen (unter Win XP):

Ein fremdes Programm soll über eine TCP/IP Verbindung über das eingebaute "Remote Control Interface" gesteuert werden.

Ich kann zum Beispiel mittels Hyperlink.exe eine "TCP/IP (Winsock)" Verbindung zum localhost (127.0.0.1) aufbauen und dann Befehle "kommandoorientiert" eingeben, die dieses externe Programm ausführt.
Prompt>Help?
Befehl1=xxx
Befehl2=xxx
Prompt>
Das Ganze versuche ich in OO Basic einzubauen. Folgenden Codeschnipsel habe ich nach langen Suchen gefunden und angepaßt. Er funktioniert so weit ganz gut, Befehle werden von dem externen Programm auch ausgeführt.

Code: Alles auswählen

REM  *****  BASIC  *****

Private oListener
Private oOutput

Sub Main


   oOutput = StarDesktop.loadComponentFromURL( "private:factory/swriter", "_blank", 0, Array() )
   
   Writer_PrintLn( oOutput, "Opening connection." )

   oConnector  = createUnoService( "com.sun.star.connection.Connector" )
 

   oConnection = oConnector.connect("socket,host=127.0.0.1,port=29858")
   

   
   ' Create a scream listener.
   Writer_PrintLn( oOutput, "Creating scream listener." )
   oListener = createUnoListener( "ScreamListener_", "com.sun.star.io.XStreamListener" )
   oConnection.addStreamListener( oListener )
   
   
   sCR = chr(13)
   sLF = chr(10)

   
   Writer_PrintLn( oOutput, "Sending HTTP request." )
   oConnection.write( StringToByteArray( "Help?" + sCR  ) )

      
   Writer_PrintLn( oOutput, "Flush output." )
   oConnection.flush()
   
      
   Writer_PrintLn( oOutput, "Reading result." )
   aByteArray = Array()
   
    Writer_PrintLn( oOutput, "Result follows..." )
    
    
   
   nBytesRead = oConnection.read( aByteArray, 50 )
  
   Writer_PrintLn( oOutput, ByteArrayToString( aByteArray ) )
   
    
   
   Writer_PrintLn( oOutput, "Read " + CStr( nBytesRead ) + " bytes." )

   Writer_PrintLn( oOutput, "Close connection." )
   oConnection.close()
   
   
   
  ' Remove scream listener.
   Writer_PrintLn( oOutput, "Removing scream listener." )
   oConnection.removeStreamListener( oListener )
End Sub




'########################################
'   My Scream Listener implementation.

' interface: com.sun.star.io.XStreamListener
Sub ScreamListener_started()
   Writer_PrintLn( oOutput, "ScreamListener_started() was called." )
End Sub

' interface: com.sun.star.io.XStreamListener
Sub ScreamListener_closed()
   Writer_PrintLn( oOutput, "ScreamListener_closed() was called." )
End Sub

' interface: com.sun.star.io.XStreamListener
Sub ScreamListener_terminated()
   Writer_PrintLn( oOutput, "ScreamListener_terminated() was called." )
End Sub

' interface: com.sun.star.io.XStreamListener
Sub ScreamListener_error( oException )
   Writer_PrintLn( oOutput, "ScreamListener_error() was called." )
End Sub

' interface: com.sun.star.lang.XEventListener
' oEventSource is a struct com.sun.star.lang.EventObject
Sub ScreamListener_disposing( oEventSource )
   Writer_PrintLn( oOutput, "ScreamListener_disposing() was called." )
End Sub

'########################################



'----------------------------------------
'   Stuff ripped out of my library
'----------------------------------------


' The next four routines were previously posted
'   http://www.oooforum.org/forum/viewtopic.php?t=6910


' Convert an array of bytes to a string.
' Pass in an array of bytes.
' Each "byte" in the array is an integer value from -128 to +127.
' The array of bytes could have come from reading
'  from a com.sun.star.io.XInputStream.
' This function returns a string.
' This function is the opposite of StringToByteArray().
Function ByteArrayToString( aByteArray )
   cBytes = ""
   For i = LBound( aByteArray ) To UBound( aByteArray )
      nByte = aByteArray(i)
      nByte = ByteToInteger( nByte )
      cBytes = cBytes + Chr( nByte )
   Next i
   ByteArrayToString() = cBytes
End Function


' Convert a string into an array of bytes.
' Pass a string value to the cString parameter.
' The function returns an array of bytes, suitable
'  for writing to a com.sun.star.io.XOutputStream.
' Each "byte" in the array is an integer value from -128 to +127.
' This function is the opposite of ByteArrayToString().
Function StringToByteArray( ByVal cString As String )
   nNumBytes = Len( cString )
   Dim aBytes(nNumBytes-1) As Integer
   For i = 1 To nNumBytes
      cChar = Mid( cString, i, 1 )
      nByte = Asc( cChar )
      nByte = IntegerToByte( nByte )
      aBytes(i-1) = nByte
   Next
   StringToByteArray() = aBytes()
End Function


' Convert a byte value from the range -128 to +127 into
'  an integer in the range 0 to 255.
' This function is the opposite of IntegerToByte().
Function ByteToInteger( ByVal nByte As Integer ) As Integer
   If nByte < 0 Then
      nByte = nByte + 256
   EndIf
   ByteToInteger() = nByte
End Function


' This function is the opposite of ByteToInteger().
Function IntegerToByte( ByVal nByte As Integer ) As Integer
   If nByte > 127 Then
      nByte = nByte - 256
   EndIf
   IntegerToByte() = nByte
End Function



' The next two routines were previously posted...
'   http://www.oooforum.org/forum/viewtopic.php?t=7068


' Sugar Coated way to Print into a Writer document.
' The oOutput parameter can be any of....
'   com.sun.star.text.TextDocument
'   com.sun.star.drawing.Text
'   com.sun.star.text.TextCursor
Sub Writer_Print( oOutput, cString )
   If oOutput.SupportsService( "com.sun.star.text.TextDocument" ) Then
      oText = oOutput.getText()
      oCursor = oText.createTextCursor()
   ElseIf oOutput.SupportsService( "com.sun.star.drawing.Text" ) Then
      oText = oOutput
      oCursor = oText.createTextCursor()
   ElseIf oOutput.SupportsService( "com.sun.star.text.Text" ) Then
      oText = oOutput
      oCursor = oText.createTextCursor()
   ElseIf oOutput.SupportsService( "com.sun.star.text.TextCursor" ) Then
      oCursor = oOutput
      oText = oCursor.getText()
   Else
      Exit Sub
   EndIf
   
   oCursor.gotoEnd( False )
   
   nLen = Len( cString )
   nStart = 1
   Do
      nPos = Instr( nStart, cString, Chr(13) )
      bCRFound = (nPos > 0)
      If Not bCRFound Then
         nPos = nLen + 1
      EndIf
      cSegment = Mid( cString, nStart, nPos-nStart )
      nStart = nPos + 1
      
      oText.insertString( oCursor, cSegment, False )
      
      If bCRFound Then
         oText.insertControlCharacter( oCursor, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False )
      EndIf
   Loop While bCRFound
End Sub

' Same as Writer_Print(), just adds a line ending.
Sub Writer_PrintLn( oOutput, Optional cString )
   If IsMissing( cString ) Then
      cString = ""
   EndIf
   Writer_Print( oOutput, cString + Chr(13) )
End Sub 
Das Problem sind die "Antworten" des zu steuernden Programmes, die ich natürlich auch auswerten möchte. Der Code nBytesRead = oConnection.read( aByteArray, 50 ) liest genau 50 Zeichen. Wenn die nicht da sind, wartet OO bis die Verbindunug manuell kurz unterbrochen wird und läuft erst dann weiter. Auch ein zeichenweises Auslesen (immer nur ein Zeichen in einer Schleife) führt nicht zum Erfolg, da ich kein spezifisches Endzeichen kenne. Beim letzten Zeichen bleibt wieder alles hängen.

Wie kann ich nur die gesendeten Zeichen auslesen?

Ich finde keine EOF vergleichbaren Methoden. Wie kann ich eine Funtion namens "readsomebytes()" aus XConnection2 einbauen? Ich finde im Netz keine Beispiele. Und die Sache mit "createUnoListener( "ScreamListener_"" habe ich auch nicht so ganz verstanden ....

Für Eure Hilfe schonmal vielen Dank!

Ralf
RaKa
*
Beiträge: 12
Registriert: Di, 05.08.2008 18:50

Re: Anderes Programm über eine TCP/IP Verbindung fernsteuern

Beitrag von RaKa »

kann denn keiner helfen .... ?
Antworten