Ararat Synapse
From Morfik Wiki
Morfik's Ararat Synapse Package provides an interface to the complete library of classes and functions for network communication application programming.
View Live Demo
| Download Package
|
A Quick Walkthrough
To use Morfik's Ararat Synapse Package, add the package to the project by using the ‘Used Packages’ command on the project ribbon (see Figure 1), or drag the package file onto the application main client area.
|
| Figure 1: Used Packages Command |
To use this package in your code, first you have to add mfk_Synapse to the ‘Uses’ section of your module.
After that you will gain access to all the functionality of the Ararat Synapse Library. The description of all the units, classes, interfaces, objects, records, types, variables, constants, identifiers, functions and procedures you can use with Ararat Synapse can be found here.
Code Examples for Morfik's Ararat Synapse Package
Below are four examples of Morfik's Ararat Synapse Package being implemented in a package demonstration.
Receiveng an email
FX Code
Procedure wm_POP3.mfk_Pop3Send(); Var pop: TPOP3Send; Begin //Create POP3 object pop := TPoP3Send.Create; //Enable SSL|TLS protocol pop.autoTLS := true; //Specify user's login pop.userName := fLogin; //Specify password pop.password := fPass; //Specify target server port (or symbolic name) for POP3 server pop.TargetHost:=fHost; //Type of authorisation for login to POP3 pop.authType := pop3AuthAll; //Specify POP3 server port pop.TargetPort := fPort; //Enable all features of SSL protocol pop.FullSSL := true; //Connect to POP3 server pop.Login; showMessage('Login : '+pop.resultString); //Send LIST command pop.list(0); showMessage('list : '+pop.resultString); //Get list of messages MailContent := 'Loaded : ' + pop.FullResult.ToString('<br>', true); //Downloading first message pop.retr(1); showMessage('retr : '+pop.resultString); //Get mail content MailContent := MailContent + '<br>------<br>' + pop.FullResult.ToString('<br>', true); //Disconnect from POP3 server pop.logout; showMessage('logout : '+pop.resultString); pop.Free; End;
| BX Code |
|---|
Public Sub mfk_Pop3Send Dim pop As TPOP3Send pop = New TPOP3Send() pop.AutoTLS = true pop.UserName = fLogin pop.Password = fPass pop.TargetHost = fHost pop.AuthType = pop3AuthAll pop.TargetPort = fPort pop.FullSSL = true pop.Login() ShowMessage("Login : " & pop.ResultString) pop.List(0) ShowMessage("list : " & pop.ResultString) MailContent = "Loaded : " & pop.FullResult.ToString("<br>", true) pop.Retr(1) ShowMessage("retr : " & pop.ResultString) MailContent = MailContent + "<br>------<br>" + pop.FullResult.ToString("<br>", true) pop.Logout() ShowMessage("logout : " & pop.ResultString) pop.Free() End Sub |
| CX Code |
|---|
public void mfk_Pop3Send() { TPOP3Send pop; pop = new TPOP3Send(); pop.AutoTLS = true; pop.UserName = fLogin; pop.Password = fPass; pop.TargetHost = fHost; pop.AuthType = pop3AuthAll; pop.TargetPort = fPort; pop.FullSSL = true; pop.Login(); ShowMessage("Login : " + pop.ResultString); pop.List(0); ShowMessage("list : " + pop.ResultString); MailContent = "Loaded : " + pop.FullResult.ToString("<br>", true); pop.Retr(1); ShowMessage("retr : " + pop.ResultString); MailContent = MailContent + "<br>------<br>" + pop.FullResult.ToString("<br>", true); pop.Logout(); ShowMessage("logout : " + pop.ResultString); pop.Free(); } |
Sending an email
FX Code
Procedure wm_SMTP.MailSend(Const sSmtpHost, sSmtpPort, sSmtpUser, sSmtpPasswd, sFrom, sTo, sSubject, sMesBody: String); Var smtp: TSMTPSend; msg_lines: TStringList; Begin msg_lines := TStringList.Create; //Create SMTP object smtp := TSMTPSend.Create; Try //Fill in the content of future mail msg_lines.Add('From: ' + sFrom); msg_lines.Add('To: ' + sTo); msg_lines.Add('Subject: ' + sSubject); If Length(sMesBody) > 0 Then Begin msg_lines.Add(''); msg_lines.Add(sMesBody); End; //==================================== //If authorization is required, then fill in username smtp.UserName := sSmtpUser; //Specify user's password smtp.Password := sSmtpPasswd; //Specify target server IP (or symbolic name) smtp.TargetHost := sSmtpHost; //Specify target server port smtp.TargetPort := sSmtpPort; //Enable SSL|TLS protocols smtp.autoTLS := true; //Connect to SMTP server If Not smtp.Login() Then Showmessage('SMTP ERROR: Login:' + smtp.EnhCodeString); //If you successfully pass authorization to the remote server If smtp.AuthDone Then Begin //Send MAIL FROM SMTP command to set sender’s e-mail address. If Not smtp.MailFrom(sFrom, Length(sFrom))Then Showmessage('SMTP ERROR: MailFrom:' + smtp.EnhCodeString); //Send RCPT TO SMTP command to set receiver’s e-mail address. If Not smtp.MailTo(sTo) Then Showmessage('SMTP ERROR: MailTo:' + smtp.EnhCodeString); //Send DATA SMTP command and transmit message data. If Not smtp.MailData(msg_lines) Then Showmessage('SMTP ERROR: MailData:' + smtp.EnhCodeString); End; //Close SMTP session (QUIT command) and disconnect from SMTP server. If Not smtp.Logout() Then Showmessage('SMTP ERROR: Logout:' + smtp.EnhCodeString); Finally msg_lines.Free; smtp.Free; End; End;
| BX Code |
|---|
Public Sub MailSend(Const sSmtpHost As String, Const sSmtpPort As String, Const sSmtpUser As String, Const sSmtpPasswd As String, Const sFrom As String, Const sTo As String, Const sSubject As String, Const sMesBody As String) Dim smtp As TSMTPSend Dim msg_lines As TStringList msg_lines = New TStringList() smtp = New TSMTPSend() Try msg_lines.Add("From: " & sFrom) msg_lines.Add("To: " & sTo) msg_lines.Add("Subject: " & sSubject) If Length(sMesBody) > 0 Then msg_lines.Add("") msg_lines.Add(sMesBody) End If smtp.UserName = sSmtpUser smtp.Password = sSmtpPasswd smtp.TargetHost = sSmtpHost smtp.TargetPort = sSmtpPort smtp.AutoTLS = true If Not smtp.Login() Then ShowMessage("SMTP ERROR: Login:" & smtp.EnhCodeString()) If smtp.AuthDone Then If Not smtp.MailFrom(sFrom, Length(sFrom)) Then ShowMessage("SMTP ERROR: MailFrom:" & smtp.EnhCodeString()) If Not smtp.MailTo(sTo) Then ShowMessage("SMTP ERROR: MailTo:" & smtp.EnhCodeString()) If Not smtp.MailData(msg_lines) Then ShowMessage("SMTP ERROR: MailData:" & smtp.EnhCodeString()) End If If Not smtp.Logout() Then ShowMessage("SMTP ERROR: Logout:" & smtp.EnhCodeString()) Finally msg_lines.Free() smtp.Free() End Try End Sub |
| CX Code |
|---|
public void MailSend(const String sSmtpHost, const String sSmtpPort, const String sSmtpUser, const String sSmtpPasswd, const String sFrom, const String sTo, const String sSubject, const String sMesBody) { TSMTPSend smtp; TStringList msg_lines; msg_lines = new TStringList(); smtp = new TSMTPSend(); try { msg_lines.Add("From: " + sFrom); msg_lines.Add("To: " + sTo); msg_lines.Add("Subject: " + sSubject); if (Length(sMesBody) > 0) { msg_lines.Add(""); msg_lines.Add(sMesBody); } smtp.UserName = sSmtpUser; smtp.Password = sSmtpPasswd; smtp.TargetHost = sSmtpHost; smtp.TargetPort = sSmtpPort; smtp.AutoTLS = true; if (!smtp.Login()) ShowMessage("SMTP ERROR: Login:" + smtp.EnhCodeString()); if (smtp.AuthDone) { if (!smtp.MailFrom(sFrom, Length(sFrom))) ShowMessage("SMTP ERROR: MailFrom:" + smtp.EnhCodeString()); if (!smtp.MailTo(sTo)) ShowMessage("SMTP ERROR: MailTo:" + smtp.EnhCodeString()); if (!smtp.MailData(msg_lines)) ShowMessage("SMTP ERROR: MailData:" + smtp.EnhCodeString()); } if (!smtp.Logout()) ShowMessage("SMTP ERROR: Logout:" + smtp.EnhCodeString()); } finally { msg_lines.Free(); smtp.Free(); } } |
Translating text using Google translate API
FX Code
Procedure wm_translate.Execute; Var Http : THTTPSend; str : TStringList; Begin //Create HTTPSend object Http := THttpSend.Create; //Reset headers and document and Mimetype Http.Clear; //Connects to host defined in URL and accesses resources defined in URL by method. if Http.httpMethod('GET','http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=' +encodeURIComponent(fInput)+'&langpair=en%7Ces') Then Begin str := TStringList.Create; //Save stream with document received from HTTP server str.loadFromStream(Http.Document); fOutput := str.Strings[0]; End; End;
| BX Code |
|---|
Public Overrides Sub Execute Dim Http As THTTPSend Dim str As TStringList Http = New THTTPSend() Http.Clear() If Http.HTTPMethod("GET", "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=" + encodeURIComponent(fInput) + "&langpair=en%7Ces") Then str = New TStringList() str.LoadFromStream(Http.Document) fOutput = str.Strings(0) End If End Sub |
| CX Code |
|---|
public override void Execute() { THTTPSend Http; TStringList str; Http = new THTTPSend(); Http.Clear(); if (Http.HTTPMethod("GET", "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=" + encodeURIComponent(fInput) + "&langpair=en%7Ces")) { str = new TStringList(); str.LoadFromStream(Http.Document); fOutput = str.Strings[0]; } } |
Getting resources
FX Code
Procedure wm_GetResource.GetRes; Const SQLINSERT = 'INSERT INTO "tblResources" ("FileName", "FileContent") VALUES (:"FileName", :"FileContent")'; SQLSELECT = 'SELECT COUNT(t."Id") as countid FROM "tblResources" t Where t."FileName" = :"FileName"'; SQLDELETE = 'DELETE FROM "tblResources" t Where t."FileName" = :"FileName"'; Var Http : THTTPSend; Command : TSQLCommand; HTTPServer : THTTPServer; DBParam : TDBParam; ColumnData : TMemoryStream; DBFileBlob : TDBFileBlob; DBFieldInfo : TDBFieldInfo; Begin //Initialization of Database connection and datafield access DBFieldInfo := Catalog.GetTableFieldByName('tblResources', 'FileContent'); If DBFieldInfo = Nil Then Exit; HTTPServer := THTTPServer.Create(Nil, Nil); Command := HTTPServer.DefaultDBConnection.CreateSQLCommand(''); ColumnData := TMemoryStream.Create; DBFileBlob := TDBFileBlob.Create(ColumnData, DBFieldInfo, False, False); //Create HTTPSend object Http := THttpSend.Create; //Reset headers and document and Mimetype Http.Clear; with Http do Begin //Connects to host defined in URL and accesses resources defined in URL by method. if HTTPMethod('GET',flURL) then Try //Searching for resources in the database that have the same name Command.SQL.Add(SQLSELECT); Command.Prepare; Command.ParamByName('"FileName"').AsString := flName; Command.Execute; //If resources with the same name are found then If Command.fieldByName('countid').asInteger > 0 Then Begin //Delete the resource with the same name Command.Free; Command := HTTPServer.DefaultDBConnection.CreateSQLCommand(''); Command.SQL.Add(SQLDELETE); Command.Prepare; Command.ParamByName('"FileName"').AsString := flName; Command.Execute; End; //Specify SQL query for adding new resources to database Command.Free; Command := HTTPServer.DefaultDBConnection.CreateSQLCommand(''); Command.SQL.Add(SQLINSERT); Command.Prepare; Command.ParamByName('"FileName"').AsString := flName; Try DBFileBlob.Clear(True); //Place resource stream into blob field DBFileBlob.LoadFromFile(flName, Document); DBFileBlob.SaveToDBField(Nil); DBParam := Command.ParamByName('"FileContent"'); If DBParam Is TDBFieldBlob Then TDBFieldBlob(DBParam).LoadFromStream(ColumnData); Except End; //Add new resources to database Command.Execute; flContent := flName; Except ShowMessage('Error. Cannot display resource'); End; Free; End; HTTPServer.DefaultDBConnection.DestroySQLCommand(Command); End;
| BX Code |
|---|
Public Sub GetRes Const SQLINSERT = "INSERT INTO ""tblResources"" (""FileName"", ""FileContent"") VALUES (:""FileName"", :""FileContent"")" Const SQLSELECT = "SELECT COUNT(t.""Id"") as countid FROM ""tblResources"" t Where t.""FileName"" = :""FileName""" Const SQLDELETE = "DELETE FROM ""tblResources"" t Where t.""FileName"" = :""FileName""" Dim Http As THTTPSend Dim Command As TSQLCommand Dim HTTPServer As THttpServer Dim DBParam As TIB_Column Dim ColumnData As TMemoryStream Dim DBFileBlob As TDBFileBlob Dim DBFieldInfo As TDBFieldInfo DBFieldInfo = Catalog().GetTableFieldByName("tblResources", "FileContent") If DBFieldInfo = Nothing Then Return HTTPServer = New THttpServer(Nothing, Nothing) Command = HTTPServer.DefaultDBConnection.CreateSQLCommand("") ColumnData = New TMemoryStream() DBFileBlob = New TDBFileBlob(ColumnData, DBFieldInfo, False, False) Http = New THTTPSend() Http.Clear() With Http If HTTPMethod("GET", flURL) Then Try Command.SQL.Add(SQLSELECT) Command.Prepare() Command.ParamByName("""FileName""").AsString = flName Command.Execute() If Command.FieldByName("countid").AsInteger > 0 Then Command.Free() Command = HTTPServer.DefaultDBConnection.CreateSQLCommand("") Command.SQL.Add(SQLDELETE) Command.Prepare() Command.ParamByName("""FileName""").AsString = flName Command.Execute() End If Command.Free() Command = HTTPServer.DefaultDBConnection.CreateSQLCommand("") Command.SQL.Add(SQLINSERT) Command.Prepare() Command.ParamByName("""FileName""").AsString = flName Try DBFileBlob.Clear(True) DBFileBlob.LoadFromFile(flName, Document) DBFileBlob.SaveToDBField(Nothing) DBParam = Command.ParamByName("""FileContent""") If DBParam Is TDBFieldBlob Then Ctype(DBParam, TDBFieldBlob).LoadFromStream(ColumnData) Catch End Try Command.Execute() flContent = flName Catch ShowMessage("Error. Cannot display resource") End Try End If Free() End With HTTPServer.DefaultDBConnection.DestroySQLCommand(Command) End Sub |
| CX Code |
|---|
public void GetRes() { const SQLINSERT = "INSERT INTO \"tblResources\" (\"FileName\", \"FileContent\") VALUES (:\"FileName\", :\"FileContent\")"; const SQLSELECT = "SELECT COUNT(t.\"Id\") as countid FROM \"tblResources\" t Where t.\"FileName\" = :\"FileName\""; const SQLDELETE = "DELETE FROM \"tblResources\" t Where t.\"FileName\" = :\"FileName\""; THTTPSend Http; TSQLCommand Command; THttpServer HTTPServer; TIB_Column DBParam; TMemoryStream ColumnData; TDBFileBlob DBFileBlob; TDBFieldInfo DBFieldInfo; DBFieldInfo = Catalog().GetTableFieldByName("tblResources", "FileContent"); if (DBFieldInfo == null) return; HTTPServer = new THttpServer(null, null); Command = HTTPServer.DefaultDBConnection.CreateSQLCommand(""); ColumnData = new TMemoryStream(); DBFileBlob = new TDBFileBlob(ColumnData, DBFieldInfo, False, False); Http = new THTTPSend(); Http.Clear(); with(Http) { if (HTTPMethod("GET", flURL)) { try { Command.SQL.Add(SQLSELECT); Command.Prepare(); Command.ParamByName("\"FileName\"").AsString = flName; Command.Execute(); if (Command.FieldByName("countid").AsInteger > 0) { Command.Free(); Command = HTTPServer.DefaultDBConnection.CreateSQLCommand(""); Command.SQL.Add(SQLDELETE); Command.Prepare(); Command.ParamByName("\"FileName\"").AsString = flName; Command.Execute(); } Command.Free(); Command = HTTPServer.DefaultDBConnection.CreateSQLCommand(""); Command.SQL.Add(SQLINSERT); Command.Prepare(); Command.ParamByName("\"FileName\"").AsString = flName; try { DBFileBlob.Clear(True); DBFileBlob.LoadFromFile(flName, Document); DBFileBlob.SaveToDBField(null); DBParam = Command.ParamByName("\"FileContent\""); if (DBParam is TDBFieldBlob) (TDBFieldBlob)(DBParam).LoadFromStream(ColumnData); } catch { } Command.Execute(); flContent = flName; } catch { ShowMessage("Error. Cannot display resource"); } } Free(); } HTTPServer.DefaultDBConnection.DestroySQLCommand(Command); } |

