Figure 1 Include File for Text Query Application #include <algorithm>
#include <string>
#include <vector>
#include <utility>
#include <map>
#include <set>
#include <iostream>
#include <fstream>
#include <stddef.h>
#include <ctype.h>
using namespace std;
Figure 2 Data Representation typedef pair<short,short> location;
typedef vector<location> loc;
typedef vector<string> text;
typedef pair<text*,loc*> text_loc;
class TextQuery {
public:
// ...
private:
vector<string> *lines_of_text;
text_loc *text_locations;
map<string,loc*> *word_map;
Query *query;
static string filt_elems;
vector<int> line_cnt;
};
Figure 3 Query Session Enter a query-please separate each item by a space.
Terminate query (or session) with a dot( . ).
==> fiery && ( bird || shyly )
fiery ( 1 ) lines match
bird ( 1 ) lines match
shyly ( 1 ) lines match
( bird || shyly ) ( 2 ) lines match
fiery && ( bird || shyly ) ( 1 ) lines match
Requested query: fiery && ( bird || shyly )
( 3 ) like a fiery bird in flight. A beautiful fiery bird, he tells her,
Figure 4 Wrapping Native C++ Classes #include "TextQuery.h"
__gc class TextQueryNet
{
private:
TextQuery *pquery;
public:
TextQueryNet() : pquery( new TextQuery()){}
~TextQueryNet(){ delete pquery; }
void query_text() { pquery->query_text(); }
void build_up_text() { pquery->build_up_text();}
// ...
};
Figure 5 Generated Main Function #include "stdafx.h"
#using <mscorlib.dll>
#include <tchar.h>
using namespace System;
#include "gc_TextQuery.h"
int _tmain(void)
{
Console::WriteLine(
S"Beginning managed wrapper test ..." );
TextQueryNet *tqn = new TextQueryNet();
tqn->build_up_text();
tqn->query_text();
Console::WriteLine(
S"Ending managed wrapper test ..." );
return 0;
}
Figure 6 Simple Socket Server Class // include the necessary assemblies
#using <mscorlib.dll>
#using <System.dll>
#using <System.Data.dll>
#using <System.Xml.dll>
// open up the associated namespaces
using namespace System;
using namespace System::Threading;
using namespace System::Data;
using namespace System::Data::SqlClient;
using namespace System::Collections;
using namespace System::Net::Sockets;
// ok: here is our class
__gc class SocketDemo_Server
{
private:
static const int port = 4554;
static const int maxPacket = 128;
TcpListener *tcpl;
DataSet *ds;
DataRowCollection *rows;
public:
SocketDemo_Server();
void Start();
void handleConnection();
// grab the data from the SQL database
void retrieveData();
};
Figure 7 Server Output Server[4554]: OK: started TcpListener ...
Server[4554]: OK: listening for connections ...
Server[4554]: OK: retrieved SQL database info ...
Server[4554]: OK: a client connected ...
Server[4554]: OK: client requested phone # for Fuller
Server[4554]: OK: first request for Fuller
Server[4554]: Phone number for Fuller: (206) 555-9482
Server[4554]: OK: a client connected ...
Server[4554]: OK: client requested phone # for King
Server[4554]: OK: first request for King
Server[4554]: Phone number for King: (71) 555-5598
Server[4554]: OK: a client connected ...
Server[4554]: OK: client requested phone # for Fuller
Server[4554]: OK: cached request for Fuller
Server[4554]: Phone number for Fuller: (206) 555-9482
Server[4554]: OK: a client connected ...
Server[4554]: OK: client requested phone # for Musil
Server[4554]: OK: first request for Musil
Server[4554]: Phone number for Musil: Sorry. Cannot be found.
Figure 8 Start Function void SocketDemo_Server::
Start()
{
try
{
tcpl = new TcpListener( port );
tcpl->Start();
Console::WriteLine(
S"Server[{0}]: OK: started TcpListener ... ",
__box( port ));
// retrieve the data from the data base ...
Thread *tdata =
new Thread( new ThreadStart( this,
&SocketDemo_Server::retrieveData ));
tdata->Start(); // ok: kick off the thread ...
// thread to handle a socket connection ...
Thread *tconnect =
new Thread( new ThreadStart( this,
&SocketDemo_Server::handleConnection ));
tconnect->Start();
}
catch( Exception *ex )
{
Console::WriteLine(
S"Oops: Unable to Set Up SocketDemo_Server" );
Console::WriteLine( ex->ToString() );
}
}
|