Skip to content

Commit

Permalink
use db configuration and fix json decode issue
Browse files Browse the repository at this point in the history
  • Loading branch information
scottliyq committed Jun 21, 2015
1 parent 0074fbd commit 15ba270
Show file tree
Hide file tree
Showing 9 changed files with 361 additions and 32 deletions.
42 changes: 33 additions & 9 deletions engine/InvokerEngine.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package engine
import (
"adexchange/lib"
m "adexchange/models"
"encoding/json"
"github.com/astaxie/beego"
"github.com/astaxie/beego/httplib"
"time"
Expand Down Expand Up @@ -40,7 +41,7 @@ var _AvbAdspaceRegionTargeting map[string]bool
var _AvbAdSpaceRegion map[string]bool

func init() {
test()
//test()
}

func test() {
Expand All @@ -63,6 +64,10 @@ func test() {

func InvokeDemand(adRequest *m.AdRequest) *m.AdResponse {

if _AdspaceMap == nil || _AdspaceDemandMap == nil || _DemandMap == nil {
return &m.AdResponse{StatusCode: lib.ERROR_INITIAL_FAILED}
}

adspaceKey := adRequest.AdspaceKey

demandIds := _AdspaceDemandMap[adspaceKey]
Expand Down Expand Up @@ -130,7 +135,7 @@ func invokeMH(demand *Demand) {
req.Param("conn", adRequest.Conn)
req.Param("carrier", adRequest.Carrier)
req.Param("apitype", adRequest.ApiType)
req.Param("os", adRequest.Os)
req.Param("os", string(adRequest.Os))
req.Param("osv", adRequest.Osv)
req.Param("imei", adRequest.Imei)
req.Param("wma", adRequest.Wma)
Expand All @@ -149,17 +154,36 @@ func invokeMH(demand *Demand) {
req.Param("lat", adRequest.Lat)

var resultMap map[string]*m.MHAdUnit
req.ToJson(&resultMap)

if resultMap != nil {
for _, v := range resultMap {
demand.Result <- mapMHResult(v)
break
}
b, err := req.Bytes()

if err != nil {
beego.Error(err.Error())
demand.Result <- generateErrorResponse(lib.ERROR_MHSERVER_ERROR)
} else {
demand.Result <- generateErrorResponse(lib.ERROR_MH_ERROR)
err = json.Unmarshal(lib.EscapeCtrl(b), &resultMap)
if resultMap != nil {
for _, v := range resultMap {
demand.Result <- mapMHResult(v)
break
}
} else {
beego.Error(err.Error())
demand.Result <- generateErrorResponse(lib.ERROR_MAP_ERROR)
}
}

//req.ToJson(&resultMap)

//if resultMap != nil {
// for _, v := range resultMap {
// demand.Result <- mapMHResult(v)
// break
// }
//} else {
// demand.Result <- generateErrorResponse(lib.ERROR_MH_ERROR)
//}

}

func mapMHResult(mhAdunit *m.MHAdUnit) (adResponse *m.AdResponse) {
Expand Down
Binary file added ip.dat
Binary file not shown.
4 changes: 3 additions & 1 deletion lib/Constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ const ERROR_ILLEGAL_SERVER_IP = 407

const ERROR_PARSE_REQUEST = 601
const ERROR_MSGPACK_IMP = 602
const ERROR_INITIAL_FAILED = 603
const ERROR_UNKNON_ERROR = 701
const ERROR_MH_ERROR = 702
const ERROR_MHSERVER_ERROR = 702
const ERROR_MAP_ERROR = 703
30 changes: 30 additions & 0 deletions lib/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,33 @@ func GetBytes(key interface{}) ([]byte, error) {
}
return buf.Bytes(), nil
}

func DivisionInt(first int, second int) float32 {
if second == 0 || first == 0 {
return 0.0
} else {
f := float32(first) / float32(second)
return f
}
}

func EscapeCtrl(ctrl []byte) (esc []byte) {
u := []byte(`\u0000`)
for i, ch := range ctrl {
if ch <= 31 {
if esc == nil {
esc = append(make([]byte, 0, len(ctrl)+len(u)), ctrl[:i]...)
}
esc = append(esc, u...)
hex.Encode(esc[len(esc)-2:], ctrl[i:i+1])
continue
}
if esc != nil {
esc = append(esc, ch)
}
}
if esc == nil {
return ctrl
}
return esc
}
31 changes: 16 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,25 @@ import (
// beego.Controller
//}

func setup() {
//flag.Parse()
//pool = newPool(*redisServer, *redisPassword)
beego.SetLogger("file", `{"filename":"logs/admux.log"}`)
beego.SetLogFuncCall(true)
//func setup() {
// //flag.Parse()
// //pool = newPool(*redisServer, *redisPassword)
// beego.SetLogger("file", `{"filename":"logs/admux.log"}`)
// beego.SetLogFuncCall(true)

//c1 := httpclient.NewHttpClient().Defaults(httpclient.Map{
// httpclient.OPT_USERAGENT: "browser1", httpclient.OPT_CONNECTTIMEOUT_MS: 300, httpclient.OPT_TIMEOUT_MS: 80,
//})
// //c1 := httpclient.NewHttpClient().Defaults(httpclient.Map{
// // httpclient.OPT_USERAGENT: "browser1", httpclient.OPT_CONNECTTIMEOUT_MS: 300, httpclient.OPT_TIMEOUT_MS: 80,
// //})

//c1.Get("http://www.baidu.com/", nil)
// //c1.Get("http://www.baidu.com/", nil)

//c2 := httpclient.NewHttpClient().Defaults(httpclient.Map{
// httpclient.OPT_USERAGENT: "browser2", httpclient.OPT_CONNECTTIMEOUT_MS: 300, httpclient.OPT_TIMEOUT_MS: 80,
//})
// //c2 := httpclient.NewHttpClient().Defaults(httpclient.Map{
// // httpclient.OPT_USERAGENT: "browser2", httpclient.OPT_CONNECTTIMEOUT_MS: 300, httpclient.OPT_TIMEOUT_MS: 80,
// //})

//c2.Get("http://www.baidubee.com/", nil)
// //c2.Get("http://www.baidubee.com/", nil)

}
//}

func main() {
//setup()
Expand All @@ -61,7 +61,8 @@ func main() {
//beego.AdminHttpPort = 8888
//runtime.GOMAXPROCS(runtime.NumCPU())
m.Connect()
go tasks.ScheduleInit(100)
tasks.InitEngineData()
go tasks.ScheduleInit(5)
beego.SetLogger("file", `{"filename":"logs/admux.log"}`)
beego.SetLogFuncCall(true)
orm.Debug, _ = beego.AppConfig.Bool("orm_debug")
Expand Down
2 changes: 1 addition & 1 deletion models/AdRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type AdRequest struct {
Conn string `form:"conn"`
Carrier string `form:"carrier"`
ApiType string `form:"apitype"`
Os string `form:"os"`
Os int `form:"os"`
Osv string `form:"osv"`
Imei string `form:"imei"`
Wma string `form:"wma"`
Expand Down
2 changes: 1 addition & 1 deletion models/AdResponse.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type AdspaceData struct {
}

type PmpAdplaceInfo struct {
AdspaceKey string
PmpAdspaceKey string
DemandId int
DemandAdspaceKey string
DemandSecretKey string
Expand Down
10 changes: 5 additions & 5 deletions models/PmpDao.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func GetMatrixData() (adspaceMap map[string]AdspaceData, adspaceDemandMap map[string][]int, err error) {
o := orm.NewOrm()

sql := "select matrix.pmp_adspace_id, adspace.adspace_key, matrix.demand_id as demand_id,demand.adspace_key as demand_adspace_key,demand.secret_key as demand_adspace_secret from pmp_adspace_matrix as matrix inner join pmp_adspace as adspace on matrix.pmp_adspace_id=adspace.id inner join pmp_demand_adspace as demand on matrix.demand_adspace_id=demand.id order by adspace.adspace_key,matrix.priority"
sql := "select matrix.pmp_adspace_id, adspace.pmp_adspace_key, matrix.demand_id as demand_id,demand.demand_adspace_key as demand_adspace_key,demand.secret_key as demand_adspace_secret from pmp_adspace_matrix as matrix inner join pmp_adspace as adspace on matrix.pmp_adspace_id=adspace.id inner join pmp_demand_adspace as demand on matrix.demand_adspace_id=demand.id order by adspace.pmp_adspace_key,matrix.priority"

var dataList []PmpAdplaceInfo

Expand All @@ -31,18 +31,18 @@ func GetMatrixData() (adspaceMap map[string]AdspaceData, adspaceDemandMap map[st
for _, record := range dataList {
adspaceData := AdspaceData{AdspaceKey: record.DemandAdspaceKey}
adspaceData.SecretKey = record.DemandSecretKey
adspaceMap[record.AdspaceKey+"_"+lib.ConvertIntToString(record.DemandId)] = adspaceData
adspaceMap[record.PmpAdspaceKey+"_"+lib.ConvertIntToString(record.DemandId)] = adspaceData

if oldAdspaceKey != record.AdspaceKey {
oldAdspaceKey = record.AdspaceKey
if oldAdspaceKey != record.PmpAdspaceKey {
oldAdspaceKey = record.PmpAdspaceKey

if pmpDemandInfo != nil {
demandIds := pmpDemandInfo.GetDemandIds()
adspaceDemandMap[pmpDemandInfo.AdspaceKey] = demandIds
}
pmpDemandInfo = new(PmpDemandInfo)
pmpDemandInfo.InitDemand()
pmpDemandInfo.AdspaceKey = record.AdspaceKey
pmpDemandInfo.AdspaceKey = record.PmpAdspaceKey
pmpDemandInfo.AddDemand(record.DemandId)
} else {
pmpDemandInfo.AddDemand(record.DemandId)
Expand Down
Loading

0 comments on commit 15ba270

Please sign in to comment.